在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
本文写的是最简单的 按照API文档来写的分页。就是插件自带的分页效果。 一、html代码:field就是代表你后台数据的对应的列名。 1 <table id="dg" class="easyui-datagrid" style="width: 100%; height: 400px;" data-options="nowrap:false"> 2 <thead> 3 <tr> 4 <th data-options="field:'DeviceId',checkbox:true"></th> 5 <th data-options="field:'DeviceName', width:120,align:'center'" >名称</th> 6 <th data-options="field:'DeviceUnitName', width:80,align:'center'">单位</th> 7 <th data-options="field:'MakePlace', width:120,align:'center'">产地</th> 8 <th data-options="field:'BuyTime', width:120,align:'center'">购置日期</th> 9 10 </tr> 11 </thead> 12 </table> 二丶JS代码:在js写easyUI-datagrid读取数据时候,要分页就加上 pagination: true,pageSize:10,pageList[10,20,30],表示grid要分页,每页10条,但可选10,20或者30条每页。 1 $(".easyui-datagrid").datagrid({ 2 rownumbers: true, 3 singleSelect: false, 4 fitColumns: false, 5 idField: 'DeviceId', 6 method: 'post', 7 url: '/Admin/Report/DeviceDetialListSearch', 8 remoteSort: false, 9 multiSort: false, 10 pagination: true, 11 pageSize: 10, 12 pageList: [10,20,30], 13 queryParams: { 14 'DeviceIdList':"", 15 'DeviceName':$("#DeviceName").combobox('getValue'), 16 17 }, 18 onLoadSuccess: function () { 19 //$("#dg").datagrid('clearChecked');//清除复选框 20 //$("#dg").datagrid('load'); 21 } 22 }); 三丶Controller后台读取数据 DeviceDetialListSearch()函数: Request["rows"]代表当前页面有多少行,Request["page"]代表当前页,这两个是easyUI-datagrid自带的只要用到分页,就会有这两个,不需要顾虑。recordCount代表搜索到的数据总数,这个会在Model里求出。并可在这里调用。 1 public ActionResult DeviceDetialListSearch() 2 { 3 FADeviceInfoModel deviceInfoModel = new FADeviceInfoModel(); 4 int pageSize = int.Parse(Request["rows"]); 5 int nowPage = int.Parse(Request["page"]); 6 int recordCount = 0; //搜索条件下的总记录数量 7 DataTable dtDeviceInfo = deviceInfoModel.SearchForDetail(pageSize, nowPage,out recordCount, Request["DeviceName"], Request["startTime"], Request["endTime"]); 8 return Content(MyJson.DataTableToJsonByPage(dtDeviceInfo, recordCount, "")); 9 } 四、Model里SearchForDetail()函数 1 public DataTable SearchForDetail(int pageSize, int nowPage, out int recordCount,string DeviceName, string startTime, string endTime) 2 { 3 string sqlCondition = " "; 4 if (startTime != null && !startTime.Equals("")) 5 sqlCondition += " and FADeviceInfo.ReleaseTime >= '" + startTime + " 00:00:01' "; 6 if (endTime != null && !endTime.Equals("")) 7 sqlCondition += " and FADeviceInfo.ReleaseTime <= '" + endTime + " 23:59:59' "; 8 if (DeviceName != null && !DeviceName.Equals("")) 9 sqlCondition += " and (FADeviceInfo.DeviceName like '%" + DeviceName + "%' or FADeviceInfo.DeviceInputCode like '%" + DeviceName + "%')"; 10 11 string sqlOrder = " order by DeviceId desc "; 12 string sqlResult = " DeviceId,DeviceStatus,DeviceCode,DeviceName,DepartmentName,DeviceSpec,DeviceUnitName,OriginalValue,MakePlace,BuyTime,FinancialCode "; 13 14 string sqlSon = "(select top " + (nowPage - 1) * pageSize + " DeviceId from FADeviceInfo " + sqlOn + " where 1 = 1 " + sqlCondition + sqlOrder + ")"; 15 string sql = " select top " + pageSize + sqlResult + " from FADeviceInfo " + sqlOn + " where DeviceId not in " + sqlSon + sqlCondition + sqlOrder; 16 string sqlCount = "select count(*) from FADeviceInfo " + sqlOn + " where 1 = 1 " + sqlCondition; 17 18 DataTable dataTable = new DataTable(); 19 dataTable = db.MyExecuteQuery(sql); 20 recordCount =db.GetCount(sqlCount); 21 return dataTable; 22 } 五、table转Json的函数DataTableToJsonByPage(),最下面添加Footer,如果你用不到页脚,就是统计总合计的,可以设置参数footer=“”,就OK了。 1 public static string DataTableToJsonByPage(DataTable dt, int total, string footer) 2 { 3 StringBuilder jsonBuilder = new StringBuilder(); 4 //添加表格总行数 5 jsonBuilder.Append("{\"total\":" + total + ",\"rows\":"); 6 //添加行数据 7 jsonBuilder.Append("["); 8 for (int i = 0; i < dt.Rows.Count; i++) 9 { 10 jsonBuilder.Append("{"); 11 for (int j = 0; j < dt.Columns.Count; j++) 12 { 13 jsonBuilder.Append("\""); 14 jsonBuilder.Append(dt.Columns[j].ColumnName); 15 jsonBuilder.Append("\":\""); 16 jsonBuilder.Append(dt.Rows[i][j].ToString()); 17 jsonBuilder.Append("\","); 18 } 19 jsonBuilder.Remove(jsonBuilder.Length - 1, 1); 20 jsonBuilder.Append("},"); 21 } 22 if (dt.Rows.Count != 0) 23 { 24 jsonBuilder.Remove(jsonBuilder.Length - 1, 1); 25 } 26 jsonBuilder.Append("]"); 27 //添加Footer 28 jsonBuilder.Append(",\"footer\":["); 29 jsonBuilder.Append(footer); 30 jsonBuilder.Append("]}"); 31 32 jsonBuilder = jsonBuilder.Replace("\n", "").Replace("\r", ""); 33 return jsonBuilder.ToString(); 34 }
|
请发表评论