jqGrid 参数详解

2023-11-03 02:32
文章标签 参数 详解 jqgrid

本文主要是介绍jqGrid 参数详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、 jqGrid的加载。

1.引用相关头文件

引入CSS:

<link href="Scripts/jquery-ui-1.8.1.custom.css" rel="stylesheet" type="text/css" />

<link href="Scripts/ui.jqgrid.css" rel="stylesheet" type="text/css" />

引入JS:

<script src="Scripts/jquery-1.5.1.js" type="text/javascript"></script>

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

<script src="Scripts/grid.locale-en.js" type="text/javascript"></script>

<script src="Scripts/jquery.jqGrid.min.js" type="text/javascript"></script>

因为jqGrid3.6及以后的版本集成了jQuery UI,所以,此处需要导入UI相关js和css。另外grid.locale-en.js这个语言文件必须在jquery.jqGrid.min.js之前加载,否则会出问题。

2.将jqgrid加入页面中

根据jqGrid的文档,要想生成一个jqGrid,最直接的方法就是:

$("#list").jqGrid(options);

其中list是页面上的一个table:<table id="list"></table>

下面是一个简单的例子:

<span style="font-size: 18px;"><script type=<span class="str" style="color: rgb(0, 96, 128);">"text/javascript"</span>></span>
 
