问题描述
我正在使用 jqGrid 的 filterToolbar
方法让用户快速搜索/过滤网格数据.我使用 loadonce: true
和 datatype: local
作为我的网格配置.我的一个列有一个下拉(选择)过滤器类型,效果很好.
I'm using jqGrid's filterToolbar
method to let users quick search/filter the grid data. I'm using loadonce: true
and datatype: local
as my grid configs. I have a drop-down (select) filter type for one of my columns, which works fine.
问题是当我尝试更新不可见的行(使用 setRowData
)时(因为过滤器/搜索结果隐藏了它们),当我重新显示它们时,该行没有得到更新通过清除过滤器.
Problem is when i try to update a row (using setRowData
) that is not visible (because the filter/search result is hiding them), the row doesn't get updated when I reshow them by clearing the filter.
有什么建议吗?我也试过触发 reloadGrid
事件,没有运气.
Any suggestions? I've tried triggering the reloadGrid
event too, no luck.
干杯
这里是重现问题的方法,使用 jqGrid 的官方演示:
Here's how to reproduce the problem, using jqGrid's official demos:
浏览到 jqGrid 的演示页面,并在 'New in version 3.7' 下打开名为 'Toolbar search' 的演示
Browse to the jqGrid's demo page, and open the demo named 'Toolbar search' under 'New in version 3.7'
在演示网格中,按值为 575878 的代码列进行过滤,以便网格上只显示第一行.
In the demo grid, filter by the code column with the value 575878 so that only the first row is shown on the grid.
打开 javascript 控制台并更新当前不可见的行,在此示例中更新第 2 行:jQuery("#toolbar").jqGrid('setRowData',2,{item_id:2,item:'blargh',item_cd:12345678});
Bring up the javascript console and update a row that's not currently visible, in this example update row 2:jQuery("#toolbar").jqGrid('setRowData',2,{item_id:2,item:'blargh',item_cd:12345678});
通过清除过滤器值取消隐藏所有行,并看到第 2 行没有更新!
Unhide all the rows by clearing the filter value, and see that row 2 has not been updated!
我在这里做错了什么?可能的解决方法?
Anything I'm doing wrong here? Possible workarounds?
推荐答案
你误解了网格是如何构建的.网格可以包含隐藏列,但没有隐藏行.如果一个过滤器网格,整个网格体将被删除,并且只插入过滤的行.
You misunderstand how the grid are build. Grid can contain hidden columns, but no hidden rows. If one filter grid the full grid body will be removed and only the filtered rows will be inserted.
setRowData
方法可用于修改网格的任何行,但不能修改网格中不存在的内容.
The method setRowData
can be used to modify any row of the grid, but you can't modify something which is not present in the grid.
如果您使用本地网格(datatype: 'local'
),那么您保存在网格中的数据将保存在两个内部 jqGrid 参数 data
和 _index
.所以你应该修改 data
对象.要使用修改后的 data
填充网格,您需要调用 .trigger("reloadGrid")
.
If you use local grid (datatype: 'local'
) then the data which you save in the grid will be saved in two internal jqGrid parameters data
and _index
. So you should modify the data
object. To fill grid with modified data
you need call .trigger("reloadGrid")
.
因此,如果您想为 rowid=2 修改网格数据的 item_id
、item
和 item_cd
列,您可以执行以下步骤.
So if you want modify columns item_id
, item
and item_cd
of the grid's data for the rowid=2 you can do the following steps.
1) 获取对内部jqGrid参数data
和_index
的引用:
1) Get references to the internal jqGrid parameters data
and _index
:
var $myGrid = jQuery("#toolbar"),
data = $myGrid.jqGrid('getGridParam', 'data'),
index = $myGrid.jqGrid('getGridParam', '_index');
2) 获取代表您需要的 rowid 的对象的引用:
2) Get reference to the object which represent the rowid which you need:
var rowId = '2',
itemIndex = index[rowId],
rowItem = data[itemIndex];
3)根据需要修改数据项:
3) Modify the data item like you as need:
rowItem.item_id = 2;
rowItem.item = 'blargh';
rowItem.item_cd = 12345678;
4) 通过重新加载网格来刷新网格包含(如果需要)
4) Refresh grid contain (if needed) by reloading the grid
$myGrid.trigger('reloadGrid');
这篇关于jqGrid setRowData 方法不更新隐藏行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!