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

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

问题描述

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

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

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

  1. 进行$.ajax()手册的调用,然后在成功或$.ajax的完整句柄内部调用jQuery.parseJSON()(或eval),然后调用jqGrid的 addJSONData 函数.我在stackoverflow.com上找到了许多使用 addJSONData 的示例.
  2. 根据用户输入
  3. 更新jqGrid的 url ,将当前页面编号重置为1,并可选地更改网格的标题.所有这些操作都可以针对 setGridParam()和可选的 setCaption() jqGrid方法完成.最后,调用网格的 trigger('reloadGrid')函数.为了构造 url ,我主要使用jQuery.param函数来确保相对于 encodeURIComponent 正确包装了所有url参数.

我希望我们讨论两种方式的优缺点.我目前使用第二种方式,因此我将从这种方式的优势开始.

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

好的,我会选择另一种方式. jqGrid可以直接在Web服务上进行调用,并将结果填充在网格内.有很多jqGrid选项,可用于自定义此过程.

首先,可以针对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请求的任何参数,例如 error complete beforeSend 事件.我认为定义 dataFilter 事件可能很有趣,以便能够对从服务器返回的行数据进行任何修改.

使用 trigger('reloadGrid')方法的另一个参数是在AJAX请求处理期间阻塞jqGrid.通常,在将JSON请求发送到服务器期间,我通常使用参数loadui: 'block'来阻止jqGrid.关于jQuery blockUI插件 http://malsup.com/jquery/block/可以阻止网页的更多部分仅作为网格.为此,可以致电:

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

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

最后一句话:通常,我通常使用数据类型设置为'local'而不是'json'来创建jqGrid,然后将其称为 trigger('change')某些控件(组合框之一)的功能,例如: jQuery("#selector").change(myRefresh).keyup(myKeyRefresh).trigger('change'). 因此,我仅在更改句柄内部的某个位置构造jqGrid的 url 参数,并将上述 setGridParam()内的 datatype 更改为'json'.

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

使用 addJSONData()函数的人可以向我解释其用法的优点吗?

为了公平起见,我可以添加存在于jqGrid较早版本中的 addJSONData(),因为它具有我在此描述的大多数功能.是否应该将jqGrid的 addJSONData 的用法替换为 setGridParam() trigger('reloadGrid')的用法?

解决方案

我一直在jqgrid中使用addJSONData,但那是1年前,自那时以来,jqGrid中发生了很多变化.

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

第一个版本使用的是Ajax IO,当我受到锁和等待问题以及即将出现的新的复杂GUI问题的困扰时,我非常高兴找到这个addJSONData钩子并在外面拥有自己的ajax时间轴的jQgrid.

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.

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.

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

  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.

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!

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.

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.

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>' });

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.

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().

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

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

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')?

解决方案

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

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.

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天全站免登陆