<span style="font-size: 18px;">$(document).ready(<span class="kwrd" style="color: rgb(0, 0, 255);">function</span> () {</span>
 
<span style="font-size: 18px;">jQuery(<span class="str" style="color: rgb(0, 96, 128);">"#list"</span>).jqGrid({</span>
 
<span style="font-size: 18px;">url: <span class="str" style="color: rgb(0, 96, 128);">'Handler.ashx'</span>,</span>
 
<span style="font-size: 18px;">datatype: <span class="str" style="color: rgb(0, 96, 128);">"json"</span>,</span>
 
<span style="font-size: 18px;">mtype: <span class="str" style="color: rgb(0, 96, 128);">'GET'</span>,</span>
 
<span style="font-size: 18px;">colNames: [<span class="str" style="color: rgb(0, 96, 128);">'SalesReasonID'</span>, <span class="str" style="color: rgb(0, 96, 128);">'Name'</span>, <span class="str" style="color: rgb(0, 96, 128);">'ReasonType'</span>, <span class="str" style="color: rgb(0, 96, 128);">'ModifiedDate'</span>],</span>
 
<span style="font-size: 18px;">colModel: [</span>
 
<span style="font-size: 18px;">{ name: <span class="str" style="color: rgb(0, 96, 128);">'SalesReasonID'</span>, index: <span class="str" style="color: rgb(0, 96, 128);">'SalesReasonID'</span>, width: 40, align: <span class="str" style="color: rgb(0, 96, 128);">"left"</span>, editable: <span class="kwrd" style="color: rgb(0, 0, 255);">true</span> },</span>
 
<span style="font-size: 18px;">{ name: <span class="str" style="color: rgb(0, 96, 128);">'Name'</span>, index: <span class="str" style="color: rgb(0, 96, 128);">'Name'</span>, width: 100, align: <span class="str" style="color: rgb(0, 96, 128);">"center"</span> },</span>
 
<span style="font-size: 18px;">{ name: <span class="str" style="color: rgb(0, 96, 128);">'ReasonType'</span>, index: <span class="str" style="color: rgb(0, 96, 128);">'ReasonType'</span>, width: 100, align: <span class="str" style="color: rgb(0, 96, 128);">"center"</span> },</span>
 
<span style="font-size: 18px;">{ name: <span class="str" style="color: rgb(0, 96, 128);">'ModifiedDate'</span>, index: <span class="str" style="color: rgb(0, 96, 128);">'ModifiedDate'</span>, width: 150, align: <span class="str" style="color: rgb(0, 96, 128);">"center"</span>, search: <span class="kwrd" style="color: rgb(0, 0, 255);">false</span> }</span>
 
<span style="font-size: 18px;">],</span>
 
<span style="font-size: 18px;">rowList: [10, 20, 30],</span>
 
<span style="font-size: 18px;">sortname: <span class="str" style="color: rgb(0, 96, 128);">'SalesReasonID'</span>,</span>
 
<span style="font-size: 18px;">viewrecords: <span class="kwrd" style="color: rgb(0, 0, 255);">true</span>,</span>
 
<span style="font-size: 18px;">sortorder: <span class="str" style="color: rgb(0, 96, 128);">"desc"</span>,</span>
 
<span style="font-size: 18px;">jsonReader: {</span>
 
<span style="font-size: 18px;">root: <span class="str" style="color: rgb(0, 96, 128);">"griddata"</span>,</span>
 
<span style="font-size: 18px;">total: <span class="str" style="color: rgb(0, 96, 128);">"totalpages"</span>,</span>
 
<span style="font-size: 18px;">page: <span class="str" style="color: rgb(0, 96, 128);">"currpage"</span>,</span>
 
<span style="font-size: 18px;">records: <span class="str" style="color: rgb(0, 96, 128);">"totalrecords"</span>,</span>
 
<span style="font-size: 18px;">repeatitems: <span class="kwrd" style="color: rgb(0, 0, 255);">false</span></span>
 
<span style="font-size: 18px;">},</span>
 
<span style="font-size: 18px;">pager: jQuery(<span class="str" style="color: rgb(0, 96, 128);">'#pager'</span>),</span>
 
<span style="font-size: 18px;">rowNum: 5,</span>
 
<span style="font-size: 18px;">altclass: <span class="str" style="color: rgb(0, 96, 128);">'altRowsColour'</span>,</span>
 
<span class="rem" style="color: rgb(0, 128, 0);"><span style="font-size: 18px;">//width: 'auto',</span></span>
 
<span style="font-size: 18px;">width: <span class="str" style="color: rgb(0, 96, 128);">'500'</span>,</span>
 
<span style="font-size: 18px;">height: <span class="str" style="color: rgb(0, 96, 128);">'auto'</span>,</span>
 
<span style="font-size: 18px;">caption: <span class="str" style="color: rgb(0, 96, 128);">"DemoGrid"</span></span>
 
<span style="font-size: 18px;">}).navGrid(<span class="str" style="color: rgb(0, 96, 128);">'#pager'</span>, { add: <span class="kwrd" style="color: rgb(0, 0, 255);">true</span>, edit: <span class="kwrd" style="color: rgb(0, 0, 255);">true</span>, del: <span class="kwrd" style="color: rgb(0, 0, 255);">true</span>,search:<span class="kwrd" style="color: rgb(0, 0, 255);">false</span>,refresh:<span class="kwrd" style="color: rgb(0, 0, 255);">false</span> }); ;</span>
 
<span style="font-size: 18px;">})</span>

二、 jqgrid的重要选项

