MVC非同步分頁

來源:互聯網
上載者:User

標籤:style   blog   class   code   java   ext   

 

1: 控制器代碼

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 //         // 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;    }

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.