javascript jqGrid reloadGrid 并刷新新的 colModel 和 colNames

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15836496/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:16:53  来源:igfitidea点击:

jqGrid reloadGrid and refresh new colModel and colNames

javascriptjqueryjqgrid

提问by gawpertron

I'm trying to reload a jqGrid with new rows, colNames and colModel. The row data seems to load fine but the columns don't seem to be refreshed. I've tried using GridUnload and GridDestroy but I end up losing the jQuery DOM instance entirely and no longer loads any data as well.

我正在尝试使用新行、colNames 和 colModel 重新加载 jqGrid。行数据似乎加载良好,但列似乎没有刷新。我曾尝试使用 GridUnload 和 GridDestroy,但最终我完全丢失了 jQuery DOM 实例并且不再加载任何数据。

var grid = $('#my-grid');

if(grid[0].grid == undefined) {
    grid.jqGrid(options);
} else {
    grid.setGridParam(options);
    grid.trigger('reloadGrid');
}

The grid instance is important because it will be passed to other objects as a param. These objects may attach listeners or trigger events.

网格实例很重要,因为它将作为参数传递给其他对象。这些对象可以附加侦听器或触发事件。

I'm using version 4.4.2

我使用的是 4.4.2 版

采纳答案by gawpertron

It seems that jqGrid removes the initial <table></table>from the DOM and replaces it or forgets the reference (I haven't looked that hard into it).

似乎 jqGrid<table></table>从 DOM 中删除了初始值并替换它或忘记了引用(我没有仔细研究它)。

So you have to reselect the new table everytime you want to create a new grid ie. $('table#my-grid'). This makes it tricky if you want to pass a reference of the grid's table about to other parts of your app as a parameter.

因此,每次要创建新网格时,都必须重新选择新表。$('table#my-grid'). 如果您想将网格表的引用作为参数传递给应用程序的其他部分,这会变得很棘手。

My work around involves deleting the grid reference and replacing the grid's wrapped div with the original table. then creating a jqGrid in the normal way with the new colModel and colNames.

我的解决方法包括删除网格引用并用原始表替换网格的包装 div。然后使用新的 colModel 和 colNames 以正常方式创建一个 jqGrid。

grid.empty();
delete grid[0].grid;
$('#gbox_my-grid').replaceWith(grid);
grid.jqGrid(options);

It isn't the tidiest of solutions but it does allow me to keep a permanent reference to the original <table>. I'm uncertain how other jqGrid plugins will be affect by this though.

这不是最整洁的解决方案,但它确实让我可以永久引用原始<table>. 我不确定其他 jqGrid 插件将如何受此影响。

Edit

编辑

it turns out jQuery DataTablesis better suited for customisation and we have adopted this instead of using jqGrid.

事实证明jQuery DataTables更适合自定义,我们采用了它而不是使用 jqGrid。

回答by Oleg

reloadGridreload only the bodyof the grid and not changes the column headers which will be created when the grid was created.

reloadGrid仅重新加载网格的主体,而不更改创建网格时将创建的列标题。

If you need to change number of columns or to use colNamesand colModelon place of old grid you have or recreategrid. You can use GridUnloadmethod first and then create new grid (call grid.jqGrid(data)in your case). It's important that if you cached jQuery selector to grid in a variable like gridin your code you have to assign gridone more time after call of GridUnload, so you should do something like grid = $("#grid");directly after call of GridUnload.

如果您需要更改列数或使用colNamescolModel代替旧网格,则您拥有或重新创建网格。您可以先使用GridUnload方法,然后创建新网格(grid.jqGrid(data)在您的情况下调用)。重要的是,如果您将 jQuery 选择器缓存到变量中的网格,例如grid在您的代码中,您必须grid在调用 of 之后再分配一次GridUnload,因此您应该grid = $("#grid");在调用 of 之后直接执行类似的操作GridUnload

See the answerfor more details and the code example.

有关更多详细信息和代码示例,请参阅答案

回答by Laurent Jacquot

I have combined both answers and made some modification in order to have it to work.

我结合了这两个答案并进行了一些修改以使其正常工作。

var grid = $('#tableID');

if(grid[0].grid == undefined) {
  grid.jqGrid(options);
} else {
  delete grid;
  $('#tableID').GridUnload('#tableID');
  $('#tableID').jqGrid(options);
}