具体的options参考,可以访问jqGrid文档关于option的章节(http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options)。其中有几个是比较常用的,重点介绍一下:

  • url :jqGrid控件通过这个参数得到需要显示的数据,具体的返回值可以使XML也可以是Json。
  • datatype :这个参数用于设定将要得到的数据类型。类型包括:json 、xml、xmlstring、local、javascript、function。
  • mtype : 定义使用哪种方法发起请求,GET或者POST。
  • height :Grid的高度,可以接受数字、%值、auto,默认值为150。
  • width :Grid的宽度,如果未设置,则宽度应为所有列宽的之和;如果设置了宽度,则每列的宽度将会根据shrinkToFit选项的设置,进行设置。
  • shrinkToFit :此选项用于根据width计算每列宽度的算法。默认值为true。如果shrinkToFit为true且设置了width值,则每列宽度会根据width成比例缩放;如果shrinkToFit为false且设置了width值,则每列的宽度不会成比例缩放,而是保持原有设置,而Grid将会有水平滚动条。
  • autowidth :默认值为false。如果设为true,则Grid的宽度会根据父容器的宽度自动重算。重算仅发生在Grid初始化的阶段;如果当父容器尺寸变化了,同时也需要变化Grid的尺寸的话,则需要在自己的代码中调用setGridWidth方法来完成。
  • pager :定义页码控制条Page Bar,在上面的例子中是用一个div(<div id=”pager”></div>)来放置的。
  • sortname :指定默认的排序列,可以是列名也可以是数字。此参数会在被传递到Server端。
  • viewrecords :设置是否在Pager Bar显示所有记录的总数。
  • caption :设置Grid表格的标题,如果未设置,则标题区域不显示。
  • rowNum :用于设置Grid中一次显示的行数,默认值为20。正是这个选项将参数rows(prmNames中设置的)通过url选项设置的链接传递到Server。注意如果Server返回的数据行数超过了rowNum的设定,则Grid也只显示rowNum设定的行数。
  • rowList :一个数组,用于设置Grid可以接受的rowNum值。例如[10,20,30]。
  • colNames :字符串数组,用于指定各列的题头文本,与列的顺序是对应的。
  • colModel :最重要的数组之一,用于设定各列的参数。(稍后详述)
  • prmNames :这是一个数组,用于设置jqGrid将要向Server传递的参数名称。(稍后详述)
  • jsonReader :这又是一个数组,用来设定如何解析从Server端发回来的json数据。(稍后详述)

2.1 prmNames选项

prmNames是jqGrid的一个重要选项,用于设置jqGrid将要向Server传递的参数名称。其默认值为:

prmNames : {

page:"page", // 表示请求页码的参数名称

rows:"rows", // 表示请求行数的参数名称

sort: "sidx", // 表示用于排序的列名的参数名称

order: "sord", // 表示采用的排序方式的参数名称

search:"_search", // 表示是否是搜索请求的参数名称

nd:"nd", // 表示已经发送请求的次数的参数名称

id:"id", // 表示当在编辑数据模块中发送数据时,使用的id的名称

oper:"oper", // operation参数名称

editoper:"edit", // 当在edit模式中提交数据时,操作的名称

addoper:"add", // 当在add模式中提交数据时,操作的名称

deloper:"del", // 当在delete模式中提交数据时,操作的名称

subgridid:"id", // 当点击以载入数据到子表时,传递的数据名称

npage: null,

totalrows:"totalrows" // 表示需从Server得到总共多少行数据的参数名称,参见jqGrid选项中的rowTotal

}

2.2 jsonReader选项

jsonReader是jqGrid的一个重要选项,用于设置如何解析从Server端发回来的json数据,如果Server返回的是xml数据,则对应的使用xmlReader来解析。jsonReader的默认值为:

jsonReader : {

root: "rows", // json中代表实际模型数据的入口

page: "page", // json中代表当前页码的数据

total: "total", // json中代表页码总数的数据

records: "records", // json中代表数据行总数的数据

repeatitems: true, // 如果设为false,则jqGrid在解析json时,会根据name来搜索对应的数据元素(即可以json中元素可以不按顺序);而所使用的name是来自于colModel中的name设定。

cell: "cell",

id: "id",

userdata: "userdata",

subgrid: {

root:"rows",

repeatitems: true,

cell:"cell"

}

}

假如有下面一个json字符串:

{"totalpages":"3","currpage":"1","totalrecords":"11","griddata":[{"SalesReasonID":"1","Name":"Price","ReasonType":"Other","ModifiedDate":"1998年6月1日"},{"SalesReasonID":"2","Name":"On Promotion","ReasonType":"Promotion","ModifiedDate":"1998年6月1日"},{"SalesReasonID":"3","Name":"Magazine Advertisement","ReasonType":"Marketing","ModifiedDate":"1998年6月1日"},{"SalesReasonID":"4","Name":"Television Advertisement","ReasonType":"Marketing","ModifiedDate":"1998年6月1日"},{"SalesReasonID":"5","Name":"Manufacturer","ReasonType":"Other","ModifiedDate":"1998年6月1日"}]}

