前篇講到了Datatables的基本用法,連接地址:http://www.cnblogs.com/wumian1360/p/4263129.htmlhtml
今天來實現5,6,7三點。ide
其實Datatables控件自己就已經帶了分頁屬性,排序屬性和篩選屬性,分別是:bPaginate,bSort,bFilter,咱們只須要將這三個屬性設置爲true,那麼在Ajax刷新的時候就會在Query String Parameters有參數了。具體以下:this
1 sEcho:1 //操做次數,具體用途不是很清楚 2 iColumns:2 //Datatables的列總數 3 sColumns:Id,Name //列名 4 iDisplayStart:0 //分頁開始頁碼 5 iDisplayLength:10 //每頁顯示行數 6 mDataProp_0:Id //第一列映射字段 7 sSearch_0: //第一列篩選內容 8 bRegex_0:false //該字段是否使用正則 9 bSearchable_0:true //是否使用篩選功能 10 bSortable_0:false //是否能夠排序 11 mDataProp_1:Name //第一列映射字段 12 sSearch_1: //第一列篩選內容 13 bRegex_1:false //該字段是否使用正則 14 bSearchable_1:true //是否使用篩選功能 15 bSortable_1:true //是否能夠排序 16 sSearch: //全文篩選 17 bRegex:false //是否使用正則 18 iSortCol_0:0 //當前排序列索引(第一列) 19 sSortDir_0:asc //排序爲升序 20 iSortingCols:1 //排序的列數 21 _:1423311150960
功能很強大啊,什麼都有了,那麼只須要建立對象來接受處理這些參數,而後利用對象去分別作分頁,排序和篩選就可。spa
該處理對象類用了「冠軍」的代碼,具體連接:http://www.cnblogs.com/haogj/archive/2011/03/21/1990595.htmlcode
有了該對象,那麼能夠在具體的Controller中處理了。htm
代碼以下:對象
1 public JsonResult Get() 2 { 3 DataTablesRequest parm = new DataTablesRequest(this.Request); //處理對象 4 int totalCount = 0; 5 int start = parm.iDisplayStart; //頁索引 6 int length = parm.iDisplayLength; //頁行數 7 string order = string.Empty; //排序 8 9 //獲取排序 10 if (parm.SortColumns.Count() > 0) 11 { 12 string sortField = parm.Columns[parm.SortColumns[0].Index].Name; 13 string sort = parm.SortColumns[0].Direction.ToString(); 14 order = sortField + " " + sort; 15 } 16 17 string strQuery = parm.Search; 18 string[] fields = new string[parm.iColumns]; 19 //此處能夠建立對象來封裝全文查詢字段內容,簡單,我的去實現便可 20 21 var list=XXXX.LoadPage(start,length,out totalCount,order,strQuery,fields) 22 23 return Json(new { aaData = list, iTotalRecords = totalCount, iTotalDisplayRecords = totalCount }, 24 JsonRequestBehavior.AllowGet); 25 }