这几天一直在学习基于MVC的JQGrid. 记得刚毕业时候做web最头疼的就是GridView,各种分页查询删除,后来学习了Ajax,使用的jqury UI框架ligerui给公司做ERP系统,再后来看到前辈用的JQgrid,决定拿来学习下,并且想跟以前用过的技术做一个对比。

JQGrid很好上手,但是里面有很多规则记起来比较头疼。

1.下载文件
  1. 下载jqGrid的软件包,目前最新版本为4.4.1 下载地址为:http://www.trirand.com/blog/?page_id=6
  2. 下载jQuery文件,目前最新版本为1.8.2 下载地址为:http://code.jquery.com/jquery-1.8.2.min.js
  3. 下载jqGrid皮肤,下载地址为:http://jqueryui.com/themeroller/ 我使用的是:ThemeRoller->gallery->cupertino样式

2.referance

和其他的jquery框架一样,首先需要引用jquery类库,jquery.jqgrid类库即可。

<scriptsrc="~/Scripts/jquery-1.7.1.min.js"type="text/javascript"></script>

<scriptsrc="~/Scripts/jquery-ui-1.8.20.min.js"type="text/javascript"></script>

<scriptsrc="~/jqGrid/js/jquery.jqGrid.min.js"type="text/javascript"></script>

