1

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

jayAnn
  • 827
  • 3
  • 18
  • 38

3 Answers3

2

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.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • wait...ok.. i think i should study your code... i believe this can help me... thank you for the help and your time... im new in jqgrid and json and i really appreciate your effort. i hope i can have a mind like yours Oleg...") – jayAnn Mar 21 '11 at 09:18
  • @jayAnn: You are welcome! If you will receive some additional question about the code later you can ask here your questions. – Oleg Mar 21 '11 at 09:20
  • i've been reading your code many times and i still can't get it right...i see it so complicated. i dont know where to write the jQuery.extend({....}) (in main javascript or in the functions.js), and the postdata, idon't know where should i insert it and where should i write it.. thank you oleg:) and sorry if i can't hardly understand... – jayAnn Mar 22 '11 at 03:14
  • 1
    @jayAnn: You can do following: in the functions.js use `jQuery.extend(jQuery.jgrid.defaults, ...)` to change any default parameters of jqGrid. In the way you can set `prmNames` or `serializeGridData` for example. Then in the main script you define jqGrid having no `serializeGridData` parameter, In the case as the default parameter will be used values from `jQuery.jgrid.defaults` which you changed in the functions.js. – Oleg Mar 22 '11 at 07:14
  • @jayAnn: Helped this? Probably I just try to write an code example as an answer on your next question. – Oleg Mar 22 '11 at 10:09
  • this help me, you help me... one more question. if i'm going to write another program that will using jqgrid, can this functions.js also be used? and also to my next, next jqgrid program? – jayAnn Mar 23 '11 at 02:20
0

You are mixing function declaration and object literal notation. This syntax: property: value is used when creating an object with object literal notation:

var obj = {
   prop: val,
   prop2: val
};

serializeGridData and loadError are properties of some object and you cannot define those by just putting them into a function.

One way would be to create two functions, one for serializeGridData and one for loadError, e.g.

function serialLoad(postData){
  var jsonParams = {
     //...
  };

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

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

Then you can assign them in your other file to the object:

// ... some code here
serializeGridData: serialLoad,
loadError: onError,
//... some code here

Another way is to pass the object in question to the function and assign the properties there:

function attachLoadHandler(obj) {
    obj.serializeGridData = function(postData) {
        //...
    };

    obj.loadError = function(xhr, msg, e) {
        //...
    };
}

Then you have to pass the object you created to that function:

attachLoadHandler(obj);

But I think the first approach is easier to understand.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • thank you for the reply, but it didn't work...it didn't display my grid anymore on load.. – jayAnn Mar 21 '11 at 08:16
  • there's no error on the console.... and yes, i never forgot to include my functions.js... – jayAnn Mar 21 '11 at 08:32
  • @jayAnn: I updated my answer, the declaration of the `onError` function was wrong. – Felix Kling Mar 21 '11 at 08:37
  • you're first example shows an error "missing ( before formal parameters [Break on this error] function onError: function(xhr, msg, e) { \n" – jayAnn Mar 21 '11 at 08:38
  • @jayAnn: Yep, I already corrected it. Check my update and my comments. – Felix Kling Mar 21 '11 at 08:40
  • there's an error message "invalid label [Break on this error] obj.serializeGridData: function(postData) {\n" – jayAnn Mar 21 '11 at 08:49
  • @jayAnn: It seems it should be `obj.serializeGridData = function(postData)`... but it is correct in my code. Be sure you use the code I posted correctly. – Felix Kling Mar 21 '11 at 08:51
  • yes, thank you... there's no error now but it still didn't show my grid on load... – jayAnn Mar 21 '11 at 09:06
0

The second example is incorrect, as you are declaring a javascript object as the body of a function, what you could do is:

function serialLoad() {
    // Return an object with the required members
    return {
        serializeGridData: function(postData) { ... },
        loadError: function(xhr, msg, e) { ... }
    };
}
Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129