是否应该将 jqGrid 的 addJSONData 的用法替换为 setGridParam() 和 trigger('reloadGrid') 的用法? [英] Should one replace the usage addJSONData of jqGrid to the usage of setGridParam(), and trigger('reloadGrid')?

查看:12
本文介绍了是否应该将 jqGrid 的 addJSONData 的用法替换为 setGridParam() 和 trigger('reloadGrid') 的用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近写了一个问题的答案jqGrid 在更新表/自定义更新时显示默认加载"消息".在写答案时我想:为什么他使用 addJSONData() 函数来刷新网格中的数据,而不是更改相对于 setGridParam() 的 URL 并刷新 jqGrid关于 trigger('reloadGrid') 的数据?一开始我想推荐使用'reloadGrid',但考虑到这一点后我明白我不太确定最好的方法是什么.至少,我无法用两句话解释为什么我更喜欢第二种方式.所以我决定这可能是一个有趣的讨论主题.

I wrote recently an answer to the question "jqGrid display default "loading" message when updating a table / on custom update". While writing the answer I thought: why does he use the addJSONData() function for refreshing data in the grid instead of changing the URL with respect to setGridParam() and refreshing jqGrid data with respect to trigger('reloadGrid')? At the beginning I wanted to recommend using 'reloadGrid', but after thinking about this I understood that I am not quite sure what the best way is. At least, I can't explain in two sentences why I prefer the second way. So I decided that it could be an interesting subject of a discussion.

确切地说:我们有一个典型的情况.我们有一个网页,其中至少包含一个 jqGrid 和一些其他控件,例如组合框(选择)、复选框等,这些控件使用户可以更改 jqGrid 中显示的信息范围.通常我们会定义一些事件处理程序,例如 jQuery("#selector").change(myRefresh).keyup(myKeyRefresh)我们需要根据用户的选择重新加载 jqGrid 容器.

So to be exact: We have a typical situation. We have a web page with at least one jqGrid and some other controls like combo-boxes (selects), checkboxes etc. which give the user possibilities to change the scope on information displayed in a jqGrid. Typically we define some event handler like jQuery("#selector").change(myRefresh).keyup(myKeyRefresh) and we need to reload the jqGrid container based on user's choices.

在阅读和分析来自其他用户输入的信息后,我们至少可以通过两种方式刷新 jqGrid 容器:

After reading and analyzing the information from additional user's input we can refresh the jqGrid container in at least two ways:

  1. 手动调用 $.ajax() 然后在 $.ajax 的成功或完整句柄内调用 jQuery.parseJSON()(或eval)然后调用jqGrid的addJSONData函数.我在 stackoverflow.com 上找到了很多使用 addJSONData 的示例.
  2. 根据用户输入更新 jqGrid 的 url,将当前 page 编号重置为 1,并可选择更改网格的 caption.所有这些都可以通过 setGridParam() 和可选的 setCaption() jqGrid 方法来完成.最后调用网格的 trigger('reloadGrid') 函数.为了构造 url,顺便说一下,我主要使用 jQuery.param 函数来确保我已经正确打包了关于 encodeURIComponent 的所有 url 参数.
  1. Make call of $.ajax() manual and then inside of success or complete handle of $.ajax call jQuery.parseJSON() (or eval) and then call addJSONData function of jqGrid. I found a lot of examples on stackoverflow.com which use addJSONData.
  2. Update url of jqGrid based on user's input, reset current page number to 1 and optionally change the caption of the grid. All these can be done with respect to setGridParam(), and optionally setCaption() jqGrid methods. At the end call the grid's trigger('reloadGrid') function. To construct the url, by the way I use mostly jQuery.param function to be sure, that I have all url parameters packed correctly with respect to encodeURIComponent.

我希望我们讨论两种方式的优缺点.我目前使用的是第二种方式,所以我会从这种方式的优点开始.

I'd like us to discuss the advantages and disadvantages of both ways. I currently use the second way, so I'll start with advantages of this one.

可以说:我调用现有的Web Service,将接收到的数据转换为jqGrid格式并调用addJSONData.这就是我使用 addJSONData 方法的原因!

One can say: I call existing Web Service, convert received data to the jqGrid format and call addJSONData. This is the reason why I use addJSONData method!

好的,我会选择另一种方式.jqGrid 可以直接调用 Web Service 并将结果填充到网格中.有很多 jqGrid 选项,可让您自定义此过程.

OK, I'll choose another way. jqGrid can make a call on the Web Service directly and fill results inside the grid. There are a lot of jqGrid options, which allow you to customize this process.

首先,可以删除或重命名发送到服务器的关于 jqGrid 的 prmNames 选项的任何标准参数,或者添加更多关于 postData 选项(参见 http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options).通过定义 serializeGridData() 函数(jqGrid 的另一个选项),可以在 jqGrid 发出相应的 $.ajax 请求之前立即修改所有构造参数.不仅如此,还可以通过设置 jqGrid 的 ajaxGridOptions 选项来更改每个 $.ajax 参数.例如,我使用 ajaxGridOptions: {contentType: "application/json"} 作为 $.jgrid.defaults 的一般设置.ajaxGridOptions 选项非常强大.关于 ajaxGridOptions 选项,可以重新定义 jqGrid 发送的 $.ajax 请求的任何参数,例如 errorcompletebeforeSend 事件.我认为定义 dataFilter 事件可能很有趣,以便能够对从服务器返回的行数据进行任何修改.

