javascript loadComplete 和 gridComplete 事件有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15437218/
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
What the difference between loadComplete and gridComplete events?
提问by Gill Bates
This question originated after I looked on thisanswer of Olegand demo-grids in it.
这个问题起源于我在其中查看了Oleg和 demo-grids 的这个答案。
gridComplete
:
gridComplete
:
This fires after all the data is loaded into the grid and all other processes are complete. Also the event fires independent from the datatype parameter and after sorting paging and etc.
这会在所有数据加载到网格中并且所有其他进程完成后触发。此外,该事件独立于数据类型参数以及在对分页等进行排序后触发。
loadComplete
:
loadComplete
:
This event is executed immediately after every server request. data Data from the response depending on datatype grid parameter
此事件在每次服务器请求后立即执行。来自响应的数据取决于数据类型网格参数
From that docs I understood that gridComplete
fires at the end of drawing grid, and loadComplete
fires after jqGrid completes communication with backend.
从那个文档我了解到,gridComplete
在绘制网格结束时loadComplete
触发,并在 jqGrid 完成与后端的通信后触发。
And so I wonder - why in demos, loadComplete
used for change color of cells and not gridComplete
?
所以我想知道 - 为什么在演示中,loadComplete
用于改变细胞的颜色而不是gridComplete
?
回答by Oleg
I think that this question is asked by many users of jqGrid. So it's interesting to know the answer.
我认为 jqGrid 的许多用户都问过这个问题。所以知道答案很有趣。
I personally prefer to use loadComplete
. If you examine code from all my examples which I posted, you will find gridComplete
only when the Original Poster posted it in the question and I would have modified a little code. I prefer to use loadComplete
because of some advantages of loadComplete
and disadvantages of gridComplete
.
我个人更喜欢使用loadComplete
. 如果您检查我发布的所有示例中的代码,您会发现gridComplete
只有当原始海报将其发布在问题中时,我才会修改一些代码。我更喜欢使用loadComplete
的原因的一些优点loadComplete
和缺点gridComplete
。
Here are advantages of loadComplete
:
以下是优点loadComplete
:
- It's the last callback which will be called if the whole grid bodywill be reloaded. For example after loading the page on the grid from the server. It's important to understand, that if the user changes sorting of some column or sets filter or chooses another page of the grid; the grid body will be reloaded.
loadComplete
has parameterdata
which represent full page of local data or full data loaded from the server.
- 如果整个网格体将被重新加载,它将是最后一个回调。例如在从服务器加载网格上的页面之后。重要的是要了解,如果用户更改某些列的排序或设置过滤器或选择网格的另一页;网格体将被重新加载。
loadComplete
具有data
代表本地数据的完整页面或从服务器加载的完整数据的参数。
On the other side gridComplete
will be called (in the current version of jqGrid 4.4.4) from internal updatepager
(see here), which will be called from delRowData
(see here), addRowData
(see here) and clearGridData
(see here) methods; in additionto addXmlData
(see here) and addJSONData
(see here). It's not what one mostly want.
另一方面gridComplete
将被调用(在当前版本的 jqGrid 4.4.4 中)从内部updatepager
(见这里),它将从delRowData
(见这里)、addRowData
(见这里)和clearGridData
(见这里)方法调用;除了到addXmlData
(见这里)和addJSONData
(见这里)。这不是人们最想要的。
Another disadvantage of gridComplete
one can see if one examines the code of addXmlData
(see here) and addJSONData
(see here) from whereupdatepager
is called and so gridComplete
will be called. If one uses loadonce: true
and the internal parameters data
and _index
will be filled with full data returned from the server. One can see when using loadonce: true
; the callback gridComplete
will be called after the first page of data are loaded from the sever. At this moment data
and _index
contains only the data for the page. On the other side loadComplete
will be called later after all data returned from the server are processed and saved locallyin data
and _index
.
另一个缺点是gridComplete
可以查看是否检查addXmlData
(see here) 和addJSONData
(see here) from where的代码,updatepager
因此gridComplete
将被调用。如果使用loadonce: true
和内部参数data
,_index
将填充从服务器返回的完整数据。使用时可以看到loadonce: true
;gridComplete
从服务器加载第一页数据后将调用回调。此时data
,_index
只包含页面的数据。在loadComplete
从服务器返回的所有数据被处理并本地保存在data
和之后,另一端将稍后调用_index
。
If you load the data from the server and if you don't use loadonce: true
option, clearGridData
, addRowData
and delRowData
then you could use gridComplete
instead of loadComplete
.
如果从服务器加载数据,而如果你不使用loadonce: true
选项,clearGridData
,addRowData
和delRowData
那么你可以使用gridComplete
的替代loadComplete
。
回答by ktm5124
Looking at the source of jqGrid (source)you can see that gridComplete is called on just one line of grid.base.
查看jqGrid的源码(source)可以看到gridComplete只在grid.base的一行调用。
1725: if($.isFunction(ts.p.gridComplete)) {ts.p.gridComplete.call(ts);}
1725: if($.isFunction(ts.p.gridComplete)) {ts.p.gridComplete.call(ts);}
This line comes from the function updatePager
.
这一行来自函数updatePager
.
You can find loadComplete
in the populate
function (line 1757). Unlike gridComplete
, it is passed an extra parameter. Both callbacks receive a reference to this
, but loadComplete
also receives the data returned from the server (or passed in locally):
您可以loadComplete
在populate
函数中找到(第 1757 行)。与 不同gridComplete
,它传递了一个额外的参数。两个回调都接收对 的引用this
,但loadComplete
也接收从服务器返回的数据(或在本地传入):
1858: case "xmlstring":
if(lcf) {ts.p.loadComplete.call(ts,dstr);}
1869: case "jsonstring":
if(lcf) {ts.p.loadComplete.call(ts,dstr);}
1881:
case "local":
case "clientside":
if(lc) { lc.call(ts,req); }
The thing is, the functions populate
and updatePager
often happen in tandem, so you see that when sorting and paging, both callbacks are called. The difference, once again, is that loadComplete
is passed an extra parameter.
问题是,这些函数populate
和updatePager
经常同时发生,所以你会看到在排序和分页时,两个回调都被调用。区别再次loadComplete
在于传递了一个额外的参数。
There are probably subtle differences that I haven't encountered yet... and there might be cases where one is called and the other isn't, but I have noticed that on sorting and paging, both are called.
可能存在我尚未遇到的细微差异……并且可能存在调用一个而另一个未调用的情况,但我注意到在排序和分页时,两者都被调用。