zoukankan      html  css  js  c++  java
  • ASP.NET MVC easyUI-datagrid 分页

    本文写的是最简单的 按照API文档来写的分页。就是插件自带的分页效果。

    一、html代码:field就是代表你后台数据的对应的列名。

     1  <table id="dg" class="easyui-datagrid" style=" 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', 120,align:'center'" >名称</th>                       
     6                         <th data-options="field:'DeviceUnitName', 80,align:'center'">单位</th>
     7                         <th data-options="field:'MakePlace', 120,align:'center'">产地</th>
     8                         <th data-options="field:'BuyTime', 120,align:'center'">购置日期</th>
     9 
    10                     </tr>
    11                 </thead>              
    12             </table>
    View Code

    二丶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             });
    View Code

    三丶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         }
    View Code

    四、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         }
    View Code

    五、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("
    ", "").Replace("
    ", "");
    33             return jsonBuilder.ToString();
    34         }
    View Code
  • 相关阅读:
    Team Foundation Server 2010完整装机过程
    Horovod介绍
    学习笔记 【Min_25 筛】
    Educational Codeforces Round 117
    C#打造秒杀腾讯的仿QQ界面,从此独孤求败
    C# ListView控件的间隔色和自动适应宽度
    jQuery1.6.1源码分析系列
    优雅框架授权验证页面
    多个DataTable的合并成一个新表
    教你自定义绘制TreeView
  • 原文地址:https://www.cnblogs.com/ElvisZhongShao/p/7357784.html
Copyright © 2011-2022 走看看