jqgrid 属性的addSubGrid方法...

30739人阅读
特别推荐:怎样获取某一方某一列的值:
var rowdata=jQuery(&#list&).jqGrid('getRowData',num);
&&& &&var emergencySencondMgrId = rowdata[&emergencySencondMgrId&];//列名和jGrid定义时候的值一样
&&&alert(emergencySencondMgrId);
1.获得当前列表行数:$(&#gridid&).getGridParam(&reccount&);
2.获取选中行数据(json):$(&#gridid&).jqGrid('getRowData', id);
3.刷新列表:$(refreshSelector).jqGrid('setGridParam', { url: ''), postData: ''}).trigger('reloadGrid');&
4.选中行:$(&#jqGrid&).setSelection(&1&, true);&&&(Toggles
a selection of the row with id =&rowid;
if&onselectrow&is
true (the default) then the event onSelectRow is launched, otherwise it is not.)//true:重新加载表格数据, false:不重新加载表格数据
5.重置选中行:$(&#jqgrid&).resetSelection(); //Resets
(unselects) the selected row(s). Also works in multiselect mode.
6.清除:$(&#jqgrid&).clearGridData();&&&//Clears
the currently loaded data from grid. If the clearfooter parameter is set to true, the method clears the data placed on the footer row.
7.&$(&#jqgrid&).setCell(rowid,colname,nData,cssp,attrp);&
//This method can change the content
of particular cell and can set class or style properties. Where:&
id of the row,&
colname&the
name of the column (this parameter can be a number (the index of the column) beginning from 0&
content that can be put into the cell. If empty string the content will not be changed&
class is string then we add a class to the cell using addC if class is an array we set the new css properties via css&
properties&sets
the attribute properies of the cell,&
forceup&If
the parameter is set to true we perform update of the cell instead that the value is empty&
8.获取选中行ID
&var rowid = $(&#jqgrid&).jqGrid('getGridParam','selrow');&
var rowid = $(&#searchResultList&).getGridParam(&selrow&);
var rowData = $(&#searchResultList&).getRowData(rowid); /根据行ID,获取选中行的数据(根据)
//获取选中的多行ID列表
var selectedIds& = jQuery(&#stationList&).jqGrid(&getGridParam&,&selarrrow&); 允许多行选择时使用
=================重点讲解================
1.1 prmNames选项
prmNames是jqGrid的一个重要选项,用于设置jqGrid将要向Server传递的参数名称。其默认值为:
可以通过这个选项来自定义当向Server发送请求时,默认发送的参数名称。
这个参数很重要也很有用,正是通过这个参数,可以方便的改变默认的request的参数,以符合Server端的需要。比如在prmNames中search默认的值为&_search&,这在Struts2的Action中不太方便命名成员变量和getter/ setter。因此可以使用 prmNames: {search: 'search'} 来改变这一默认值为&search&,这在Struts2的Action对象中就很好设置getter/ setter了,即getSearch()和setSearch()。当然其他名字也是可以的。
1.2 jsonReader选项
jsonReader是jqGrid的一个重要选项,用于设置如何解析从Server端发回来的json数据。其默认值为:
可以这样理解,prmNames设置了如何将Grid所需要的参数传给Server,而jsonReader设置了如何去解析从Server端传回来的json数据。如果没有设置jsonReader的话,jqGrid将会根据默认的设置来解析json数据,并显示在表格里。但如果传回来的json数据,不太符合默认设置(比如内部的结构名不太一样),那么就有必要修改这一设置。比如:
注1:据其他网友的文章,如果设置repeatitems为false,不但数据可以乱序,而且不用每个数据元素都要具备,用到哪个找到哪个就可以了。实验却是如此。
注2:cell、id在repeatitems为true时可以用到,即每一个记录是由一对id和cell组合而成,即可以适用另一种json结构。援引文档中的例子:
repeatitems为true时:
json结构为:
repeatitems为false时:
json结构为:
2. colModel的重要选项&
和jqGrid一样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等(&)。
3. 第一个实例
3.1 服务器端
用于提供数据的Action。为了可以复用这种专门接受jqGrid传来参数的Action,我抽象出一个基本类。具体代码如下:
其中的成员变量对应着jqGrid的prmNames和jsonReader中的设置。
对应 prmNames
对应 jsonReader
每页中现实的记录行数
是否是用于查询的请求
用于排序的列名
排序的方式
用于得到实际数据的数组名称
refreshGridModel()方法中,主要工作就是为gridModel、record、total赋值,这些值也将会传回客户端。
具体的一个实现类:
而在struts.xml中,应按如下设置配置action:
3.2 客户端(浏览器)
javascript部分:
其中主要的选项在开头已经介绍过了,另外需要说明以下几点:
1、在位置(1)处,为了配合Server端的Action类中的成员变量命名,将prmNames中search对应的“_search”更改为“search”。
2、在位置(2)(3)处,为了配合Server端的Action类中的成员变量命名,将jsonReader中root对应的“rows”更改为“gridModel”,将records对应的“records”更改为“record”。
在后面的request和response解析中,就可以看到这些更改的作用。
html部分:
要想顺利的使用jqGrid,需要想页面中引入6个文件,其中4个js文件,2个css文件。它们分别是:
jquery-ui-1.8.1.custom.css(jQuery UI界面的CSS文件)ui.jqgrid.custom.css(专用于jqGrid界面的CSS文件)jquery-1.4.2.min.js(jQuery的核心)jquery-ui-1.8.1.custom.min.js(用于支持jQuery UI界面)grid.locale-zh-CN.js(针对jqGrid的locale设置,根据locale不同,选择不同的尾缀)jquery.jqGrid.min.js(jqGrid的核心,可以到jqGrid网站,根据需求选择模块下载)
注&:jqGrid的官方包中原本针对中文的locale js文件是grid.locale-cn.js,但是里面的某些设置,并没有做到完全中文化,因此我从Struts2-jQuery插件中分离出grid.locale-zh-CN.js和jquery.ui.datepicker-zh-CN.min.js这两个文件以备后用。说起来这两个文件中,针对中文的设置还是不错的。
引入这6个文件后,创建jqGrid的工作就交给上面写的javascript代码来完成了。
HTML中的代码,异常简洁。
3.3 整个流程
通过Firebug监测request和response就可以看出Server和浏览器之间的数据交互。当打开页面的时候,jqGrid初始化,会向Server发送url中定义的request,并传递参数。如下:
http://localhost:8085/Hare/jqGridTest/jqGrid01.action?search=false&nd=0&rows=15&page=1&sidx=&sord=asc
jqGrid会根据prmNames中的定义,向Server传递参数,例如将jqGrid选项中rowNum的值,作为参数rows传递到Server。
由于我将prmNames中的search设为“search”,所以参数里表中出现了这个参数;否则仍会根据原来的默认值,出现“_search”参数。
再看看response,Server发送来的json数据,格式是这样的:
jqGrid根据jsonReader中的设置,解析json数据。根据jsonReader中的root(我设置的是“gridModel”),得到数据记录数组;根据rows得到每页显示的行数;根据page设置当前页数;根据records(我设置的是“record”)得到所有记录数量;根据total得到所有页数。
由此,jqGrid完成一个request,并将得到的response,解析为所需的数据,显示到Grid表格中。
如何操作Grid表格及其数据。
jqGrid有很多方法函数,用来操作数据或者操作Grid表格本身。jqGrid的方法有两种调用方式:
$(&#grid_id&).jqGridMethod( parameter1,...,parameterN );
$(&#grid_id&).jqGrid('method', parameter1,...,parameterN );
首先介绍一下jqGrid的几个最常用的方法函数,具体的方法API也可以参考官方文档(&)
1. getGridParam
这个方法用来获得jqGrid的选项值。它具有一个可选参数name,name即代表着jqGrid的选项名,例如:
即可获得当前选中的行的ID。
注:selrow是jqGrid选项之一,默认值是null。这是一个只读选项,代表最后选中行的ID。如果执行翻页或者排序后,此选项将被设为null。关于其他选项,后续会有介绍。
如果不传入name参数,则会返回jqGrid整个选项options。
2. getRowData
这个方法用来获得某行的数据。它具有一个rowid参数,jqGrid会根据这个rowid返回对应行的数据,返回的是name:value类型的数组。例如:
如果rowid未能被找到,则返回一个空数组;如果未设置rowid参数,则以数组的形式返回Grid的所有行数据。
3. addRowData
这个方法用于向Grid中插入新的一行。执行成功返回true,否则返回false。它具有4个参数:
rowid&:新行的id号;data&:新行的数据对象,形式为{name1:value1,name2: value2…},其中name为colModel中定义的列名称name;position&:插入的位置(first:表格顶端;last:表格底端;before:srcrowid之前;after:srcrowid之后);srcrowid&:新行将插入到srcrowid指定行的前面或后面。
这个方法可以一次性插入多行,data参数必须是[{name1:value1,name2: value2…}, {name1:value1,name2: value2…}]这样的数组形式,而且rowid参数也应该设为data参数对象中代表id的field名称。不过,此时的rowid不用必须是colModel中的一部分。
注:我测试了一下,一次插入多行的情况下,用于设置插入位置的后两个参数,似乎没有起作用。插入的几行数据都被置于Grid的底端。
4. setRowData
这个方法用于为某行数据设置数据值。执行成功返回true,否则返回false。它具有3个参数:
rowid&:更新数据的行id;data&:更新的数据对象,形式为{name1:value1,name2: value2…},其中name为colModel中定义的列名称name;这个数据对象,不必设置完全,需要更新哪列,就设置哪列的name:value对;cssprop&:如果cssprop为String类型,则会使用jQuery的addClass为行增加相应名称的CSS类;如果为object类型,则会使用html的css属性,为行添加样式。如果只想增加css样式而不更新数据,可以将data参数设为false。
5. delRowData
这个方法用于删除某行数据。执行成功返回true,否则返回false。具有一个参数rowid,代表要删除的行id。例如:
6. setGridParam
这个方法与getGridParam对应,用于设置jqGrid的options选项。返回jqGrid对象。参数为{name1:value1,name2: value2…}形式的对象(name来自jqGrid的options选项名)。某些选项在设置之后需要trigger(&reloadGrid&),才能显示出效果。
注:我测试了一下,这个方法对于设置jqGrid的caption选项似乎无效。大概因此有了setCaption方法。
7. setGridWidth
为Grid动态地设定一个新的宽度。两个参数:
new_width&:以px为单位的新宽度值;shrink&:作用与jqGrid的shrinkToFit选项相同;如果此参数未设置,则沿用jqGrid的shrinkToFit选项的值。
8. trigger(&reloadGrid&)
根据当前设置,重新载入Grid表格,即意味着向Server发送一个新的请求。此方法只能用于已经构建好的Grid。此外,此方法不会使对colModel所做出的改变生效。应该使用gridUnload来重新载入对colModel的新设置。
9. 其他方法
除了以上介绍的的方法外,jqGrid还有其他有用的方法,例如:
addJSONData、clearGridData、hideCol、resetSelection、setCaption、setGridHeight、setLabel、showCol等
以及增强模块提供的方法,例如:
filterGrid、GridDestroy、GridUnload、setColProp等。
这些方法的具体用法,或浅显易懂,或不是非常常用。都可以参考官方文档(&),得到具体指示。
10. 额外的考虑
在上面介绍的增删改数据行的操作中,jqGrid实际上完成的只是客户端的操作,主要是DOM的更改工作。但是如果传到Server的请求失败了,或者没有得到想要的返回结果,那jqGrid的处理还继续吗?会不会和Server端的数据不同步了呢?
这个问题在下一篇中再讨论吧。
注: 本篇中介绍的方法在jqGrid的Demo中也有介绍,但是我觉得还是自己的实例看起来更直观,更适合自己理解。
附上代码:
javascript部分:
html部分:
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1054952次
积分:13220
积分:13220
排名:第323名
原创:325篇
转载:18篇
评论:671条
思想决定高度,
态度决定成败!
文章:14篇
阅读:105040
(2)(1)(7)(6)(5)(1)(1)(1)(1)(2)(1)(6)(24)(18)(18)(15)(16)(25)(11)(24)(51)(50)(21)(32)(1)(2)(1)Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
'm creating 2nd level subgrid. followed the example
(Sub Grid (2 nested levels)).
[jqgrid]http://www.trirand.net/demoaspnet.aspx
when parent grid's row(+) symbol is clicked, It just shows one more empty row below It without column names, So I think, Its not even loading the subgrid on client.
any ideas plz?
***aspx page:*******
& cc1:JQGrid ID="Jqgrid1" runat="server" OnDataRequesting="Jqgrid1_DataRequesting">
& SortSettings InitialSortColumn="" />
& Columns>
& cc1:JQGridColumn DataField="PrimaryId" PrimaryKey="True">
& /cc1:JQGridColumn>
& cc1:JQGridColumn DataField="ContractNumber">
& /cc1:JQGridColumn>
& cc1:JQGridColumn DataField="PONumber">
& /cc1:JQGridColumn>
& cc1:JQGridColumn DataField="POLineNumber">
& /cc1:JQGridColumn>
& cc1:JQGridColumn DataField="Description">
& /cc1:JQGridColumn>
& /Columns>
& ClientSideEvents SubGridRowExpanded="showSubGrid" />
& HierarchySettings HierarchyMode="Parent" />
& /cc1:JQGrid>
& cc1:JQGrid ID="Jqgrid2" runat="server" OnDataRequesting="Jqgrid2_DataRequesting" >
& SortSettings InitialSortColumn="" />
& Columns>
& cc1:JQGridColumn DataField="BOMKEY" PrimaryKey="True" HeaderText="BKey">
& /cc1:JQGridColumn>
& cc1:JQGridColumn DataField="PARTNUMBER" HeaderText="Part#" >
& /cc1:JQGridColumn>
& cc1:JQGridColumn DataField="DESCRIPTION" HeaderText="Desc">
& /cc1:JQGridColumn>
& /Columns>
& HierarchySettings HierarchyMode="Child" />
& /cc1:JQGrid>
& script type="text/javascript">
function showSubGrid(subgrid_id, row_id)
showSubGrid_Jqgrid2(subgrid_id, row_id);
**code behind**
protected void Jqgrid1_DataRequesting(object sender,
> Trirand.Web.UI.WebControls.JQGridDataRequestEventArgs e)
if (!LoadPurchaseOrderLineDataSet())
Jqgrid1.DataSource = dtPurchaseOrderL
Jqgrid1.DataBind();
protected void Jqgrid2_DataRequesting(object sender,
Trirand.Web.UI.WebControls.JQGridDataRequestEventArgs e)
LoadPurchaseOrderBOMDataSet(e.ParentRowKey);
Jqgrid2.DataSource = dsPurchaseOrderBOM;
Jqgrid2.DataBind();
216k44483565
I found solution - "Primary key should not contain any special characters".
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Stack Exchange
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledBad Request
Bad Request - Invalid URL
HTTP Error 400. The request URL is invalid.Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
I am new to jqGrid I want to add button to jqGrid I have reffered
and did the same but instead of showing button it is showing me the string value for be
My sample code is
jQuery("#rowed2").jqGrid({
url:'server.php?q=3',
datatype: "json",
colNames:['Buttons','type','school'],
colModel:[
{name:'act',index:'act', width:75,sortable:false},
{name:'type',index:'type', width:55},
{name:'school',index:'school', width:90, editable:true}],
rowNum:10,
rowList:[10,20,30],
pager: '#prowed2',
sortname: 'id',
viewrecords: true,
sortorder: "desc",
gridComplete: function(){
var ids = jQuery("#rowed2").jqGrid('getDataIDs');
for(var i=0;i & ids.i++){
var cl = ids[i];
be = "&input style='height:22width:20' type='button' value='E' onclick=\"jQuery('#rowed2').editRow('"+cl+"');\"
se = "&input style='height:22width:20' type='button' value='S' onclick=\"jQuery('#rowed2').saveRow('"+cl+"');\"
ce = "&input style='height:22width:20' type='button' value='C' onclick=\"jQuery('#rowed2').restoreRow('"+cl+"');\" /&";
jQuery("#rowed2").jqGrid('setRowData',ids[i],{act:be+se+ce});
jQuery("#rowed2").jqGrid('navGrid',"#prowed2",{edit:false,add:false,del:false});
I am getting result as
Where am I lacking?
Know someone who can answer?
Share a link to this
via , , , or .
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Stack Exchange
Post as a guest
Post as a guest
By posting your answer, you agree to the
Browse other questions tagged
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 jqgrid 属性 的文章

 

随机推荐