jquery easyui datagrid data format

Bertha 。 2022-08-24 11:47 273阅读 0赞



概要

  1. jQuery EasyUI是一个基于jquery的集成了各种用户界面的框架,使用easyui我们不需要写太多javascript代码,一般情况下我们只需要使用一些html标记来定义用户接口,easyui将我们需要的功能都已经封装好了,我们只要来调用easyui给我们提供的接口就可以了,今天我们来学习下easyui中的DataGrid

数据显示

  1. 我们可以在easyui-datagrid中显示数据,显示数据从大类上我们也分为两种,一种是datagrid显示的数据不是来自于数据库,另一种就是将数据库的内容进行显示.

第一种

  1. 这一种是直接将写好的内容放到前台界面上写,所以相对来说比较简单,我们来看下前台代码的书写.

[html] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. <**table** id=”dg”title=”定性指标基础信息” class=”easyui-datagrid” style=”width:900px; height: 400px; padding-left: 200px;” url=”get_users.php”toolbar=”#toolbar” pagination=”true”
  2. rownumbers=”true” fitcolumns=”true”singleselect=”true”>
  3. <**thead**>
  4. //设置列名
  5. <**tr**>
  6. <**thfieldthfield=”firstname” width=”50”>定性指标级别</th>**
  7. <thfieldthfield=”lastname” width=”50”>分数</**th**>
  8. </**tr**>
  9. </**thead**>
  10. //数据填充
  11. <**tbody**>
  12. <**tr**>
  13. <**td**>优秀</**td**>
  14. <**td**>100</**td**>
  15. </**tr**>
  16. <**tr**>
  17. <**td**>良好</**td**>
  18. <**td**>80</**td**>
  19. </**tr**>
  20. <**tr**>
  21. <**td**>一般</**td**>
  22. <**td**>60</**td**>
  23. </**tr**>
  24. <**tr**>
  25. <**td**>较差</**td**>
  26. <**td**>50</**td**>
  27. </**tr**>
  28. </**tbody**>
  29. </**table**>
  30. //工具栏按钮
  31. <**dividdivid=”toolbar”>**
  32. <**ahrefahref=”javascript:void(0)” class=”easyui-linkbutton”iconcls=”icon-add” plain=”true”onclick=”newUser()”>添加</a>**
  33. <**ahrefahref=”javascript:void(0)” class=”easyui-linkbutton”iconcls=”icon-edit” plain=”true”onclick=”editUser()”>修改</a>**
  34. <ahrefahref=”javascript:void(0)” class=”easyui-linkbutton”iconcls=”icon-remove” plain=”true”onclick=”destroyUser()”>删除</**a**>
  35. </**div**>

下面这是效果图

Center

第二种

  1. 这种是将我们数据库中的内容显示到前台,我们经过一般处理程序从数据库中将数据提取出来,然后将数据显示到DataGrid中。
  2. 在这里需要注意的是,我们从数据库中取出来的内容是表格形式的datasetdatatable,而前台DataGrid接收的数据是固定格式的Json格式数据,所以我们需要将表格形式的数据转化为DataGrid需要的固定格式的Json格式数据。

[html] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. {“total”:28,”rows”:[
  2. {“产品ID”:”FI-SW-01”,”产品名称”:”Koi”,”价格”:10.00,”状态”:”P”},
  3. {“产品ID”:”K9-DL-01”,”产品名称”:”Dalmation”,”价格”:12.00,”状态””:”P”},
  4. {“产品ID”:”RP-SN-01”,”产品名称”:”Rattlesnake”,”价格”:12.00,”状态””:”P”},
  5. {“产品ID”:”RP-SN-01”,”产品名称”:”Rattlesnake”,”价格”:12.00,”状态””:”P”},
  6. {“产品ID”:”RP-LI-02”,”产品名称”:”Iguana”,”价格”:12.00,”状态””:”P”},
  7. {“产品ID”:”FL-DSH-01”,”产品名称”:”Manx”,”价格”:12.00,”状态””:”P”},
  8. ]}

下面我们来看下怎么样将数据库中的数据显示到前台的具体过程,

首先是前台页面的书写:

[html] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. <**div id=”tt”class=”easyui-tabs” style=”width: 1100px; height: 530px;margin-top: 15px”>**
  2. <**div title=”已录入单位”style=”padding: 10px”>**
  3. <**table** id=”HaveInput”title=”原始数据录入情况” class=”easyui-datagrid” style=”width:1050px; height: 480px; padding-left: 200px;”data-options=”rownumbers:true,url:’InputScoresDetailsDeveloped.ashx/ProcessRequest’,pageSize:5,pageList:[5,10,15,20],method:’get’,toolbar:’#tb’,”
  4. toolbar=”#bar”pagination=”true” rownumbers=”true”fitcolumns=”true” striped=”true”singleselect=”true”>
  5. <**thead**>
  6. <%—设置绑定表格的列名,列名与数据库相同—%>
  7. <**tr**>
  8. <**thdata-optionsthdata-options=”field:’DepartmentName’,width:100”>单位名称</th>**
  9. <thdata-optionsthdata-options=”field:’QuantyOriginalData’,width:100”>定量原始数据</**th**>
  10. <**thdata-optionsthdata-options=”field:’QualityOriginalData’,width:100”>定性原始数据</th>**
  11. <**thdata-optionsthdata-options=”field:’Remarks’, width:175,align:’right’”>备注</th>**
  12. <thdata-optionsthdata-options=”field:’YearTime’, width:85,align:’right’”>年份</**th**>
  13. </**tr**>
  14. </**thead**>
  15. </**table**>
  16. <**div id=”bar”>**
  17. <**ahrefahref=”javascript:void(0)” class=”easyui-linkbutton”iconcls=”icon-edit” plain=”true”onclick=”editUser()”>修改数据</a>**
  18. </**div**>
  19. </**div**>

表格属性里的的URL就是与一般处理程序交互用的。。

下面我们来看一般处理程序的书写,一般处理程序要首先接收由前台传过来的信息。

[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. public void ProcessRequest(HttpContextcontext)
  2. {
  3. string command =context.Request.QueryString[“test”];//前台传的标示值
  4. if (command == “add”)
  5. { //调用添加方法
  6. Add(context);
  7. }
  8. else if (command ==”modify”)
  9. { //调用修改方法
  10. Modify(context);
  11. }
  12. else
  13. { //调用查询方法
  14. Query(context);
  15. }
  16. }

然后的话才是我们从一般处理程序来向数据库获取要显示的数据库数据

[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. public void Query(HttpContext context)
  2. {
  3. //调用B层的方法从而获取数据库的Dataset
  4. DataSet ds =developmentRecordBLL.GetInputDevelopmentInfo();
  5. //将Dataset转化为Datable
  6. DataTable dt = ds.Tables[0];
  7. int count = dt.Rows.Count;
  8. string strJson =ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据
  9. context.Response.Write(strJson);//返回给前台页面
  10. context.Response.End();
  11. }

然后我们来看下DataSet数据转化为Json数据的方法

[csharp] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. public class ToJson
  2. {
  3. #region DataSet转换成Json格式
  4. ///
  5. /// DataSet转换成Json格式
  6. ///
  7. /// DataSet
  8. ///
  9. public static stringDataset2Json(DataSet ds, int total = -1)
  10. {
  11. StringBuilder json = newStringBuilder();
  12. foreach (DataTable dt in ds.Tables)
  13. {
  14. //{“total”:5,”rows”:[
  15. json.Append(“{\“total\“:”);
  16. if (total == -1)
  17. {
  18. json.Append(dt.Rows.Count);
  19. }
  20. else
  21. {
  22. json.Append(total);
  23. }
  24. json.Append(“,\“rows\“:[“);
  25. json.Append(DataTable2Json(dt));
  26. json.Append(“]}“);
  27. } return json.ToString();
  28. }
  29. #endregion
  30. #region dataTable转换成Json格式
  31. ///
  32. /// dataTable转换成Json格式
  33. ///
  34. ///
  35. ///
  36. public static stringDataTable2Json(DataTable dt)
  37. {
  38. StringBuilder jsonBuilder = newStringBuilder();
  39. for (int i = 0; i <dt.Rows.Count; i++)
  40. {
  41. jsonBuilder.Append(“{“);
  42. for (int j = 0; j <dt.Columns.Count; j++)
  43. {
  44. jsonBuilder.Append(“\“”);
  45. jsonBuilder.Append(dt.Columns[j].ColumnName);
  46. jsonBuilder.Append(“\“:\“”);
  47. jsonBuilder.Append(dt.Rows[i][j].ToString());
  48. jsonBuilder.Append(“\“,”);
  49. }
  50. if (dt.Columns.Count > 0)
  51. {
  52. jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
  53. }
  54. jsonBuilder.Append(“},”);
  55. }
  56. if (dt.Rows.Count > 0)
  57. {
  58. jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
  59. }
  60. return jsonBuilder.ToString();
  61. }
  62. #endregion dataTable转换成Json格式
  63. }

到这里我们就可以成功的将数据库中的数据显示到前台的DataGrid了。

下面我们来看下效果

Center 1

到这里我们就可以将数据库中的数据在前台显示了。

总结

  1. easyuidatagrid的使用还是很重要的,在这里我们先简要的介绍下,使datagrid可以简单的显示一些数据,关于datagrid的进一步的知识我们在下篇博客中将继续进行介绍。

发表评论

表情:
评论列表 (有 0 条评论,273人围观)

还没有评论,来说两句吧...

相关阅读