EasyUI算是比较有名的,搜一下网上的资料也比较多,具体的参数,下载地址我就不写了

平常也不怎么写文章,大部分都是代码,有不能运行的可以直接评论回复

有可能遇到的问题:

json数据格式,这个要仔细对下,有错误的话无法正常显示数据

分页的原理:

向后台传递页数、和每页显示的条数,然后用sql语句取出对应的数据,转换为json字符串传到前台

首先,页面视图代码

  1. <script type="text/javascript" src="~/Scripts/jquery-1.8.2.min.js"></script>
  2. <script type="text/javascript" src="~/Scripts/jquery.easyui.min.js"></script>
  3.  
  4. <link rel="stylesheet" href="~/Scripts/easyui.css" type="text/css" />
  5. <link rel="stylesheet" href="~/Scripts/icon.css" type="text/css" />
  6.  
  7. <table id="test" class="easyui-datagrid" title="Basic DataGrid" style="width:700px;height:250px"
  8. >
  9. <thead>
  10. <tr>
  11.  
  12. <th data-options="field:'GameID',width:100">Product</th>
  13. <th data-options="field:'GameName',width:80,align:'right'">List Price</th>
  14. <th data-options="field:'CompanyName',width:80,align:'right'">Unit Cost</th>
  15.  
  16. </tr>
  17. </thead>
  18. </table>
  19.  
  20. <script>
  21. $('#test').datagrid({
  22. url: 'GetGridData',
  23. pagination: true,//分页控件
  24. rownumbers: true,//行号
  25. frozenColumns: [[
  26. { field: 'ck', checkbox: true }
  27. ]],
  28. toolbar: [{
  29. text: '添加',
  30. iconCls: 'icon-add',
  31. handler: function () {
  32. openDialog("add_dialog", "add");
  33. }
  34. }, '-', {
  35. text: '修改',
  36. iconCls: 'icon-edit',
  37. handler: function () {
  38. openDialog("add_dialog", "edit");
  39. }
  40. }, '-', {
  41. text: '删除',
  42. iconCls: 'icon-remove',
  43. handler: function () {
  44. delAppInfo();
  45. }
  46. }],
  47. pageSize: 10, //设置默认分页大小
  48. pageList: [10, 15, 20, 25, 30, 35, 40, 45, 50], //设置分页大小
  49. });
  50.  
  51.  
  52. var p = $('#test').datagrid('getPager');
  53. $(p).pagination({
  54. pageSize: 10,//每页显示的记录条数,默认为10
  55. pageList: [5, 10, 15],//可以设置每页记录条数的列表
  56. beforePageText: '第',//页数文本框前显示的汉字
  57. afterPageText: '页 共 11 页',
  58. displayMsg: '当前显示 1 - 11 条记录 共 11 条记录',
  59. onBeforeRefresh:function(){
  60. $(this).pagination('loading');
  61. alert('before refresh');
  62. $(this).pagination('loaded');
  63. },
  64. onChangePageSize: function () {
  65. alert("pagesized changed");
  66. }
  67. });
  68.  
  69. </script>

//分页sql

  1. //datagrid分页数据格式,sortname为按照哪一个字段排序,
  2. //sortorder为desc、asc,startRecord为第几页,maxRecords为每页显示多少条
  3. public DataTable GetDataSetBySQL(string sql, string sortname, string sortorder, int startRecord, int maxRecords)
  4. {
  5.  
  6. DataSet ds = new DataSet();
  7.  
  8. int staRecord = (startRecord - ) * maxRecords + ;
  9. int endRecord = (startRecord) * maxRecords + ;
  10.  
  11. sql = "SELECT * FROM ( SELECT __tmp.*,ROW_NUMBER() OVER (ORDER BY __tmp." + sortname + " " + sortorder + ") AS RowNo FROM (" + sql;
  12. sql += ") AS __tmp ) AS __tmplist WHERE RowNo>=" + staRecord + " and RowNo<" + endRecord + " ORDER BY RowNo ";
  13.  
  14. DataTable dt=db.GetDataSet(sql,"d1");
  15.  
  16. return dt;
  17. }

接下来是把datatable转换成datagrid需要的json数据格式

  1. //datagrid需要的json数据格式,count为sql总的条数
  2. public string DataTableToJson(DataTable dt,int count)
  3. {
  4. StringBuilder jsonBuilder = new StringBuilder();
  5. jsonBuilder.Append("{\"");
  6. jsonBuilder.Append("total\":");
  7. jsonBuilder.Append(count);
  8. jsonBuilder.Append(",");
  9. jsonBuilder.Append("\"rows\"");
  10. jsonBuilder.Append(":[");
  11.  
  12. for (int i = ; i < dt.Rows.Count; i++)
  13. {
  14. jsonBuilder.Append("{");
  15. for (int j = ; j < dt.Columns.Count; j++)
  16. {
  17. jsonBuilder.Append("\"");
  18. jsonBuilder.Append(dt.Columns[j].ColumnName);
  19. jsonBuilder.Append("\":");
  20.  
  21. jsonBuilder.Append("\"");
  22. jsonBuilder.Append(dt.Rows[i][j].ToString());
  23. jsonBuilder.Append("\"");
  24.  
  25. jsonBuilder.Append(",");
  26. //jsonBuilder.Append("\",");
  27. }
  28. jsonBuilder.Remove(jsonBuilder.Length - , );
  29. jsonBuilder.Append("},");
  30. }
  31. jsonBuilder.Remove(jsonBuilder.Length - , );
  32. jsonBuilder.Append("]");
  33. jsonBuilder.Append("}");
  34. return jsonBuilder.ToString();
  35. }

