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

查看:100
本文介绍了是否可以在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 file:

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 em>文件:

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

在示例中,ovewride另外默认为 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参数的一些defaulf名称。例如,使用

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
}

你将标准行参数重命名为 recordLimit ,将 sidx sort_fields 并删除 _search nd 要发送的参数。

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 参数,您直接在要发送的参数列表。当然,只发送一种方式( 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天全站免登陆