Bootstrap表格分頁(一)


最近在學習Bootstrap的分頁,有一種方法用“Bootstrap-Paginator”的東西來做。

先預覽一下:

為了能夠局部刷新頁面,我創建了一個PartialView

 

頁面的HTML部分如下:

 1 < div class ="tableContainer">
 2     < input id ="currentPage" type ="hidden" value =" @ViewData[ "currentPage"] "/>
 3     < input id ="totalPages" type ="hidden" value =" @ViewData["totalPages" ] " />
 4     < table class ="table table-hover table-striped">
 5         < thead>
 6             < tr>
 7                 < th class ="col-md-4 text-center"> 乘車碼 </th >
 8                 < th class ="col-md-4 text-center"> 訂單號 </th >
 9                 < th class ="col-md-4 text-center"> 訂單日期 </th >
10             </ tr>
11         </ thead>
12         < tbody>
13             @foreach ( var item in Model)
14             {
15                 < tr>
16                     < td> @item.BusNo </ td>
17                     < td> @item.OrderId </ td>
18                     < td> @item.OrderDate </ td>
19                 </ tr>
20             }
21         </ tbody>
22     </ table>
23     < ul id ="example"></ ul>
24 </ div >
View Code  
頁面的JavaScript部分如下:(此處需要引用插件 bootstrap-paginator,具體的使用方法也在官網能看到,這里就不再詳述)
 1 @ Scripts.Render( "~/bundles/bootstrap-paginator" )
 2 < script type ="text/javascript">
 3     $( '#example' ).bootstrapPaginator({
 4         currentPage: $( '#currentPage' ).val(),   //當前頁
 5         totalPages: $( '#totalPages' ).val(),     //總頁數
 6         bootstrapMajorVersion: 3,               //兼容Bootstrap3.x版本
 7         tooltipTitles: function (type, page) {
 8             switch (type) {
 9                 case "first" :
10                     return "第一頁" ;
11                 case "prev" :
12                     return "上一頁" ;
13                 case "next" :
14                     return "下一頁" ;
15                 case "last" :
16                     return "最后一頁" ;
17                 case "page" :
18                     return page;
19             }
20             return "" ;
21         },
22         onPageClicked: function (event, originalEvent, type, page) {
23             $.get( '/Home/GetPaginationData' , { currentPage: page, pageSize:10 }, function (view) {
24                 $( '#tableTest' ).html(view);
25             });
26         }
27     });
28 </ script >
View Code
上面的“#tableTest”是一個div,是< div class ="tableContainer">的父節點,在父頁面中占位,就是說當頁面數據返回將刷新整個PartialView,后台是一個GET,如下:
 
 1 public ActionResult GetPaginationData( int currentPage = 1, int pageSize = 10)
 2         {
 3             using (var context = new TestEntities ())
 4             {
 5                 int count;
 6                 var q = (from a in context.Tickets
 7                          join b in context.Order on a.OrderId equals b.Id
 8                          select new TableItem
 9                          {
10                              BusNo = a.BusNumber,
11                              OrderId = b.Id,
12                              OrderDate = b.OrderDateTime,
13                          }).Pagination(currentPage, pageSize, out count);
14                 var data = q.ToList();
15                 ViewData[ "totalPages" ] = count / pageSize + 1; //計算分頁后總的頁面數
16                 ViewData[ "currentPage" ] = currentPage;  //當前頁碼
17                 return PartialView("~/Views/Home/OrderList.cshtml" ,data);
18             }
19         }
View Code

 

      
 其中的Pagination擴展函數用於數據庫分頁,請參考我的另外一篇博文 “ Entity Framework 分頁處理
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM