生活随笔
收集整理的這篇文章主要介紹了
(easyui datagrid+mvc+json)之asp.net分页查询
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?最近在做分頁查詢的功能,在網(wǎng)上也翻看了不少,但是自己的吸收能力就差了好多,而且當(dāng)時(shí)最大的想法就是,怎么就沒有我想要的那種,既是easyui的,又要用mvc的架構(gòu),還要能夠?qū)崿F(xiàn)底層的分頁傳值,用.net平臺(tái)寫的demo,那時(shí)就想,等我做出來了,我也要寫一篇分頁查詢的博客,專門為了實(shí)現(xiàn)這種需求來做的demo。
效果圖
? ? ?
前臺(tái)view
[html]?view plaincopyprint?
<table?id="list_data"?class="easyui-datagrid"?style="width:1075px;height:300px"?cellpascing="0"?cellpadding="0"????></table>??
[javascript]?view plaincopyprint?
<script?type="text/javascript">?? ?????????? ????????$(function(){?? ???????????????????????????$('#list_data').datagrid({?? ???????????????????????????????title:?'建議',?? ???????????????????????????????iconCls:?'icon-view',??? ???????????????????????????????loadMsg:"數(shù)據(jù)加載中,請(qǐng)稍后......",?? ???????????????????????????????width:1056,?? ???????????????????????????????height:'auto',?? ???????????????????????????????nowrap:false,?? ???????????????????????????????striped:true,?? ???????????????????????????????border:true,?? ???????????????????????????????collapsible:false,??? ???????????????????????????????fit:true,??? ???????????????????????????????url:?"/EvaluationSuggestion/GetData",????? ???????????????????????????????remoteSort:false,?? ???????????????????????????????singleSelect:true,???? ???????????????????????????????pagination:true,???? ???????????????????????????????rownumbers:?false,???? ???????????????????????????????? ???????????????????????????????columns:?[[?? ????????????????????????????????????{?field:?'SuggestionContent',?title:?'建議',?width:?'1056'?},???? ???????????????????????????????]],?? ???????????????????????????});?? ????????????????????????????????? ???????????????????????????????var?p=$('#list_data').datagrid('getPager');?? ???????????????????????????????$(p).pagination({?? ???????????????????????????????pageNumber:1,??? ???????????????????????????????pageSize:10,?? ???????????????????????????????pageList:[5,10,15],?? ???????????????????????????????beforePageText:'第',?? ???????????????????????????????afterPageText:'頁?????共{pages}頁',?? ???????????????????????????????displayMsg:'當(dāng)前顯示{from}-{to}條記錄??共{total}條記錄'?? ???????????????????????????});?? ???????????????????????? ????????});?? ?? ?????????</script>??
解說:如果想要進(jìn)行分頁,首先就必須將datagrid的屬性pagination設(shè)置為true。
pager是分頁欄,這個(gè)標(biāo)簽用來設(shè)置分頁的總體參數(shù),
url是鏈接的重要根地址,pager標(biāo)簽會(huì)在這個(gè)鏈接的基礎(chǔ)上附加分頁參數(shù)。
?
Controller.cs
[csharp]?view plaincopyprint?
#region?查詢建議的controller?? ???????? ???????? ???????? ???????? ??????public?ActionResult?SuggestionIndex()?? ??????{?? ?????????? ????????????return?View();?? ??????}?? ??????#endregion?? ? ??????#region?將建議數(shù)據(jù)轉(zhuǎn)換成json字符串?? ???????? ???????? ???????? ???????? ??????public?JsonResult??GetData()?? ??????{?? ??????????IEvaluationSuggestionWCF?suggestion?=?ServiceFactory.GetEvaluationSuggestion();?? ??????????List<EvaluationSuggestion>?lstResut?=?new?List<EvaluationSuggestion>();?? ???????????? ??????????var?pageIndex?=?int.Parse(Request["page"]);???? ??????????var?pageSize?=?int.Parse(Request["rows"]);???? ??????????var?total?=?0;?? ??????????lstResut?=?suggestion.QuerySuggestionbyPage(pageSize,?pageIndex,?out?total);?? ??????????var?data?=?new?? ??????????{?? ??????????????total,?? ??????????????rows?=?lstResut?? ??????????};?? ??????????return?Json(data,?JsonRequestBehavior.AllowGet);?? ?? ??????}?? ??????#endregion??
解說:page和rows是直接可以從前臺(tái)獲取的。data設(shè)置數(shù)據(jù)格式,轉(zhuǎn)換成Json字符串后,能夠在分頁中正確獲取。
服務(wù)端
[csharp]?view plaincopyprint?
#region?分頁查詢?+?排序:?? ?????????? ?????????? ?????????? ?????????? ?????????? ?????????? ?????????? ?????????? ?????????? ?????????? ????????public?IQueryable<T>?LoadPageItems<Tkey>(int?pageSize,?int?pageIndex,?out?int?total,?Func<T,?Tkey>?orderbyLambda,?bool?isAsc)?? ????????{?? ????????????total?=?MyBaseDbContext.Set<T>().Count();?? ????????????if?(isAsc)?? ????????????{?? ????????????????var?temp?=?MyBaseDbContext.Set<T>().OrderBy<T,?Tkey>(orderbyLambda)?? ?????????????????????????????.Skip(pageSize?*?(pageIndex?-?1))?? ?????????????????????????????.Take(pageSize);?? ????????????????return?temp.AsQueryable();?? ????????????}?? ????????????else?? ????????????{?? ????????????????var?temp?=?MyBaseDbContext.Set<T>().OrderByDescending<T,?Tkey>(orderbyLambda)?? ???????????????????????????.Skip(pageSize?*?(pageIndex?-?1))?? ???????????????????????????.Take(pageSize);?? ????????????????return?temp.AsQueryable();?? ????????????}?? ????????}?? ????????#endregion??
解說:這個(gè)是我們底層的類庫,我直接貼過來的,底層使用EF,涉及到lambda表達(dá)式。不過除去形式,分頁查詢的思想都是一樣的,真分頁,根據(jù)記錄總數(shù),每頁記錄數(shù)和當(dāng)前頁碼,獲取當(dāng)前頁碼的數(shù)據(jù)集合。頁數(shù)=總記錄數(shù)/每頁記錄數(shù)。當(dāng)前頁碼的數(shù)據(jù)集合,向數(shù)據(jù)庫傳遞條件篩選,from(當(dāng)前頁碼-1)*每頁記錄數(shù)?to當(dāng)前頁碼*每頁記錄數(shù)獲取當(dāng)前頁碼數(shù)據(jù)集合。具體實(shí)現(xiàn)自己來做吧。
總結(jié)
以上是生活随笔為你收集整理的(easyui datagrid+mvc+json)之asp.net分页查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。