Javascript jqGrid 重新加载网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4232430/
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 reload grid
提问by Max Frai
It's an addition for previous my question about adding columns into jqGrid-based table. Here my new js-code:
这是我之前关于将列添加到基于 jqGrid 的表的问题的补充。这是我的新 js 代码:
var col_names = ['First', 'Second', 'Third', 'Fourth', 'Fifth'];
var col_model = [
{name:'invid', index:'invid', width:100},
{name:'invdate', index:'invdate', width:90},
{name:'amount', index:'amount', width:80, align:'right'},
{name:'tax', index:'tax', width:80, align:'right'},
{name:'total', index:'total', width:80, align:'right'},
];
function createGrid()
{
var handle = $("#list").jqGrid({
url:'data.xml',
datatype: 'xml',
mtype: 'GET',
colNames: col_names,
colModel : col_model,
});
}
Now I call createGrid();
after document is loaded, everything works fine. Now I want to add a new column (with empty data) and reload jqGrid:
现在我createGrid();
在加载文档后调用,一切正常。现在我想添加一个新列(带有空数据)并重新加载 jqGrid:
$("#add_column").click(function() {
$('#list').trigger("DestroyGrid"); // Also tried UnloadGrid
col_names.push('New');
col_model.push({name: 'test', index: 'test', width: 100});
createGrid(); // And recreate grid
});
But nothing happens, why?
但什么也没发生,为什么?
UPD
UPD
$("#add_column").click(function() {
col_names.push('New');
col_model.push({name: 'test', index: 'test', width: 100});
$('#list').trigger("reloadGrid");
});
The same situation
同样的情况
UPD2I tried these:
UPD2我试过这些:
ajaxGridOptions: {cache: false},
loadonce:false
Didn't change the situation.
没有改变局面。
回答by Oleg
You can do about following
您可以执行以下操作
var counter=1; // to be able to click more then one time
$("#add_column").click(function() {
$("#list").jqGrid('GridUnload');
col_names.push('New'+counter);
col_model.push({name: 'test'+counter, index: 'test'+counter, width: 100});
counter++;
createGrid();
});
回答by Vadyus
try not to Destroy/Create, but .trigger("reloadGrid");
尽量不要破坏/创造,而是 .trigger("reloadGrid");
UPD:: try to pass loadonce:false to grid creation params
UPD::尝试将 loadonce:false 传递给网格创建参数
UPD2:: Seemed like jQgrid cant operate with column changes "On fly". Try to destroy first grid and create another one instead of it.
UPD2::似乎 jQgrid 无法在“动态”列更改的情况下进行操作。尝试销毁第一个网格并创建另一个网格而不是它。