`

jqGrid basic demo

阅读更多
一 数据来自本地(客户端)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My First Grid</title>
 
<link rel="stylesheet" type="text/css" media="screen" href="css/ui-lightness/jquery-ui-1.9.2.custom.min.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
 
<style type="text/css">
html, body {
    margin: 0;
    padding: 0;
    font-size: 75%;
}
</style>
 
<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="js/grid.locale-en.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
 
<script type="text/javascript">

var data=[
         {id:1,name:"xiao",birthday:"2012-12-12"},
         {id:2,name:"xiao",birthday:"2012-12-12"},
         {id:3,name:"xiao",birthday:"2012-12-12"},
         {id:4,name:"xiao",birthday:"2012-12-12"},
         {id:5,name:"xiao",birthday:"2012-12-12"},
         {id:6,name:"xiao",birthday:"2012-12-12"},
         {id:7,name:"xiao",birthday:"2012-12-12"},
         {id:8,name:"xiao",birthday:"2012-12-12"},
         {id:9,name:"xiao",birthday:"2012-12-12"},
         {id:10,name:"xiao",birthday:"2012-12-12"},
];// the native data

$(function(){ 
  $("#list").jqGrid({
    datatype: "local",// specify the data from local 
	data:data,// the data which will be displayed 
    colNames:["ID","NAME", "BIRTHDAY"],// the column header names
    colModel :[ 
      {name:"id", index:"id", width:100}, 
      {name:"name", index:"name", width:100}, 
      {name:"birthday", index:"birthday", width:100, sortable:false}, 
    ],// the column discription
    pager: "#pager",// specify the pager bar 
    rowNum:10, // sets how many records we want to view in the grid
 	// An array to construct a select box element in the pager 
 	// in which we can change the number of the visible rows.
    rowList:[10,20,30],
 	// add the new column which counts the number of available rows
    rownumbers:true, 
 	// the column according to which the data is to be sorted 
 	// when it is initially loaded
    sortname: "id",   
    sortorder: "desc", // the initial sorting order
    // view the beginning and ending record number in the grid
    viewrecords: true, 
    gridview: true,
    caption: "native grid",
    width:500, 
    height:200,
  }); 
}); 
</script>
</head>
<body>
	<table id="list"></table> 
	<div id="pager"></div> 
</body>
</html>


二 数据源来自服务器
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My First Grid</title>
 
<link rel="stylesheet" type="text/css" media="screen" href="css/ui-lightness/jquery-ui-1.9.2.custom.min.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
 
<style type="text/css">
html, body {
    margin: 0;
    padding: 0;
    font-size: 75%;
}
</style>
<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="js/grid.locale-en.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){ 
	var result=null;
	  $("#list").jqGrid({
		    datatype: "json", // specify the data in json format
		 	// specify the url of the file that returns 
		 	// the data needed to populate the grid
		    url:"/JavaWeb/JSONServlet", 
		    // an array which describes the structure of the expected json data
		    jsonReader:{ repeatitems: false },
		    colNames:["ID","NAME", "BIRTHDAY"],// the column header names
		    colModel :[ 
		      {name:"id", index:"id", width:100}, 
		      {name:"name", index:"name", width:100}, 
		      {name:"birthday", index:"birthday", width:100, sortable:false}, 
		    ],// the column discription
		    pager: "#pager",// specify the pager bar 
		    rowNum:10, // sets how many records we want to view in the grid
		 	// An array to construct a select box element in the pager 
		 	// in which we can change the number of the visible rows.
		    rowList:[10,20,30],
		 	// add the new column which counts the number of available rows
		    rownumbers:true, 
		 	// the column according to which the data is to be sorted 
		 	// when it is initially loaded
		    sortname: "id",   
		    sortorder: "desc", // the initial sorting order
            // view the beginning and ending record number in the grid    
            viewrecords: true,
		    gridview: true,
		    caption: "remote grid",
		    width:500, 
		    height:200,
		  }); 

}); 
</script>
</head>
<body>
	<table id="list"></table> 
	<div id="pager"></div> 
</body>
</html>


Note:jsonReader可以不指定。它的默认设置如下:
jQuery("#gridid").jqGrid({
...
   jsonReader : {
     root: "rows",
     page: "page",
     total: "total",
     records: "records",
     repeatitems: true,
     cell: "cell",
     id: "id",
     userdata: "userdata",
     subgrid: {root:"rows", 
        repeatitems: true, 
       cell:"cell"
     }
   },
...
});

如果使用默认的jsonReader设置(repeatable:true),jqGrid所期望的json格式是
{ 
  "total": "xxx", 
  "page": "yyy", 
  "records": "zzz",
  "rows" : [
    {"id" :"1", "cell" :["cell11", "cell12", "cell13"]},
    {"id" :"2", "cell":["cell21", "cell22", "cell23"]},
      ...
  ]
}

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


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

其中的id,name,birthday对应于colModel中jsonmap的name属性。

更多可参考http://blog.csdn.net/hurryjiang/article/details/6891115
分享到:
评论

相关推荐

    jqGrid4.6.0 jqgrid_demo40

    jqgrid_demo40-----可用的-使用方法请查看README文件.rar-----------经我修改过的demo40,官网上面demo40下载下来是没法用的,因为里面有些东西是错的,当然我这个demo40还是还有是些不足,比喻说数据库中少表导致...

    jqueryGridDemo jqGriddemo38

    jqueryGridDemo jqGriddemo38 jquery jqgrid demo与文档

    jqGrid4.8.2 jqgrid_demo40

    jqgrid_demo40-----可用的-使用方法请查看README文件. jqgrid_3.6.5_API_en.chm------附加放在这里的其它资料。 jqGrid中文文档.doc------附加放在这里的其它资料。 jqGrid的使用.doc------附加放在这里的其它资料。

    jQgrid+demo

    直接从官方网站上下载得到的...2 如果遇到Toolbar上 search按钮 放大镜图标 出现布局错误 遮罩把所有内容都遮住了 此时可以尝试将demo文件夹中的 themes目录下的 ui jqgrid css 文件 替换为源码包中的版本 或者替换成 ...

    Jqgrid demo-史上最强大,没有之一

    Jqgrid demo-史上最强大,没有之一, 为了大家能够更好的学习和使用Jqgrid网格插件,我决定用Strtus2+Spring+hibernate+Jquery+Jqgrid实现一个Jqgrid网格插件的demo。当然官方网站上面已经有了PHP版本和ASP.NET版本...

    jqgrid_demo35

    jqgrid3.5官方demo源文件,后台用php实现

    jqGrid demo (完整版)

    一款完整版的jqGrid Demo ,对网站编程的朋友帮助很大 。可以简单实现PHP 与表格之间的运作,也可改编实现Jsp。

    jQgrid demo

    jQgrid+demo

    JqGrid中文API文档

    JqGrid中文API文档,包含了基础的JqGrid控件的属性方法事件等,有如何使用JqGrid的Demo.

    jqGridDemo

    官网也有的(http://blog.mn886.net/jqGrid/ )上传文件是根据官网的demo调试的。方便自己查阅

    JQgrid demo

    JQgrid的jsp demo,集成mysql Hibernate 测试demo,下载即用

    JqGrid插件+JqGridDemo+JqGrid主题

    JqGrid插件+JqGridDemo+JqGrid主题,自己项目里引入的文件

    jqGrid MVC demo(含jqChart)

    vs2008源码,含NORTHWND数据库。

    JqGrid 在asp.net 中的demo

    JqGrid 在asp.net 中的demo。异步从服务器端或许json格式的数据。只是简单配置,复杂功能可查询文档配置。使用sql server 2005 数据库,开启数据库服务后,可直接运行default.aspx

    jqGrid_demo

    NULL 博文链接:https://lidechungo-126-com.iteye.com/blog/1534362

    jqGrid 3.5源码+DEMO+DOC

    好用的jquery表格插件jqGrid。 本压缩包含源码和DEMO,还有一个3.4的DOC。

    jqgrid demo

    jqgrid 中文文档离线demo打包. 下载 本地可架设运行。

    jqGrid使用demo: 数据加载 增加, 修改, 删除, 还原, 撤销等

    jqgrid 使用: 数据加载 增加, 修改, 删除, 还原, 撤销等 逻辑上都做了很好的判断,希望能给你们带来帮助,以后也会持续更新jqgrid使用代码。

Global site tag (gtag.js) - Google Analytics