jQuery 用新数据重新加载 jqGrid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28320787/
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
Reload jqGrid with new data
提问by Theodore K.
I have this simple function
我有这个简单的功能
function createGrid(limit){
$("#list").trigger("reloadGrid");
$("#list").jqGrid({
url:indexObj.gridUrl,
postData:{
"limit":limit
},
datatype: "json",
colNames:['ID','type','folder','Description'],
colModel:[
{name:'id',index:'id', width:50},
{name:'type',index:'type', width:100},
{name:'folder_id',index:'folder_id', width:100},
{name:'description',index:'description', width:200}
]
});
}
I first call it with limit = 1 and it calls the server and returns the right data. Then I call it with limit = 2 and it just reruns the previous ajax call (with limit=1) and returns the same data of course.
我首先使用 limit = 1 调用它,它调用服务器并返回正确的数据。然后我用 limit = 2 调用它,它只是重新运行之前的 ajax 调用(limit=1)并返回相同的数据。
Why postDatadoesn't actually change? I see in fireBug that "limit" does have the correct value.
为什么postData实际上没有改变?我在 fireBug 中看到“限制”确实具有正确的值。
回答by Runcorn
Or while reloading you can use setGridParam
for resetting postData
,
或者在重新加载时您可以setGridParam
用于重置postData
,
$("#list").jqGrid('setGridParam', {
postData: {"limit":limit }
}).trigger('reloadGrid');
And, you don't need to reinitialize/create jqGrid but you can simply use:
而且,您不需要重新初始化/创建 jqGrid,但您可以简单地使用:
function createGrid(limit){
$("#list").trigger("reloadGrid");
#Code Goes here#
}
回答by Oleg
I'm not sure that the construct with such caching of the data is good, but nevertheless your current problem is clear.
我不确定具有这种数据缓存的构造是否良好,但是您当前的问题很清楚。
It's important to understand that the call
重要的是要了解调用
$("#list").jqGrid({
url:indexObj.gridUrl,
...
});
creates the grid. It converts empty <table id="list"></table>
in very complex structure of divs and tables. So one can create the grid only once.
创建网格。它<table id="list"></table>
在非常复杂的 div 和表格结构中转换为空。所以一个人只能创建一次网格。
The grid consist from many parts (like headers) which don't need be recreated on the next filling of the grid. So jqGrid provides reloadGrid
event which can be triggered to refill the bodyof the grid. If jqGrid have postData
with some value like
网格由许多部分(如标题)组成,不需要在下一次填充网格时重新创建。所以 jqGrid 提供了reloadGrid
可以触发的事件来重新填充网格的主体。如果 jqGrid 具有postData
某些值,例如
postData: {
limit: limitVar
}
then if means that the object value of postData
will be created and initialized oncewith the current value of limitVar
variable. If you have outervariable (global, or defined in some outer scope) then you can use
then if 表示postData
将使用变量的当前值创建和初始化一次的对象值limitVar
。如果您有外部变量(全局变量,或在某个外部作用域中定义),那么您可以使用
postData: {
limit: function () { return limitVar; }
}
In the case you will have the currentvalue of limitVar
as the value of limit
parameter of URL. By the way if the user just click on the column header, the grid need be sorted and jqGrid will make new HTTP request to url
. If you use function
inside of postData
then you will have the currentvalue of limitVar
in the cases too.
在这种情况下,您将使用当前值limitVar
作为limit
URL 参数的值。顺便说一下,如果用户只是单击列标题,则需要对网格进行排序,jqGrid 将向url
. 如果您使用function
inside ,postData
那么您也将拥有案例中的当前值limitVar
。
If you will make less modification in your existing code then you can replace the line $("#list").trigger("reloadGrid");
(which is absolutely unneeded in the current code) to
如果您将在现有代码中进行较少的修改,则可以将该行$("#list").trigger("reloadGrid");
(在当前代码中绝对不需要)替换为
$("#list").jqGrid("GridUnload");
It will destroy previously created structure of dives and tables (which build the grid) and creates an empty <table id="list"></table>
on the same place. So you can recreate the grid. Such code will work not so effectively, but it could be very helpful in some scenarios (see the answerand this onefor example).
它将破坏先前创建的潜水和表格结构(构建网格)并<table id="list"></table>
在同一个地方创建一个空的。所以你可以重新创建网格。这样的代码不会那么有效,但它在某些情况下可能非常有用(例如,请参阅答案和这个)。