最后是获取数据的url代码

  1. //rows为每页多少条数据,page为当前页数,datagrid空间post传过来的,直接接收即可
  2. //一个两种方法获取page和rows
  3. public string GetGridData(string rows,string page)
  4. {
  5. int pageSize = Convert.ToInt32(Request["rows"]);
  6. int pageNum = Convert.ToInt32(Request["page"]);
  7. //result为json数据格式测试
  8. string result = "{\"total\":11,\"rows\":[{\"productid\":\"FI-SW-01\",\"productname\":\"Koi\",\"unitcost\":10.00,\"status\":\"P\",\"listprice\":36.50,\"attr1\":\"Large\",\"itemid\":\"EST-1\"}";
  9. result += ",{\"productid\":\"AV-CB-01\",\"productname\":\"Amazon Parrot\",\"unitcost\":92.00,\"status\":\"P\",\"listprice\":63.50,\"attr1\":\"Adult Male\",\"itemid\":\"EST-18\"}";
  10. result += ",{\"productid\":\"AV-CB-01\",\"productname\":\"Amazon Parrot\",\"unitcost\":92.00,\"status\":\"P\",\"listprice\":63.50,\"attr1\":\"Adult Male\",\"itemid\":\"EST-18\"}";
  11. result += ",{\"productid\":\"AV-CB-01\",\"productname\":\"Amazon Parrot\",\"unitcost\":92.00,\"status\":\"P\",\"listprice\":63.50,\"attr1\":\"Adult Male\",\"itemid\":\"EST-18\"}";
  12. result += ",{\"productid\":\"AV-CB-01\",\"productname\":\"Amazon Parrot\",\"unitcost\":92.00,\"status\":\"P\",\"listprice\":63.50,\"attr1\":\"Adult Male\",\"itemid\":\"EST-18\"}";
  13. result += ",{\"productid\":\"AV-CB-01\",\"productname\":\"Amazon Parrot\",\"unitcost\":92.00,\"status\":\"P\",\"listprice\":63.50,\"attr1\":\"Adult Male\",\"itemid\":\"EST-18\"}";
  14. result += ",{\"productid\":\"AV-CB-01\",\"productname\":\"Amazon Parrot\",\"unitcost\":92.00,\"status\":\"P\",\"listprice\":63.50,\"attr1\":\"Adult Male\",\"itemid\":\"EST-18\"}";
  15. result += ",{\"productid\":\"AV-CB-01\",\"productname\":\"Amazon Parrot\",\"unitcost\":92.00,\"status\":\"P\",\"listprice\":63.50,\"attr1\":\"Adult Male\",\"itemid\":\"EST-18\"}";
  16. result += ",{\"productid\":\"AV-CB-01\",\"productname\":\"Amazon Parrot\",\"unitcost\":92.00,\"status\":\"P\",\"listprice\":63.50,\"attr1\":\"Adult Male\",\"itemid\":\"EST-18\"}";
  17. result += ",{\"productid\":\"AV-CB-01\",\"productname\":\"Amazon Parrot\",\"unitcost\":92.00,\"status\":\"P\",\"listprice\":63.50,\"attr1\":\"Adult Male\",\"itemid\":\"EST-18\"}";
  18. result += ",{\"productid\":\"AV-CB-01\",\"productname\":\"Amazon Parrot\",\"unitcost\":92.00,\"status\":\"P\",\"listprice\":63.50,\"attr1\":\"Adult Male\",\"itemid\":\"EST-18\"}";
  19. result += ",{\"productid\":\"AV-CB-01\",\"productname\":\"Amazon Parrot\",\"unitcost\":92.00,\"status\":\"P\",\"listprice\":63.50,\"attr1\":\"Adult Male\",\"itemid\":\"EST-18\"}]}";
  20. //return Json(result,JsonRequestBehavior.AllowGet);
  21.  
  22. string sql = "select a.GameID,a.GameName,b.CompanyName from GameProfile a LEFT JOIN CompanyProfile b ON a.CompanyId=b.CompanyId";
  23. string sqlcount = " select count(*) from GameProfile ";
  24. DataTable dtrows = db.GetDataSet(sqlcount, "count");
  25. int record = Convert.ToInt32(dtrows.Rows[][].ToString());
  26. DataTable dt = GetDataSetBySQL(sql, "gameid", "desc",Convert.ToInt32(page),Convert.ToInt32(rows));
  27. string result2 = DataTableToJson(dt,record);
  28. return result2;
  29. }

