欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MVC异步分页代码分享

程序员文章站 2023-08-17 15:40:38
如图:   1、控制器代码  // // get: /ajaxuser/ shopentities s...

如图:

MVC异步分页代码分享 

1、控制器代码 

//

    // get: /ajaxuser/

    shopentities shop = new shopentities();
    public actionresult index()
    {
      return view();
    }
    public actionresult loadjson()

    {
      int pagesize = request["pagesize"] == null ? 10 : int.parse(request["pagesize"]);
      int pageindex = request["pageindex"] == null ? 1 : int.parse(request["pageindex"]);
      int totalcount = shop.tbl_admin.count();
 

      //给前台userinfo所有的数据,并且是json格式
      var allorder = shop.tbl_admin.orderby(u=>u.id)
        .skip(pagesize*(pageindex-1))
        .take(pagesize)
        .tolist();
      //接受一个对像,内部把此对象使用javascript序列化类对象志字符串,发送到前台
      var data = from u in allorder select new { u.id,u.realname,u.sex};
      string strnav = pagenavhelper.showpagenavigate(pageindex,pagesize,totalcount);
      var result = new {data=data, navstr=strnav };
      return json(result, jsonrequestbehavior.allowget);

    }

2、html代码 

@{
  layout = null;
}
<!doctype html>
<html>

<head>

  <meta name="viewport" content="width=device-width" />

  <title>index</title>
  <link href="~/content/navpage.css" rel="stylesheet" />
  <script src="~/scripts/jquery-1.8.2.min.js"></script>
  <script src="~/scripts/jquery-ui-1.8.24.js"></script>
  <script src="~/scripts/jquery.easyui.min.js"></script>
  <script src="~/scripts/jquery.unobtrusive-ajax.js"></script>
  <script src="~/scripts/jquery.validate.unobtrusive.js"></script>
  <script type="text/javascript">
    $(function () {
      //页面加载完成后从后如加载当前页数据
      inittable();
    });

  //初始化表格数据

    function inittable(querydata) 

    {

      $.getjson("/ajaxuser/loadjson",querydata, function (data) {
        var tb = $("#tblist");
        //先移除旧的,添加新的
        $("#tblist tr[type=data]").remove();
        for (var i = 0; i < data.data.length; i++)
        { 

          var strtr = "<tr type='data'>";
          strtr += "<td>" + data.data[i].id + "</td>";
          strtr += "<td>" + data.data[i].realname + "</td>";
          strtr += "<td>" + data.data[i].sex + "</td>";
          strtr += "<td><a uid='" + data.data[i].id + "' href='javascript:void(0)'>修改</a>" +
            "<a uid='" + data.data[i].id + "' href='javascript:void(0)'>删除</a></td>";
          strtr += "</tr>";
          tb.append(strtr);

        }

        $("#nav").html(data.navstr);

        //绑定分页标签的点击事件

        $(".pagelink").click(function () {
          //把页码弹出来
          var strhref = $(this).attr("href");
          var querystr = strhref.substr(strhref.indexof('?') + 1);
          //alert(querystr);
          inittable(querystr);
          return false;
        });
      });
    }
  </script>

</head>
<body>
  <div>
    <table id="tblist">
      <tr>
        <td>id</td>

        <td>姓名</td>
        <td>性别</td>
        <td>操作</td>

      </tr>
    </table>
    <div id="nav" class="paginator"> 
    </div>     
  </div>

</body>

</html> 

3、分页类

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.web;
namespace mvctest4.models

{
  public class pagenavhelper

  {
    //主要就是输出分页的超级链接的标签

    //自定义分页helper扩展

    public static string showpagenavigate(int currentpage, int pagesize, int totalcount)
    {

      var redirectto = httpcontext.current.request.url.absolutepath;
      pagesize = pagesize <= 0 ? 3 : pagesize;
      var totalpages = math.max((totalcount + pagesize - 1) / pagesize, 1); //总页数
       var output = new stringbuilder();
      if (totalpages > 1)

      {

        //if (currentpage != 1)

        {//处理首页连接

          output.appendformat("<a class='pagelink' href='{0}?pageindex=1&pagesize={1}'>首页</a> ", redirectto, pagesize);
        }
        if (currentpage > 1)
        {//处理上一页的连接
          output.appendformat("<a class='pagelink' href='{0}?pageindex={1}&pagesize={2}'>上一页</a> ", redirectto, currentpage - 1, pagesize);
        }

        else
        {
          // output.append("<span class='pagelink'>上一页</span>");

        }
        output.append(" ");
        int currint = 5;
        for (int i = 0; i <= 10; i++)
        {//一共最多显示10个页码,前面5个,后面5个
          if ((currentpage + i - currint) >= 1 && (currentpage + i - currint) <= totalpages)
          {

            if (currint == i)

            {//当前页处理

              //output.append(string.format("[{0}]", currentpage));

              output.appendformat("<a class='cpb' href='{0}?pageindex={1}&pagesize={2}'>{3}</a> ", redirectto, currentpage, pagesize, currentpage);
            }
            else
            {//一般页处理
              output.appendformat("<a class='pagelink' href='{0}?pageindex={1}&pagesize={2}'>{3}</a> ", redirectto, currentpage + i - currint, pagesize, currentpage + i - currint);

            }
          }
          output.append(" ");

        }

        if (currentpage < totalpages)
        {//处理下一页的链接
          output.appendformat("<a class='pagelink' href='{0}?pageindex={1}&pagesize={2}'>下一页</a> ", redirectto, currentpage + 1, pagesize);
        }
        else
        {
          //output.append("<span class='pagelink'>下一页</span>");
        }
        output.append(" ");
        if (currentpage != totalpages)
        {
          output.appendformat("<a class='pagelink' href='{0}?pageindex={1}&pagesize={2}'>末页</a> ", redirectto, totalpages, pagesize);
        }
        output.append(" ");
      }
      output.appendformat("第{0}页 / 共{1}页", currentpage, totalpages);//这个统计加不加都行
 
      return output.tostring();

    }

  }

} 

4、分页css 

body {
} 
.paginator {
  font: 12px arial, helvetica, sans-serif;
  padding: 10px 20px 10px 0;
  margin: 0px;
}

  .paginator a {
    border: solid 1px #ccc;
    color: #0063dc;
    cursor: pointer;
    text-decoration: none;
  }
    .paginator a:visited {
      padding: 1px 6px;
      border: solid 1px #ddd;
      background: #fff;
      text-decoration: none;
    }
  .paginator .cpb {
    border: 1px solid #f50;
    font-weight: 700;
    color: #f50;
    background-color: #ffeee5;
  }

  .paginator a:hover {
    border: solid 1px #f50;
    color: #f60;
    text-decoration: none;
  }
  .paginator a, .paginator a:visited, .paginator .cpb, .paginator a:hover {
    float: left;
    height: 16px;
    line-height: 16px;
    min-width: 10px;
    _width: 10px;
    margin-right: 5px;
    text-align: center;
    white-space: nowrap;
    font-size: 12px;
    font-family: arial,simsun;
    padding: 0 3px;

  } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。