其对应的jsonReader为:jsonReader: {

root: "griddata",

total: "totalpages",

page: "currpage",

records: "totalrecords",

repeatitems: false

}

注:cell、id在repeatitems为true时可以用到,即每一个记录是由一对id和cell组合而成,即可以适用另一种json结构。援引文档中的例子:

repeatitems为true时:

jQuery("#gridid").jqGrid({  

     ...  

     jsonReader : {  

         root:"invdata",  

         page: "currpage",  

         total: "totalpages",  

         records: "totalrecords"

     },  

     ...  

});  

json结构为:

{   

"totalpages": "xxx",   

"currpage": "yyy",  

"totalrecords": "zzz",  

"invdata" : [  

                  {"id" :"1", "cell" :["cell11", "cell12", "cell13"]},   // cell中不需要各列的name,只要值就OK了,但是需要保持对应

                  {"id" :"2", "cell" :["cell21", "cell22", "cell23"]},  

                  ...  

     ]  

}  

repeatitems为false时:

jQuery("#gridid").jqGrid({  

     ...  

     jsonReader : {  

         root:"invdata",  

         page: "currpage",  

         total: "totalpages",  

         records: "totalrecords",  

         repeatitems: false,  

         id: "0"

     },  

     ...  

});  

json结构为:

{   

"totalpages" : "xxx",   

"currpage" : "yyy",  

"totalrecords" : "zzz",  

"invdata" : [  

                 {"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"},  

                  ...  

     ]  

}  

2.3 colModel的重要选项

colModel也有许多非常重要的选项,在使用搜索、排序等方面都会用到。这里先只说说最基本的。

  • name :为Grid中的每个列设置唯一的名称,这是一个必需选项,其中保留字包括subgrid、cb、rn。
  • index :设置排序时所使用的索引名称,这个index名称会作为sidx参数(prmNames中设置的)传递到Server。
  • label :当jqGrid的colNames选项数组为空时,为各列指定题头。如果colNames和此项都为空时,则name选项值会成为题头。
  • width :设置列的宽度,目前只能接受以px为单位的数值,默认为150。
  • sortable :设置该列是否可以排序,默认为true。
  • search :设置该列是否可以被列为搜索条件,默认为true。
  • resizable :设置列是否可以变更尺寸,默认为true。
  • hidden :设置此列初始化时是否为隐藏状态,默认为false。
  • formatter :预设类型或用来格式化该列的自定义函数名。常用预设格式有:integer、date、currency、number等(具体参见文档 )。

三、 注意事项

1. 动态改变Add Form或者Edit Form中的select的内容,如:改变下图中的Comparator下拉中的内容。

clip_image002

