为每个jqGrid ajax调用添加参数(Adding parameter to every jqGrid ajax call)

编程入门 行业动态 更新时间:2024-10-10 12:25:25
为每个jqGrid ajax调用添加参数(Adding parameter to every jqGrid ajax call)

当页面上有一个jqGrid时,我想扩展它以传递一个额外的参数。到目前为止我最好的尝试:

... $('[role=grid]').jqGrid('setGridParam', { 'serializeGridData':MyGridOnBeforeRequest }); function MyGridOnBeforeRequest (postData) { var newPostData = $.extend(postData, { specialParam: "foo" }); return $.param(newPostData); } ...

我这样做是因为看起来jqGrid忽略了你对jQuery的ajax处理做的任何改变。

谢谢。

When there is a jqGrid on the page I want to extend it to pass an extra parameter in. My best attempt so far:

... $('[role=grid]').jqGrid('setGridParam', { 'serializeGridData':MyGridOnBeforeRequest }); function MyGridOnBeforeRequest (postData) { var newPostData = $.extend(postData, { specialParam: "foo" }); return $.param(newPostData); } ...

I am doing it this way because it looks like jqGrid ignores any changes you do to jQuery's ajax handling.

Thanks.

最满意答案

通常,您发布的代码应该可以工作,并且在下一个网格重新加载时将使用新的serializeGridData 。

你发布的方式使用serializeGridData只是有点奇怪。 改为使用postData参数就足够了:

var postData = $('#grid').jqGrid('getGridParam', "postData"); $.extend(postData: {specialParam: "foo"});

此外,在您的原始代码中,您使用$.extend(postData, {specialParam: "foo"}); 修改原始的postData 。 这不错,但一般来说代码

function myGridOnBeforeRequest (postData) { return $.extend({}, postData, {specialParam: "foo"}); }

甚至

function myGridOnBeforeRequest (postData) { return $.extend(true, {}, postData, {specialParam: "foo"}); }

会更干净。 实际上并不需要使用$.param 。 如果你返回一个对象,那么jQuery.ajax将自动为你调用$.param 。

另一种非常常见的方法是使用看起来像的postData参数直接定义网格

postData: { specialParam: function () { return "foo"; } }

在您可以在函数中实现更多逻辑的方式中,您可以使用函数体内部外部scote中的一些变量,或者从页面上存在的控件中获取一些值(有关详细信息,请参见此处 )。 在这种情况下,您可以在不使用getGridParam的情况下实现动态 getGridParam 。

最后一句话。 已经从myGridOnBeforeRequest的颜色(从你的问题MyGridOnBeforeRequest它与myGridOnBeforeRequest的颜色进行比较)可以看出它将以另一种方式被解释。 如果函数的名称以大写字母开头,则表示与常用的名称转换相对应,您可以定义新类MyGridOnBeforeRequest 构造 MyGridOnBeforeRequest 。 你必须使用它作为var test = new MyGridOnBeforeRequest(); :使用new 。 只需与var now = new Date(); 。 严格建议保持标准的JavaScript名称转换。

In general the code which you posted should work and at the next grid reloading the new serializeGridData will be used.

It's only a little strange that you use serializeGridData in the way in which you as posted. It's enough to use postData parameter instead:

var postData = $('#grid').jqGrid('getGridParam', "postData"); $.extend(postData: {specialParam: "foo"});

Moreover in your original code you use $.extend(postData, {specialParam: "foo"}); which modify the original postData. It's not bad, but in general the code

function myGridOnBeforeRequest (postData) { return $.extend({}, postData, {specialParam: "foo"}); }

or even

function myGridOnBeforeRequest (postData) { return $.extend(true, {}, postData, {specialParam: "foo"}); }

will be more clean. The usage of $.param is not really needed. If you return an object then jQuery.ajax will make the call of $.param automatically for you.

One more very common way will be to define grids directly with the postData parameter which looks like

postData: { specialParam: function () { return "foo"; } }

In the way you can implement more logic in the function, you can use some variables from the outer scote inside of the body of the function or get some values from the controls existing on the page (see here for details). In the case you can implement dynamic specialParam without the usage of getGridParam.

The last remark. Already from the color of myGridOnBeforeRequest (compare it with the color of MyGridOnBeforeRequest from your question) one can see that it will be interpreted in another way. If the name of the function start with the capital letter it means correspond with common used name conversion that you define constructor of the new class MyGridOnBeforeRequest. You have to use it as var test = new MyGridOnBeforeRequest();: using new. Just compare with var now = new Date();. It is strictly recommended to hold the standard JavaScript name conversion.

更多推荐

本文发布于:2023-08-07 12:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464168.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   ajax   jqGrid   call   parameter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!