分页3
http://www.cnblogs.com/ChrisLee2011/p/4288194.html
?
頁面的HTML部分如下:
< div class ="tableContainer">
< input id ="currentPage" type ="hidden" value =" @ViewData[ "currentPage"] "/>
< input id ="totalPages" type ="hidden" value =" @ViewData["totalPages" ] " />
< table class ="table table-hover table-striped">
< thead>
< tr>
< th class ="col-md-4 text-center"> 乘車碼 </th >
< th class ="col-md-4 text-center"> 訂單號 </th >
< th class ="col-md-4 text-center"> 訂單日期 </th >
</ tr>
</ thead>
< tbody>
@foreach ( var item in Model)
{
< tr>
< td> @item.BusNo </ td>
< td> @item.OrderId </ td>
< td> @item.OrderDate </ td>
</ tr>
}
</ tbody>
</ table>
< ul id ="example"></ ul>
</ div >
?
頁面的JavaScript部分如下:(此處需要引用插件bootstrap-paginator,具體的使用方法也在官網(wǎng)能看到,這里就不再詳述)
@ Scripts.Render( "~/bundles/bootstrap-paginator" )
< script type ="text/javascript">
$( '#example' ).bootstrapPaginator({
currentPage: $( '#currentPage' ).val(), //當(dāng)前頁
totalPages: $( '#totalPages' ).val(), //總頁數(shù)
bootstrapMajorVersion: 3, //兼容Bootstrap3.x版本
tooltipTitles: function (type, page) {
switch (type) {
case "first" :
return "第一頁" ;
case "prev" :
return "上一頁" ;
case "next" :
return "下一頁" ;
case "last" :
return "最后一頁" ;
case "page" :
return page;
}
return "" ;
},
onPageClicked: function (event, originalEvent, type, page) {
$.get( '/Home/GetPaginationData' , { currentPage: page, pageSize:10 }, function (view) {
$( '#tableTest' ).html(view);
});
}
});
</ script >
?
上面的“#tableTest”是一個div,是<?div?class?="tableContainer">的父節(jié)點,在父頁面中占位,就是說當(dāng)頁面數(shù)據(jù)返回將刷新整個PartialView,后臺是一個GET,如下:
?
public ActionResult GetPaginationData( int currentPage = 1, int pageSize = 10)
{
using (var context = new TestEntities ())
{
int count;
var q = (from a in context.Tickets
join b in context.Order on a.OrderId equals b.Id
select new TableItem
{
BusNo = a.BusNumber,
OrderId = b.Id,
OrderDate = b.OrderDateTime,
}).Pagination(currentPage, pageSize, out count);
var data = q.ToList();
ViewData[ "totalPages" ] = count / pageSize + 1; //計算分頁后總的頁面數(shù)
ViewData[ "currentPage" ] = currentPage; //當(dāng)前頁碼
return PartialView("~/Views/Home/OrderList.cshtml" ,data);
}
}
?
?其中的Pagination擴展函數(shù)用于數(shù)據(jù)庫分頁,請參考我的另外一篇博文 “Entity Framework 分頁處理”
轉(zhuǎn)載于:https://www.cnblogs.com/lacey/p/5596048.html
總結(jié)
- 上一篇: 软件工程学习感想
- 下一篇: 一张传票引爆世界首富朋友圈 马斯克好友怒