基本上到这里我们就可以简单做一个jqgrid,我模仿着demos做了一个。http://www.trirand.com/blog/jqgrid/jqgrid.html

  1. $(this).ready(function () {
  2. jQuery("#list1").jqGrid({
  3. url: "/Home/SearchResult",
  4. datatype: "json",
  5. colNames: ['Inv No', 'Date', 'Client', 'Amount', 'Tax', 'Total', 'Notes'],
  6. colModel: [
  7. { name: 'InvNo', index: 'InvNo', width: 55 },
  8. { name: 'InvDate', index: 'InvDate', width: 90 },
  9. { name: 'Client', index: 'Client', width: 100 },
  10. { name: 'Amount', index: 'Amount', width: 80, align: "right" },
  11. { name: 'Tax', index: 'Tax', formatter: 'currency', width: 80, align: "right" },
  12. { name: 'Total', index: 'Total', width: 80, align: "right" },
  13. { name: 'Note', index: 'Note', width: 150, sortable: false }
  14. ],
  15. rowNum: 10,
  16. rowList: [10, 20, 30],
  17. pager: '#pager1',
  18. pgbuttons:true,
  19. sortname: 'id',
  20. mtype: "post",
  21. viewrecords: true,
  22. sortorder: 'desc',
  23. emptyrecords: "NO records",
  24. multiselect: true,
  25. rownumbers: true,
  26. caption: "JSON 实例"
  27. });
  28. jQuery("#list1").jqGrid('navGrid', '#pager1', { edit: true, add: true, del: true });

URL:获取数据的地址

datatype:从服务器端返回的数据类型,默认xml。可选类型:xml,local,json,jsonnp,script,xmlstring,jsonstring,clientside

colNames:列显示名字,数组

colModel:常用到的属性:name 列显示的名称;index 传到服务器端用来排序用的列名称;width 列宽度;align 对齐方式;sortable 是否可以排序

rowNum:默认每页显示的条数

rowList:下拉列表,可以改变每页显示的条数

pager:html元素,分页栏放置的位置。(如div的id)

sortname:默认排序的列

mtype:ajax提交方式。POST或者GET,默认GET

viewrecords:显示总的记录数(bool)

sortorder:排序方式

emptyrecords:没有记录时候显示的文字

multiselect:是否可以多选

rownumbers:如果为ture则会在表格左边新增一列,显示行顺序号,从1开始递增。此列名为'rn'.

caption:表格的标题

更多属性参考:http://blog.mn886.net/jqGrid/

Json格式的呈现:

刚开始有点想不通jqGrid到底接受什么样格式的Json呢。

后来查到有一个重要的选项jsonReader

jsonReader:默认的jsonReader设置:

  1. jsonReader : {
  2. root: "rows", // json中代表实际模型数据的入口
  3. page: "page", // json中代表当前页码的数据
  4. total: "total", // json中代表页码总数的数据
  5. records: "records", // json中代表数据行总数的数据
  6. repeatitems: true, // 如果设为false,则jqGrid在解析json时,会根据name来搜索对应的数据元素(即可以json中元素可以不按顺序);而所使用的name是来自于colModel中的name设定。
  7. cell: "cell",
  8. id: "id",
  9. userdata: "userdata"}

通常jsonReader和repeatitems是配合使用的,如果repeatitems为false,json中数据可以乱序,jqGrid会根据colModel中name属性和json数据对应,根据属性名解析。

repeatitems为true,json格式

  1. {
  2. "page": "xxx",
  3. "total": "yyy",
  4. "records": "zzz",
  5. "rows" : [
  6. { "cell" :["cell11", "cell12", "cell13"]}, // cell中不需要各列的name,但是需要与colModel一一对应
  7. {"cell" :["cell21", "cell22", "cell23"]},
  8. ... ]
  9. }

注意大小写,期间把cell写成Cell一直没有出来数据

repeatitems为false,json格式

  1. {
  2. "page" : "xxx",
  3. "total" : "yyy",
  4. "records" : "zzz",
  5. "rows" : [ {"invid" : "1","invdate":"cell11", "amount" :"cell12", "tax" :"cell13", "total" :"1234", "note" :"somenote"}, // 数据中需要各列的name,但是可以不按列的顺序 {"invid" : "2","invdate":"cell21", "amount" :"cell22", "tax" :"cell23", "total" :"2345", "note" :"some note"}, ... ]
  6. }

这里有一个自动生成json格式的泛型类:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5.  
  6. namespace MVC4.BusinessLogic
  7. {
  8. public class JQGrid
  9. {
  10. private class JsonRow
  11. {
  12. public object[] cell { get; set; }
  13. public JsonRow(int Columns)
  14. {
  15. cell = new object[Columns];
  16. }
  17. public void SetJsonCol(object data, int colnum)
  18. {
  19. cell[colnum] = data;
  20. }
  21. }
  22. public object JsonRowModel { get; set; }
  23. public JQGrid(IEnumerable<object> gridData, int columnTotal, int currentPage, int totalRecords, int totalPages)
  24. {
  25. List<JsonRow> jsonRowData = new List<JsonRow>();
  26. for (var d = 0; d < totalRecords; d++)
  27. {
  28. var data = gridData.ToList()[d];
  29. Type t = data.GetType();
  30. var pops = t.GetProperties();
  31. var row = pops.Select(p => p.GetValue(data, null)).ToArray();
  32. JsonRow jsonRow = new JsonRow(columnTotal);
  33. for (int c = 0; c < columnTotal; c++)
  34. {
  35. jsonRow.SetJsonCol(row[c], c);
  36. }
  37. jsonRowData.Add(jsonRow);
  38. }
  39. JsonRowModel = new { page = currentPage,total = totalPages, records = totalRecords, rows = jsonRowData.ToArray() };
  40. }
  41. }
  42. }

JQGrid 学习1的更多相关文章

  1. Jqgrid学习API

    JQGrid是一个在jquery基础上做的一个表格控件,以ajax的方式和服务器端通信. JQGrid Demo 是一个在线的演示项目.在这里,可以知道jqgrid可以做什么事情. 下面是转自其他人b ...

  2. jqGrid 学习

    jqGrid 学习: 一.下载需要的jqGrid包:http://www.trirand.com/blog/?page_id=6 二.下载JQuery UI:http://jqueryui.com/d ...

  3. Jqgrid学习(转载)

    jqGrid API 全   JQGrid是一个在jquery基础上做的一个表格控件,以ajax的方式和服务器端通信. JQGrid Demo 是一个在线的演示项目.在这里,可以知道jqgrid可以做 ...

  4. jQgrid学习笔记

    jQgrid学习笔记

  5. jqgrid学习笔记(转载)

    jqgrid中文帮助文档网址:http://blog.mn886.net/jqGrid/ jqgrid:用来做什么? jqgrid是web端前台表格控件,用它可以轻松将数据格式化显示,前后台用过aja ...

  6. jqGrid 学习笔记--数据异步加载方法(转)

    var commonQuery = '../importantInfoReport/pageQueryImportantInfoReport.action?type=0'; jQuery(" ...

  7. jqGrid学习笔记(二)

    本节介绍jqGrid其他的使用方法,主要是一些基本操作,特殊的数据显示等. 1 刷新jqGrid数据. 常用到刷新jqGrid数据的情况是,在用到查询的时候,根据查询条件,请求数据,并刷新jqGrid ...

  8. jqGrid学习笔记(一)

    3.2.body中的代码 <!-- jqGrid table list4 --> <table id="list4"></table> < ...

  9. ASP.NET MVC and jqGrid 学习笔记 6-增删改操作

    程序结构: Member.cs CRUD.cshtml CRUD.js HomeController 一.Model public class Member { [Key] public int No ...

随机推荐

  1. 探索软件工程道路上的我 V (Θ∀Θ#)

    开发语言:Java 开发工具:UltraEdit 小伙伴博客:http://www.cnblogs.com/hyating/ github地址:https://github.com/JUNYU217/ ...

  2. RabbitMQ - TcpConnection析构引发的一次handshake_timeout

    使用RabbitMQ时,连接rabbit-server一直连接失败,代码没有任何错误提示.但是通过rabbitmqctl始终查询不到连接以及创建的queue等信息. 官方的文件demo里面也没有Tcp ...

  3. 从零开始学习Node.js例子九 设置HTTP头

    server.js //basic server的配置文件 ; var server = require('./basicserver').createServer(); server.useFavI ...

  4. C# 字符编码解码 Encoder 和Decoder

    在网络传输和文件操作中,如果数据量很大,需要将其划分为较小的快,此时可能出现一个数据块的末尾是一个不匹配的高代理项,而与其匹配的低代理项在下一个数据块. 这时候使用Encoding的GetBytes方 ...

  5. LaTex表格内单元格内容强制换行

    /newcommand{/tabincell}[2]{/begin{tabular}{@{}#1@{}}#2/end{tabular}}%放在导言区 %然后使用&/tabincell{c}{} ...

  6. PowerDesigner自增列问题

  7. C++学习笔记32:泛型编程拓展1

    标准模板库的内容 标准模板类:复数.序偶 迭代器 标准容器:向量,表,栈,队列,集合,映射等 标准算法:查找,排序等 标准模板库型式的使用方法 "<>":模板名称< ...

  8. Python 中的虚拟环境

    检查系统是否安装了virtualenv: $ virtualenv --version 创建虚拟环境venv(名字可以随便取,一般为venv): $ virtualenv venv 使用虚拟环境ven ...

  9. IIC

    IIC多主从,双向传输,只有两根线:一根数据,一根时钟,时钟必须由主机发出控制.初始化时主机把SCL和SDA的电平都拉高,然后在SCL保持高电平时SDA拉低形成一个开始信号,紧接着开始信号就开始发送要 ...

  10. maven加载jar包配置

    maven build时报程序包不存在和找不到符号的错误,但是代码中不报错,如下: [ERROR] Failed to execute goal org.apache.maven.plugins:ma ...