First of all, one can delete or rename any standard parameter sent to the server with respect to the prmNames option of jqGrid or add any more additional parameters with respect to the postData option (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options). One can modify all constructed parameters immediately before jqGrid makes the corresponding $.ajax request by defining of the serializeGridData() function (one more option of jqGrid). More than that, one can change every $.ajax parameter by setting the ajaxGridOptions option of jqGrid. I use ajaxGridOptions: {contentType: "application/json"} for example as a general setting of $.jgrid.defaults. The ajaxGridOptions option is very powerful. With respect to the ajaxGridOptions option one can redefine any parameter of $.ajax request sending by jqGrid, like error, complete and beforeSend events. I see potentially interesting to define dataFilter event to be able to make any modification of the row data returned from the server.

使用 trigger('reloadGrid') 方式的另一个理由是在 AJAX 请求处理期间阻塞 jqGrid.大多数情况下,我使用参数 loadui: 'block' 在向服务器发送 JSON 请求期间阻止 jqGrid.关于 jQuery blockUI 插件 http://malsup.com/jquery/block/ 可以阻止网页的更多部分仅作为网格.要做到这一点,可以调用:

One more argument for the use of the trigger('reloadGrid') way is blocking of jqGrid during the AJAX request processing. Mostly I use the parameter loadui: 'block' to block jqGrid during JSON request sending to the server. With respect to jQuery blockUI plugin http://malsup.com/jquery/block/ one can block more parts of web page as the grid only. To do this one can call:

jQuery('#main').block({ message: '<h1>Die Daten werden vom Server geladen...</h1>' });

在调用 trigger('reloadGrid') 方法和 jQuery('#main').unblock() 之前在 loadCompleteloadError 函数.在这种情况下,loadui 选项可以设置为禁用".

before calling the trigger('reloadGrid') method and jQuery('#main').unblock() inside the loadComplete and loadError functions. The loadui option could be set to 'disable' in this case.

我的最后一句话:大多数情况下,我使用 datatype 设置为 'local' 而不是 'json' 创建 jqGrid,我会调用 trigger('change') 某些控件(组合框之一)的功能,例如:jQuery("#selector").change(myRefresh).keyup(myKeyRefresh).trigger('change').因此,我仅在更改句柄内的一个位置构造 jqGrid 的 url 参数,并将上述 setGridParam() 内的 datatype 更改为 'json'em>.

And my last remark: Mostly I used to create jqGrid with the datatype set to 'local' instead of 'json' and I would call the trigger('change') function of some of the controls (one of the comboboxes) like: jQuery("#selector").change(myRefresh).keyup(myKeyRefresh).trigger('change'). Thus I construct the url parameter of jqGrid only in one place inside of the change handle and change datatype to 'json' inside the above described setGridParam().

所以我不明白为什么应该使用函数 addJSONData().

So I don’t see why the function addJSONData() should be ever used.

使用 addJSONData() 函数的人可以向我解释一下使用它的优点吗?

Can somebody who uses addJSONData() function explain to me the advantages of its usage?

公平地说,我可以添加旧版本的 jqGrid 中存在的 addJSONData(),因为它具有我在此处描述的大部分功能.是否应该将 jqGrid 的 addJSONData 的用法替换为 setGridParam()trigger('reloadGrid') 的用法?

To be fair I can add that addJSONData() which exists in older versions of jqGrid as having most of the features which I describe here. Should one replace the usage of addJSONData of jqGrid to the usage of setGridParam(), and trigger('reloadGrid')?

推荐答案

我一直在使用 addJSONData 和 jqgrid,但那是 1 年前,从那时起 jqGrid 发生了很多变化.

I've been using addJSONData with jqgrid, but it was 1 year ago, a lot of things have change since that time in jqGrid.

无论如何,我需要沉重的 &客户端复杂的 gui 操作(银行共享的东西),我的 Json 数据只是本地的,并作为一些 jkey 点发送到服务器(工作完成).我有几个 jqgrid(其中一些在其他 jqgrids 中:-) )和某种本地浏览器存储的数据,这些数据足够小,可以留在浏览器中,而且足够复杂,可以通过 ajax IO 在合理的时间内无法使用.

Anyway, I needed heavy & complex gui manipulation on the client side (bank share things), my Json data was local only and sent to the server as some jkey point (job finished). I had several jqgrid (some of them inside others jqgrids :-) ) and some sort of local browser storage of data which was small enough to stay in the browser and complex and moving enough to be unusable in a reasonnable time via ajax IO.

第一个版本使用 Ajax IO,当我遇到锁和等待问题以及新的复杂 GUI 事物的数量时,我真的很高兴找到这个 addJSONData 挂钩并在外面拥有自己的 ajax 时间线jQgrid.

First version were using Ajax IO, when I've been hit by the locks and wait problems and by the amount of new complex GUI things coming I've been really happy to find this addJSONData hook and have my own ajax timeline outside of jQgrid.

这篇关于是否应该将 jqGrid 的 addJSONData 的用法替换为 setGridParam() 和 trigger('reloadGrid') 的用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