民工皇帝

龙在沙滩被虾戏,虎落平阳被犬欺. 虎伏深山听风啸,龙卧浅滩等海潮. 海到尽头天做岸,山登绝顶我为峰. 如日东山能在起,大鹏展翅恨天低。 谁无虎落平阳日,待我东山再起时. 有朝一日龙得水,必令长江水倒流. 有朝一日凤回巢,必让长城永不倒. 有朝一日虎归山,必要血染半边天. 有朝一日狮入林,我要气吼山河震. 有朝一日游地府,我让地府底朝天. 有朝一日游天边,众神跪在我身边. 有朝一日凤翔天,我要天下尽我鸣. 有朝一日我出头,我要天下唯我尊. 天下英雄出我辈,一入江湖岁月摧. 宏图霸业谈笑中,不胜人生一场醉

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
jqGrid可支持的数据类型:xml、json、jsonp、local or clientSide、xmlstring、jsonstring
、script、function (…)。
Json数据
需要定义jsonReader来跟服务器端返回的数据做对应,其默认值:
Java代码 复制代码
  1. jQuery("#gridid").jqGrid({   
  2. ...   
  3.    jsonReader : {   
  4.      root: "rows",   
  5.      page: "page",   
  6.      total: "total",   
  7.      records: "records",   
  8.      repeatitems: true,   
  9.      cell: "cell",   
  10.      id: "id",   
  11.      userdata: "userdata",   
  12.      subgrid: {root:"rows",    
  13.         repeatitems: true,    
  14.        cell:"cell"  
  15.      }   
  16.    },   
  17. ...   
  18. });  

这样服务器端返回的数据格式:
Java代码 复制代码
  1. {    
  2.   total: "xxx",    
  3.   page: "yyy",    
  4.   records: "zzz",   
  5.   rows : [   
  6.     {id:"1", cell:["cell11""cell12""cell13"]},   
  7.     {id:"2", cell:["cell21""cell22""cell23"]},   
  8.       ...   
  9.   ]   
  10. }  


jsonReader的属性
total 总页数
page 当前页
records 查询出的记录数
rows 包含实际数据的数组
id 行id
cell 当前行的所有单元格


* root
  这个元素指明表格所需要的数据从哪里开始。
Java代码 复制代码
  1. jQuery("#gridid").jqGrid({   
  2. ...   
  3.    jsonReader : {root:"invdata"},   
  4. ...   
  5. });  

从服务器端返回数据格式为:
Java代码 复制代码
  1. {    
  2.   total: "xxx",    
  3.   page: "yyy",    
  4.   records: "zzz",   
  5.   invdata : [   
  6.     {id:"1", cell:["cell11""cell12""cell13"]},   
  7.     {id:"2", cell:["cell21""cell22""cell23"]},   
  8.       ...   
  9.   ]   
  10. }  


* page
* total
* records
定义翻页所需要的信息
Java代码 复制代码
  1. jQuery("#gridid").jqGrid({   
  2. ...   
  3.    jsonReader : {   
  4.       root:"invdata",   
  5.       page: "currpage",   
  6.       total: "totalpages",   
  7.       records: "totalrecords"  
  8.    },   
  9. ...   
  10. });  


服务器端返回数据:
Java代码 复制代码
  1. {    
  2.   totalpages: "xxx",    
  3.   currpage: "yyy",   
  4.   totalrecords: "zzz",   
  5.   invdata : [   
  6.     {id:"1", cell:["cell11""cell12""cell13"]},   
  7.     {id:"2", cell:["cell21""cell22""cell23"]},   
  8.       ...   
  9.   ]   
  10. }  


* cell
当前行所包含的单元格数据
Java代码 复制代码
  1. jQuery("#gridid").jqGrid({   
  2. ...   
  3.    jsonReader : {   
  4.       root:"invdata",   
  5.       page: "currpage",   
  6.       total: "totalpages",   
  7.       records: "totalrecords",   
  8.       cell: "invrow"  
  9.    },   
  10. ...   
  11. });  

从服务器端返回数据:
Java代码 复制代码
  1. {    
  2.   totalpages: "xxx",    
  3.   currpage: "yyy",   
  4.   totalrecords: "zzz",   
  5.   invdata : [   
  6.     {id:"1", invrow:["cell11""cell12""cell13"]},   
  7.     {id:"2", invrow:["cell21""cell22""cell23"]},   
  8.       ...   
  9.   ]   
  10. }  


