jQuery jqGrid 刷新本地数据(JSON 对象)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2978523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jqGrid Refreshing Local Data (JSON Object)
提问by Koobz
I'm trying to use jqGrid with local data and I'm finding a couple of issues:
我正在尝试将 jqGrid 与本地数据一起使用,但我发现了几个问题:
I'm initializing the thing like so:
我正在初始化这样的事情:
function refreshGrid($grid, results)
{
$grid.jqGrid({
data: results,
datatype: "local",
colNames:['#','File', 'Category', 'Line Number', 'Message Text','Detailed'],
colModel:[
{name:'count',index:'count', width:100, resizable: true},
{name:'basename',index:'basename', width:100, resizable: true, classes:['basename']},
{name:'category',index:'category', width:60, resizable: true},
{name:'linenumber',index:'linenumber', width:60, resizable: true},
{name:'text',index:'text',width:400, resizable: true},
{name:'detailed',index:'detailed',width:100,classes:['detailed'], resizable: true }
],
viewrecords: true,
rowNum:100,
rowList:[100,200],
pager: '#debug_errors_pager',
caption:"JSON Example"
});
}
The data I'm passing in, results
is an array of objects.
我传入的数据results
是一个对象数组。
Issues:
问题:
1) The pager is totally off. It shows the correct count, but it doesn't actually let me page through the data.
1) 寻呼机完全关闭。它显示了正确的计数,但实际上并没有让我翻阅数据。
2) I can't refresh the data. I'm using my own search function to arrive at my results
. I can't figure out how to update the existing data. The grid initializes the first time. On subsequent attempts, it initializes to an empty table.
2)我无法刷新数据。我正在使用我自己的搜索功能来访问我的results
. 我不知道如何更新现有数据。网格第一次初始化。在随后的尝试中,它初始化为一个空表。
3) I've tried things like:
3)我试过这样的事情:
$grid.empty()
- Doesn't work because the $grid object is decorated by jqgrid. I'm trying to "nuke" the old grid and simply re-render it as a workaround.
`$grid.trigger('reloadGrid') - Doesn't work, don't know why.
$grid.empty()
- 不起作用,因为 $grid 对象是由 jqgrid 装饰的。我正在尝试“核对”旧网格并简单地将其重新渲染为一种解决方法。`$grid.trigger('reloadGrid') - 不工作,不知道为什么。
Note: this is using jQGrid 3.7.
注意:这是使用 jQGrid 3.7。
回答by Ved
You can use simple:
您可以使用简单的:
jQuery("#list")
.jqGrid('setGridParam',
{
datatype: 'local',
data:mydata
})
.trigger("reloadGrid");
回答by Megan
I was able to achieve something similar. The trick for my issue was to clear out the data before updating the data grid parameter. Assuming your grid is initialized elsewhere (with datatype: 'local'), try:
我能够实现类似的目标。我的问题的诀窍是在更新数据网格参数之前清除数据。假设您的网格在其他地方初始化(数据类型为:'local'),请尝试:
function refreshGrid($grid, results) {
$grid.jqGrid('clearGridData')
.jqGrid('setGridParam', { data: results })
.trigger('reloadGrid', [{ page: 1}]);
}
回答by bruno
question 1:
问题 1:
If we have defined a pager for grid with client side data, the buttons in pager are automatically disabled. In other words, the current release of grid does not support client side paging and searching.
如果我们为带有客户端数据的网格定义了寻呼机,寻呼机中的按钮将自动禁用。换句话说,当前版本的网格不支持客户端分页和搜索。
Question 2: Have you tried:
问题 2:您是否尝试过:
$("#list").GridUnload();
see herefor the differences between gridUnload()
and trigger('reloadGrid')
.
有关和之间的差异,请参见此处。gridUnload()
trigger('reloadGrid')
回答by WayFarer
I haven't found a good documented way to update jqGrid's local data, here is how I extended jqGrid to solve my problem:
我还没有找到一个很好的文档化方法来更新 jqGrid 的本地数据,这是我扩展 jqGrid 来解决我的问题的方法:
$.extend($.fn.jqGrid, { setData: function(data) {
this[0].p.data = data;
return true;
} } );
After this you can do the following to update jqGrid with new data:
在此之后,您可以执行以下操作以使用新数据更新 jqGrid:
suppose your grid was declared like this:
假设您的网格是这样声明的:
jQuery("#list").jqGrid({ datatype: "local", ... other params ... });
then you can do:
那么你可以这样做:
jQuery("#list").jqGrid('setData', array_with_new_data);
jQuery("#list").trigger("reloadGrid");
P.S. see the link on how to fetch all data from the grid Retrieving contents of the grid
PS 请参阅有关如何从网格中获取所有数据的链接Retrieving content of the grid
回答by zishan
Following works for me when refreshing data on an existing grid:
在现有网格上刷新数据时,以下对我有用:
var gridData = [...];
var grid = jQuery('#gridId');
grid.jqGrid('clearGridData');
if( grid.get(0).p.treeGrid ) {
grid.get(0).addJSONData({
total: 1,
page: 1,
records: gridData.length,
rows: gridData
});
}
else {
grid.jqGrid('setGridParam', {
datatype: 'local',
data: gridData,
rowNum: gridData.length
});
}
grid.trigger('reloadGrid');
I had to do it this way because reloadGrid() calls populate(), populate() calls addLocalData() and passes the return value to addJSONData(), but for treeGrid == true, addLocalData() returns nothing, and addJSONData() subsequently does nothing.
我不得不这样做,因为 reloadGrid() 调用 populate(),populate() 调用 addLocalData() 并将返回值传递给 addJSONData(),但是对于 treeGrid == true,addLocalData() 不返回任何内容,而 addJSONData()随后什么也不做。
回答by Dulith De Costa
If you are using Local data, you should mention your Local json
Data in jqGrid
's
setGridParam
property
如果您使用的是本地数据,您应该json
在jqGrid
's
属性中提及您的本地数据setGridParam
jQuery("#list")
.jqGrid('setGridParam', {
datatype: 'local',
data:results //json object
});
To reloadthe jqgrid
you can use the following.
要重装的jqgrid
,你可以使用下面的。
jQuery("#list").trigger("reloadGrid");