Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

is it possible to include an event in a javascript function?

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

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?

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.?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

serializeGridData: myGlobal.serializeGridData

(just use such parameter in the jqGrid definition).

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

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.

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
}

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...