jqgrid

来源:互联网 发布:mac系统安装word文档 编辑:程序博客网 时间:2024/04/26 13:29
一 数据来自本地(客户端)
Js代码  收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  5. <title>My First Grid</title>  
  6.    
  7. <link rel="stylesheet" type="text/css" media="screen" href="css/ui-lightness/jquery-ui-1.9.2.custom.min.css" />  
  8. <link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />  
  9.    
  10. <style type="text/css">  
  11. html, body {  
  12.     margin: 0;  
  13.     padding: 0;  
  14.     font-size: 75%;  
  15. }  
  16. </style>  
  17.    
  18. <script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>  
  19. <script src="js/grid.locale-en.js" type="text/javascript"></script>  
  20. <script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>  
  21.    
  22. <script type="text/javascript">  
  23.   
  24. var data=[  
  25.          {id:1,name:"xiao",birthday:"2012-12-12"},  
  26.          {id:2,name:"xiao",birthday:"2012-12-12"},  
  27.          {id:3,name:"xiao",birthday:"2012-12-12"},  
  28.          {id:4,name:"xiao",birthday:"2012-12-12"},  
  29.          {id:5,name:"xiao",birthday:"2012-12-12"},  
  30.          {id:6,name:"xiao",birthday:"2012-12-12"},  
  31.          {id:7,name:"xiao",birthday:"2012-12-12"},  
  32.          {id:8,name:"xiao",birthday:"2012-12-12"},  
  33.          {id:9,name:"xiao",birthday:"2012-12-12"},  
  34.          {id:10,name:"xiao",birthday:"2012-12-12"},  
  35. ];// the native data  
  36.   
  37. $(function(){   
  38.   $("#list").jqGrid({  
  39.     datatype: "local",// specify the data from local   
  40.     data:data,// the data which will be displayed   
  41.     colNames:["ID","NAME""BIRTHDAY"],// the column header names  
  42.     colModel :[   
  43.       {name:"id", index:"id", width:100},   
  44.       {name:"name", index:"name", width:100},   
  45.       {name:"birthday", index:"birthday", width:100, sortable:false},   
  46.     ],// the column discription  
  47.     pager: "#pager",// specify the pager bar   
  48.     rowNum:10, // sets how many records we want to view in the grid  
  49.     // An array to construct a select box element in the pager   
  50.     // in which we can change the number of the visible rows.  
  51.     rowList:[10,20,30],  
  52.     // add the new column which counts the number of available rows  
  53.     rownumbers:true,   
  54.     // the column according to which the data is to be sorted   
  55.     // when it is initially loaded  
  56.     sortname: "id",     
  57.     sortorder: "desc"// the initial sorting order  
  58.     // view the beginning and ending record number in the grid  
  59.     viewrecords: true,   
  60.     gridview: true,  
  61.     caption: "native grid",  
  62.     width:500,   
  63.     height:200,  
  64.   });   
  65. });   
  66. </script>  
  67. </head>  
  68. <body>  
  69.     <table id="list"></table>   
  70.     <div id="pager"></div>   
  71. </body>  
  72. </html>  


二 数据源来自服务器
Js代码  收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  5. <title>My First Grid</title>  
  6.    
  7. <link rel="stylesheet" type="text/css" media="screen" href="css/ui-lightness/jquery-ui-1.9.2.custom.min.css" />  
  8. <link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />  
  9.    
  10. <style type="text/css">  
  11. html, body {  
  12.     margin: 0;  
  13.     padding: 0;  
  14.     font-size: 75%;  
  15. }  
  16. </style>  
  17. <script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>  
  18. <script src="js/grid.locale-en.js" type="text/javascript"></script>  
  19. <script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>  
  20. <script type="text/javascript">  
  21. $(function(){   
  22.     var result=null;  
  23.       $("#list").jqGrid({  
  24.             datatype: "json"// specify the data in json format  
  25.             // specify the url of the file that returns   
  26.             // the data needed to populate the grid  
  27.             url:"/JavaWeb/JSONServlet",   
  28.             // an array which describes the structure of the expected json data  
  29.             jsonReader:{ repeatitems: false },  
  30.             colNames:["ID","NAME""BIRTHDAY"],// the column header names  
  31.             colModel :[   
  32.               {name:"id", index:"id", width:100},   
  33.               {name:"name", index:"name", width:100},   
  34.               {name:"birthday", index:"birthday", width:100, sortable:false},   
  35.             ],// the column discription  
  36.             pager: "#pager",// specify the pager bar   
  37.             rowNum:10, // sets how many records we want to view in the grid  
  38.             // An array to construct a select box element in the pager   
  39.             // in which we can change the number of the visible rows.  
  40.             rowList:[10,20,30],  
  41.             // add the new column which counts the number of available rows  
  42.             rownumbers:true,   
  43.             // the column according to which the data is to be sorted   
  44.             // when it is initially loaded  
  45.             sortname: "id",     
  46.             sortorder: "desc"// the initial sorting order  
  47.             // view the beginning and ending record number in the grid      
  48.             viewrecords: true,  
  49.             gridview: true,  
  50.             caption: "remote grid",  
  51.             width:500,   
  52.             height:200,  
  53.           });   
  54.   
  55. });   
  56. </script>  
  57. </head>  
  58. <body>  
  59.     <table id="list"></table>   
  60.     <div id="pager"></div>   
  61. </body>  
  62. </html>  


Note:jsonReader可以不指定。它的默认设置如下:
Js代码  收藏代码
  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. });  

如果使用默认的jsonReader设置(repeatable:true),jqGrid所期望的json格式是
Js代码  收藏代码
  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. }  

标签描述total返回的页面数量page要展现的当前页records返回的记录数量rows包含返回记录的数组id行的唯一标识符cell作为rows数组元素对象的一个属性包含返回记录数据的数组(与rows的区别在于:rows可包含除了真正的数据之外的其他属性)

如果设置repeatable:false,则表明让jqGrid通过名字在json数据中搜索元素。 
jqGrid期望的json返回格式如下:
Js代码  收藏代码
  1. {   
  2.   "total""xxx",   
  3.   "page""yyy",   
  4.   "records""zzz",  
  5.   "rows" : [  
  6.     {"id" :"1""name" :"xiao","birthday":"2012-01-01"},  
  7.     {"id" :"2""name":"xiao","birthday":"2012-01-01"},  
  8.       ...  
  9.   ]  
  10. }  

其中的id,name,birthday对应于colModel中jsonmap的nam
0 0
原创粉丝点击