$("#list_d").navGrid('#pager_d',{add:true,edit:true,del:true,search:false,refresh:false},

{

checkOnSubmit:false, closeAfterEdit: true,recreateForm:true,

beforeInitData:function(formid){

initComparator();

},

beforeShowForm: function(formid){

$("#list_d").jqGrid('setColProp', 'Name', { editrules:{required:false},});

$('#tr_Name', formid).hide();

}

},//edit

{},//add

{}//del

beforeInitData, beforeShowForm在每次点击编辑的时候都会执行。initComparator的作用是通过ajax获取数据,然后利用$("#list_d").jqGrid('setColProp', 'Comparator', { editoptions: { value: valueString} });来设置Comparator下拉中的内容。其中valueString的格式如下’ equal to: equal to; not equal to: not equal to’。键值之间用冒号隔开,2项之间用分号隔开。注意:把recreateForm设为true,否则'setColProp'只在第一次调用时有效。

2. var rowNum = parseInt($(this).getGridParam("records"), 10); 得到数据条数。

3. jQuery("#list_d").clearGridData();清空数据。

4. jQuery("#list").getCell(ids,"Key");获取第ids行的key列。

5. $("#list").jqGrid('setSelection', "1");选中第一行。放在loadComplete:中在gird加载完成的时候自动选中第一行。loadComplete:function(data){$("#list").jqGrid('setSelection', "1");

}

6. 对于像1中的可编辑的字段,可以设定rule,参见http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editrules

7. 修改Option,以URL为例

jQuery("#list_d").jqGrid('setGridParam',{url:"xxx.aspx",page:1}).trigger('reloadGrid');

这篇关于jqGrid 参数详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/334983

相关文章

Java 的 Condition 接口与等待通知机制详解

《Java的Condition接口与等待通知机制详解》在Java并发编程里,实现线程间的协作与同步是极为关键的任务,本文将深入探究Condition接口及其背后的等待通知机制,感兴趣的朋友一起看... 目录一、引言二、Condition 接口概述2.1 基本概念2.2 与 Object 类等待通知方法的区别

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

Java实例化对象的​7种方式详解

《Java实例化对象的​7种方式详解》在Java中,实例化对象的方式有多种,具体取决于场景需求和设计模式,本文整理了7种常用的方法,文中的示例代码讲解详细,有需要的可以了解下... 目录1. ​new 关键字(直接构造)​2. ​反射(Reflection)​​3. ​克隆(Clone)​​4. ​反序列化

Linux实现简易版Shell的代码详解

《Linux实现简易版Shell的代码详解》本篇文章,我们将一起踏上一段有趣的旅程,仿照CentOS–Bash的工作流程,实现一个功能虽然简单,但足以让你深刻理解Shell工作原理的迷你Sh... 目录一、程序流程分析二、代码实现1. 打印命令行提示符2. 获取用户输入的命令行3. 命令行解析4. 执行命令

SpringBoot基础框架详解

《SpringBoot基础框架详解》SpringBoot开发目的是为了简化Spring应用的创建、运行、调试和部署等,使用SpringBoot可以不用或者只需要很少的Spring配置就可以让企业项目快... 目录SpringBoot基础 – 框架介绍1.SpringBoot介绍1.1 概述1.2 核心功能2

Spring Boot 事务详解(事务传播行为、事务属性)

《SpringBoot事务详解(事务传播行为、事务属性)》SpringBoot提供了强大的事务管理功能,通过@Transactional注解可以方便地配置事务的传播行为和属性,本文将详细介绍Spr... 目录Spring Boot 事务详解引言声明式事务管理示例编程式事务管理示例事务传播行为1. REQUI

MySQL 字符串截取函数及用法详解

《MySQL字符串截取函数及用法详解》在MySQL中,字符串截取是常见的操作,主要用于从字符串中提取特定部分,MySQL提供了多种函数来实现这一功能,包括LEFT()、RIGHT()、SUBST... 目录mysql 字符串截取函数详解RIGHT(str, length):从右侧截取指定长度的字符SUBST

MySQL中的事务隔离级别详解

《MySQL中的事务隔离级别详解》在MySQL中,事务(Transaction)是一个执行单元,它要么完全执行,要么完全回滚,以保证数据的完整性和一致性,下面给大家介绍MySQL中的事务隔离级别详解,... 目录一、事务并发问题二、mysql 事务隔离级别1. READ UNCOMMITTED(读未提交)2

Spring AI 实现 STDIO和SSE MCP Server的过程详解

《SpringAI实现STDIO和SSEMCPServer的过程详解》STDIO方式是基于进程间通信,MCPClient和MCPServer运行在同一主机,主要用于本地集成、命令行工具等场景... 目录Spring AI 实现 STDIO和SSE MCP Server1.新建Spring Boot项目2.a

Python处理超大规模数据的4大方法详解

《Python处理超大规模数据的4大方法详解》在数据的奇妙世界里,数据量就像滚雪球一样,越变越大,从最初的GB级别的小数据堆,逐渐演变成TB级别的数据大山,所以本文我们就来看看Python处理... 目录1. Mars:数据处理界的 “变形金刚”2. Dask:分布式计算的 “指挥家”3. CuPy:GPU