是否可以在 javascript 函数中包含事件? [英] is it possible to include an event in a javascript function?

查看:12
本文介绍了是否可以在 javascript 函数中包含事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道从主 javascript 中获取 jqgrid 事件并使用另一个 javascript 以函数形式将其分开是否可行?我想做的就是这样.我有一个代码:

i was just wondering if getting a jqgrid event from a main javascript and separate it using another javascript in a form of function would work? what im trying to do is like this. i have a code :

     ...//some code here
     serializeGridData: function(postData) {
        var jsonParams = {
            'SessionID': $('#eSessionID3').val(),
            'dataType': 'data',
            'recordLimit': postData.rows,
            'recordOffset': postData.rows * (postData.page - 1),
            'rowDataAsObjects': false,
            'queryRowCount': true,
            'sort_fields': postData.sidx
        };

        if (postData.sord == 'desc')
        {
            ...//some code here
        }           
        else
        {
            ...//some code here
        }

        return 'json=' + jsonParams;
    },

    loadError: function(xhr, msg, e) { 
        showMessage('errmsg');
    },
            ...//some code here

我想获取此代码并将其写入另一个 javascript 文件并将其作为一个函数,以便我的另一个文件可以使用这个..可能吗?

i want to get this code and write this in another javascript file and make this as a function, so that my other file could use this one..is it possible?

我在我的其他 javascrtip 文件中创建了类似的东西,我计划在其中放置我的所有函数.这是代码(functions.js):

i created something like this in my other javascrtip file where i planned to put all my functions. here's the code (functions.js):

function serialLoad(){
   serializeGridData: function(postData) {
      var jsonParams = {
         'SessionID': $('#eSessionID3').val(),
         'dataType': 'data',
         'recordLimit': postData.rows,
         'recordOffset': postData.rows * (postData.page - 1),
         'rowDataAsObjects': false,
         'queryRowCount': true,
         'sort_fields': postData.sidx
      };

      if (postData.sord == 'desc')
      {
          ...//some code here
      }           
      else
      {
          ...//some code here
      }
      return 'json=' + jsonParams;
   },

   loadError: function(xhr, msg, e) { 
       showMessage('errmsg');
   }
}

这不起作用并显示消息语法错误.我不知道如何纠正这个.有谁可以帮助我吗?

this isn't working and display a message syntax error. i don't know how to correct this. is there anyone who can help me.?

推荐答案

首先回答你的正确问题.如果你在 functions.js 文件中定义了一些全局变量,例如 myGlobal:

First of all the answer on your derect question. If you define in the functions.js file some global variable, for example, myGlobal:

myGlobal = {};
myGlobal = serializeGridData: function(postData) {
    // ... here is the implementation
};

您可以在另一个 JavaScript 文件中使用它,该文件必须包含在 functions.js 文件之后:

you can use it in another JavaScript file which must be included after the functions.js file:

serializeGridData: myGlobal.serializeGridData

(只需在 jqGrid 定义中使用此类参数).

(just use such parameter in the jqGrid definition).

如果您想将 serializeGridData 参数与大多数 jqGrids 中的值一起使用,您可以覆盖 functions.js 中 serializeGridData 的默认值 文件:

If you want to use the serializeGridData parameter with the value in the most of your jqGrids you can overwrite the default value of serializeGridData in the functions.js file instead:

jQuery.extend(jQuery.jgrid.defaults, {
    datatype: 'json',
    serializeGridData: function(postData) {
        // ... here is the implementation
    },
    loadError: function(xhr, msg, e) { 
        showMessage('errmsg');
    }
});

在示例中,我将默认的 datatype: 'xml' jqGrid 参数重写为 datatype: 'json'.它表明您可以设置任何 jqGrid 参数的默认值.

In the example I ovewride additionally default datatype: 'xml' jqGrid parameter to datatype: 'json'. It shows that in the way you can set default values of any jqGrid parameter.

在我看来,您真正需要的是使用 prmNames jqGrid 参数重命名标准 jqGrid 参数的一些默认名称.例如与

What it seems to me you really need is to use prmNames jqGrid parameter to rename some defaulf names of the standard jqGrid parameters. For example with

prmNames: {
    rows:"recordLimit",
    sort: "sort_fields",
    search:null,
    nd:null
}

您将标准 rows 参数重命名为 recordLimit,将 sidx 重命名为 sort_fields 并删除 _searchnd 参数要发送.

you rename the standard rows parameter to recordLimit, the sidx to sort_fields and remove _search and nd parameters to be send.

此外,您可以使用将一些属性定义为函数的 postData(请参阅 这里了解详情).例如:

Additionally you can use postData having some properties defined as the function (see here for details). For example:

postData: {
    SessionID: function() {
        return $('#eSessionID3').val();
    },
    rowDataAsObjects: false,
    queryRowCount: true,
    dataType: 'data',
    recordOffset: function() {
        var pd = jQuery("#list2")[0].p.postData;
        return pd.recordLimit * (pd.page - 1);
    },
    json: function() {
        var pd = jQuery("#list2")[0].p.postData;
        return {
           SessionID: $('#eSessionID3').val(),
           dataType: 'data',
           recordOffset: pd.recordLimit * (pd.page - 1),
           rowDataAsObjects: false,
           queryRowCount: true,
           sort_fields: pd.sort_fields
        };
    }
}

我在这里使用了您当前使用的 json 参数,并在参数列表中直接添加 SessionIDqueryRowCount 等参数这将被发送.当然,只发送一种方式(json 或其他方式)来发送您需要的附加信息就足够了.

I used here both json parameter which you currently use and add parameters like SessionID, queryRowCount and so on directly in the list of parameters which will be send. Of course it is enough to send only one way (either json or the rest) to send the aditional information which you need.

这篇关于是否可以在 javascript 函数中包含事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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