關於jquery datables的在服務器端的排序,在網上貌似沒有看到.NET的例子,說實話,以前我也迷惑過,習慣了直接從網上找現成的東西,通過一翻搜索,沒找到,因而乎,本身調試唄,調了前臺,調後臺,還真被我看出了規律。事實上datables是支持多列排序的,可是本例,我只寫了單列排序。jquery
在控制器中,數組
Dictionary<int, string> dicSort = new Dictionary<int, string>(); //排序字段鍵值對列表 (列序號,列名稱) /// <summary> /// 運單異常數據 /// </summary> /// <returns></returns> public ActionResult WayBillException(WayBillExceptionFilter filter) { return View(filter); } public JsonResult WayBillExceptionList(WayBillExceptionFilter filter) { dicSort.Add(2, "w.PostingTime"); DataTablesRequest parm = new DataTablesRequest(this.Request); //處理對象 int pageIndex = parm.iDisplayLength == 0 ? 0 : parm.iDisplayStart / parm.iDisplayLength; filter.PageIndex = pageIndex; //頁索引 filter.PageSize = parm.iDisplayLength; //頁行數 string strSortField = dicSort.Where(x => x.Key == parm.SortColumns[0].Index).Select(x => x.Value).FirstOrDefault(); string strSortDire = parm.SortColumns[0].Direction == SortDirection.Asc ? "asc" : "desc"; filter.OrderBy = " " + strSortField + " " + strSortDire; var DataSource = Core.Reconciliation.WayBillException.GetByFilter(filter) as WRPageOfList<WayBillException>; int i = parm.iDisplayLength * pageIndex; List<WayBillException> queryData = DataSource.ToList(); var data = queryData.Select(u => new { Index = ++i, //行號 ID = u.ID, IsInputCost = u.IsInputCost, CusName = u.CusName, //客戶簡稱 PostingTime = u.PostingTime == null ? string.Empty : u.PostingTime.Value.ToStringDate(),//收寄日期 ExpressNo = u.ExpressNo, //運單號 BatchNO = u.LoadBillNum, //提單號 Weight = u.Weight == null ? 0m : u.Weight / 1000, //重量 WayBillFee = u.WayBillFee, //郵資 ProcessingFee = u.ProcessingFee, //郵政郵件處理費 InComeWayBillFee = u.ExpressFee, //客戶運費 InComeOprateFee = u.OperateFee, //客戶操做費 WayBillMargins = u.WayBillProfit, //運費毛利 TotalMargins = u.ExpressFee + u.OperateFee + u.InComeOtherFee - (u.WayBillFee + u.ProcessingFee + u.CostOtherFee), //總毛利 Margin = Math.Round((u.ExpressFee + u.OperateFee + u.InComeOtherFee == 0 ? 0m : (u.ExpressFee + u.OperateFee + u.InComeOtherFee -
(u.WayBillFee + u.ProcessingFee + u.CostOtherFee)) / (u.ExpressFee + u.OperateFee + u.InComeOtherFee) * 100), 2) + "%",
//毛利率 毛利率=(總收入-總的支出的成本)/總收入*100% ReconcileDate = u.ReconcileDate.ToStringDate(), //對帳日期 CostOtherFee = u.CostOtherFee, //成本 其餘費用 CostTotalFee = u.WayBillFee + u.ProcessingFee + u.CostOtherFee, //成本 總費用 CostStatus = u.CostStatus.ToChinese(), //成本 狀態 InComeOtherFee = u.InComeOtherFee, //收入 其餘費用 InComeTotalFee = u.ExpressFee + u.OperateFee + u.InComeOtherFee, //收入 總費用 InComeStatus = u.InComeStatus.ToChinese(), //收入 狀態 ExceptionMsg = u.ExceptionMsg, //運單異常緣由 WayBillCostID = u.WayBillCostID //運單成本ID // ExceptionType = u.ExceptionType //運單異常狀態 }); //decimal totalProfit = 0m; //總毛利求和 //構形成Json的格式傳遞 var result = new { iTotalRecords = DataSource.Count, iTotalDisplayRecords = DataSource.RecordTotal, data = data }; return Json(result, JsonRequestBehavior.AllowGet); }
在View中,設置datatables的屬性瀏覽器
bServerSide: true, //指定從服務器端獲取數據
//跟數組下標同樣,第一列從0開始,這裏表格初始化時,第四列默認降序
order: [[2, "desc"]],
當點擊排序的時候,咱們能夠打開火狐瀏覽器的Firebug查看下數據服務器
這個第一列是排序的字段的列索引,第二個字段標識有一個排序字段,由於這個控件是支持多列排序的。框架
DataTablesRequest類,裏面我封裝了對這些請求的處理。關於這個類的具體代碼能夠參見ASP.NET MVC搭建項目後臺UI框架—七、統計報表異步
string strSortField = dicSort.Where(x => x.Key == parm.SortColumns[0].Index).Select(x => x.Value).FirstOrDefault();
string strSortDire = parm.SortColumns[0].Direction == SortDirection.Asc ? "asc" : "desc";ide