EasyUI DataGrid 使用(分页,url数据获取,data转json)的更多相关文章

  1. Asp.Net MVC EasyUI DataGrid查询分页

    function doSearch() { //查询方法 var searchValue = $('#txtQueryTC001').textbox('getText'); $('#dgCMSTC') ...

  2. Easyui datagrid 修改分页组件的分页提示信息为中文

    datagrid 修改分页组件的分页提示信息为中文 by:授客 QQ:1033553122 测试环境 jquery-easyui-1.5.3 问题描述 默认分页组件为英文展示,如下,希望改成中文展示 ...

  3. easyui datagrid 加载静态文件中的json数据

    本文主要介绍easyui datagrid 怎么加载静态文件里的json数据,开发环境vs2012, 一.json文件所处的位置 二.json文件内容 {"total":28,&q ...

  4. EasyUi datagrid 表格分页例子

    1.首先引入 easyui的 css 和 js 文件 2.前台 需要写的js //源数据 function Async(action,args,callback){  $.ajax({  url: a ...

  5. easyui datagrid关于分页的问题

    easyui框架中datagrid可以很好的来展示大量的列表数组,但是由于datagrid一般都是从控件本身传递一个页码给后台,后台进行处理. 但是,最近项目跟webgis有关,数据查询直接是从服务中 ...

  6. easyUI datagrid 重复发送URL请求

    如果在table属性中配置了URL参数,在初始化datagrid时,会发送一次url请求.或者在js中datagrid{url:''}时,也会自动发送一次url请求. 在初始化datagrid时,我并 ...

  7. easyui datagrid 的分页刷新按钮

    datagrid  刷新bug: 情形: 当用户A,B  同时操作 datagrid时(记录1,记录2.记录3).如果A如果删除记录1,  B此时已选中了记录1 ,记录2 , 这时B点击分页中的刷新按 ...

  8. JQuery EasyUI datagrid pageNumber 分页 请求/加载 两次

    解决方案: 原因是 jquery.easyui.min.js 源文件中,由于第1页的total和其他页的total不相等,EasyUI会重新发起第1页的请求!1.jQuery EasyUI 1.4.1 ...

  9. EasyUI DataGrid 获得分页信息

    var b = $('#SBDiv_1_DateGrid').datagrid('options'); console.info(b); 具体需要哪些字段,可以通过火狐debug,然后自己找需要的信息 ...

  10. JAVAEE——BOS物流项目04:学习计划、datagrid、分页查询、批量删除、修改功能

    1 学习计划 1.datagrid使用方法(重要) n 将静态HTML渲染为datagrid样式 n 发送ajax请求获取json数据创建datagrid n 使用easyUI提供的API创建data ...

随机推荐

  1. java构造函数是否可继承,以及子类构造函数可否不使用super调用超类构造函数

    问题一:java的构造函数能否被继承? 笔者初学java看的一本书说:“java的子类自然的继承其超类的“非private成员”. 通常java的构造函数被设置为public的(若你不写构造函数,ja ...

  2. C细节学习

    字符串ascii码值比较compress函数;

  3. ES6系列_2之新的声明方式

    在ES5中我们在声明时只有一种方法,就是使用var来进行声明,ES6对声明的进行了扩展,现在可以有三种声明方式. (1)var:它是variable的简写,可以理解成变量的意思. (2)let:它在英 ...

  4. Rhythmk 一步一步学 JAVA (19): 注解 annotation

    在编写注解的时候需要了解的四种注解: @Target 表示该注解可以用于什么地方,可能的ElementType参数有: CONSTRUCTOR:构造器的声明 FIELD:域声明(包括enum实例) L ...

  5. 实用 Linux 命令行使用技巧集锦

    最近在Quora上看到一个问答题目,关于在高效率Linux用户节省时间Tips.将该题目的回答进行学习总结,加上自己的一些经验,记录如下,方便自己和大家参考. 下面介绍的都是一些命令行工具,这些工具在 ...

  6. 埃氏筛法求素数&构造素数表求素数

    埃氏筛法求素数和构造素数表求素数是一个道理. 首先,列出从2开始的所有自然数,构造一个序列: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1 ...

  7. Angular: Can't bind to 'ngModel' since it isn't a known property of 'input'问题解决

    https://blog.csdn.net/h363659487/article/details/78619225 最初使用 [(ngModel)] 做双向绑定时,如果遇见Angular: Can't ...

  8. js跳出for循环

    1 使用普通的for循环 注意foreach使用return或break都无法跳出循环 2 使用every跳出循环, every 方法会遍历数组中的所有元素来判断是否满足条件,如果有一个元素返回fal ...

  9. webHttpBinding、basicHttpBinding和wsHttpBinding区别

    webHttpBinding is the REST-style binding, where you basically just hit a URL and get back a truckloa ...

  10. linux下,MySQL默认的数据文档存储目录为/var/lib/mysql。

    0.说明 Linux下更改yum默认安装的mysql路径datadir. linux下,MySQL默认的数据文档存储目录为/var/lib/mysql. 假如要把MySQL目录移到/home/data ...