* id
行id
Java代码 复制代码
  1. jQuery("#gridid").jqGrid({   
  2. ...   
  3.    jsonReader : {   
  4.       root:"invdata",   
  5.       page: "currpage",   
  6.       total: "totalpages",   
  7.       records: "totalrecords",   
  8.       cell: "invrow",   
  9.       id: "invid"  
  10.    },   
  11. ...   
  12. });  

从服务器端返回数据:
Java代码 复制代码
  1. {    
  2.   totalpages: "xxx",    
  3.   currpage: "yyy",   
  4.   totalrecords: "zzz",   
  5.   invdata : [   
  6.     {invid:"1", invrow:["cell11""cell12""cell13"]},   
  7.     {invid:"2", invrow:["cell21""cell22""cell23"]},   
  8.       ...   
  9.   ]   
  10. }  

cell 可以设置为空字符串,id也可以设置为数字:
Java代码 复制代码
  1. jQuery("#gridid").jqGrid({   
  2. ...   
  3.    jsonReader : {   
  4.       root:"invdata",   
  5.       page: "currpage",   
  6.       total: "totalpages",   
  7.       records: "totalrecords",   
  8.       cell: "",   
  9.       id: "0"  
  10.    },   
  11. ...   
  12. });  

从服务器端返回:
Java代码 复制代码
  1. {    
  2.   totalpages: "xxx",    
  3.   currpage: "yyy",   
  4.   totalrecords: "zzz",   
  5.   invdata : [   
  6.     {"1","cell11""cell12""cell13"},   
  7.     {"2",,"cell21""cell22""cell23"},   
  8.       ...   
  9.   ]   
  10. }  


* repeatitems
  指明每行的数据是可以重复的,如果设为false,则会从返回的数据中按名字来搜索元素,这个名字就是colModel中的名字:
Java代码 复制代码
  1. jQuery("#gridid").jqGrid({   
  2. ...   
  3.    jsonReader : {   
  4.       root:"invdata",   
  5.       page: "currpage",   
  6.       total: "totalpages",   
  7.       records: "totalrecords",   
  8.       repeatitems: false,   
  9.       id: "0"  
  10.    },   
  11. ...   
  12. });  

从服务器端返回数据:
Java代码 复制代码
  1. {    
  2.   totalpages: "xxx",    
  3.   currpage: "yyy",   
  4.   totalrecords: "zzz",   
  5.   invdata : [   
  6.     {invid:"1",invdate:"cell11", amount:"cell12", tax:"cell13", total:"1234", note:"somenote"},   
  7.     {invid:"2",invdate:"cell21", amount:"cell22", tax:"cell23", total:"2345", note:"some note"},   
  8.       ...   
  9.   ]   
  10. }  

此例中,id属性值为“invid”。
一旦当此属性设为false时,我们就不必把所有在colModel定义的name值都赋值。因为是按name来进行搜索元素的,所以他的排序也不是按colModel中指定的排序结果。
Java代码 复制代码
  1. {    
  2.   totalpages: "xxx",    
  3.   currpage: "yyy",   
  4.   totalrecords: "zzz",   
  5.   invdata : [   
  6.     {invid:"1",invdate:"cell11", note:"somenote"},   
  7.     {invid:"2", amount:"cell22", tax:"cell23", total:"2345"},   
  8.       ...   
  9.   ]   
  10. }  



用户数据(user data)
在某些情况下,我们需要从服务器端返回一些参数但并不想直接把他们显示到表格中,而是想在别的地方显示,那么我们就需要用到userdata标签。
Java代码 复制代码
  1. jsonReader: {   
  2.   ...   
  3.   userdata: "userdata",   
  4.   ...   
  5. }  

从服务器端返回数据:
Java代码 复制代码
  1. {    
  2.   total: "xxx",    
  3.   page: "yyy",    
  4.   records: "zzz",    
  5.   userdata: {totalinvoice:240.00, tax:40.00},    
  6.   rows : [    
  7.     {id:"1", cell:["cell11""cell12""cell13"]},    
  8.     {id:"2", cell:["cell21""cell22""cell23"]},    
  9.     ...    
  10.   ]    
  11. }  

在客户端我们得到的数据为:
Java代码 复制代码
  1. userData = {totalinvoice:240.00, tax:40.00}  


在客户端我们可以有下面两种方法得到这些额外信息:
1. 使用 getGridParam 方法:
Java代码 复制代码
  1. jQuery("grid_id").getGridParam('userData')  


2. 使用getUserData()方法
Java代码 复制代码
  1. jQuery("grid_id").getUserData()  


如果想获得某个值则为:
Java代码 复制代码
  1. jQuery("grid_id").getUserDataItem( key )  
posted on 2010-05-26 16:19  民工皇帝  阅读(2355)  评论(0编辑  收藏  举报