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

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

What the difference between loadComplete and gridComplete events?

javascriptdatagridjqgrid

提问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 gridCompletefires at the end of drawing grid, and loadCompletefires after jqGrid completes communication with backend.

从那个文档我了解到,gridComplete在绘制网格结束时loadComplete触发,并在 jqGrid 完成与后端的通信后触发。

And so I wonder - why in demos, loadCompleteused 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 gridCompleteonly when the Original Poster posted it in the question and I would have modified a little code. I prefer to use loadCompletebecause of some advantages of loadCompleteand 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.
  • loadCompletehas parameter datawhich represent full page of local data or full data loaded from the server.
  • 如果整个网格体将被重新加载,它将是最后一个回调。例如在从服务器加载网格上的页面之后。重要的是要了解,如果用户更改某些列的排序或设置过滤器或选择网格的另一页;网格体将被重新加载。
  • loadComplete具有data代表本地数据的完整页面或从服务器加载的完整数据的参数。

On the other side gridCompletewill 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 gridCompleteone can see if one examines the code of addXmlData(see here) and addJSONData(see here) from whereupdatepageris called and so gridCompletewill be called. If one uses loadonce: trueand the internal parameters dataand _indexwill be filled with full data returned from the server. One can see when using loadonce: true; the callback gridCompletewill be called after the first page of data are loaded from the sever. At this moment dataand _indexcontains only the data for the page. On the other side loadCompletewill be called later after all data returned from the server are processed and saved locallyin dataand _index.

另一个缺点是gridComplete可以查看是否检查addXmlData(see here) 和addJSONData(see here) from where的代码,updatepager因此gridComplete将被调用。如果使用loadonce: true和内部参数data_index将填充从服务器返回的完整数据。使用时可以看到loadonce: truegridComplete从服务器加载第一页数据后将调用回调。此时data_index只包含页面的数据。在loadComplete从服务器返回的所有数据被处理并本地保存data之后,另一端将稍后调用_index

If you load the data from the server and if you don't use loadonce: trueoption, clearGridData, addRowDataand delRowDatathen you could use gridCompleteinstead of loadComplete.

如果从服务器加载数据,而如果你不使用loadonce: true选项,clearGridDataaddRowDatadelRowData那么你可以使用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 loadCompletein the populatefunction (line 1757). Unlike gridComplete, it is passed an extra parameter. Both callbacks receive a reference to this, but loadCompletealso receives the data returned from the server (or passed in locally):

您可以loadCompletepopulate函数中找到(第 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 populateand updatePageroften happen in tandem, so you see that when sorting and paging, both callbacks are called. The difference, once again, is that loadCompleteis passed an extra parameter.

问题是,这些函数populateupdatePager经常同时发生,所以你会看到在排序和分页时,两个回调都被调用。区别再次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.

可能存在我尚未遇到的细微差异……并且可能存在调用一个而另一个未调用的情况,但我注意到在排序和分页时,两者都被调用。