3

I'm having some issues setting the url of the jqgrid using setGridParam.

I receive the message: "f is undefined".

My setup:

       $("#prices").jqGrid({
    colModel: [
           ...
        ],
    pager: jQuery('#pricePager'),
    ajaxGridOptions: { contentType: "application/json" },
    mtype: 'POST',
    loadonce: true,
    rowTotal: 100,
    rowNum: -1,
    viewrecords: true,
    caption: "Prices",
    height: 300,
    pgbuttons: false,
    multiselect: true,
    afterInsertRow: function (rowid, rowdata, rowelem) {
        // ...
    },
    beforeSelectRow: function (rowid, e) {
      // ...
    },
    onSelectRow: function (rowid, status) {
       // ...
    }
}); 

Getting the data:

$("#prices").setGridParam({ datatype: 'json', page: 1, url: '@Url.Action("GridDataPrices")', postData: JSON.stringify(selections) });

$("#prices").trigger('reloadGrid');

The Response is non encoded json:

{"total":1,"page":1,"records":100,"rows":[{"id":160602948,"StartDate":"\/Date(1311717600000)\/","Duration":7,"Price":1076.0000,"Code":"code"},{"id":160602950,...}]}

However, I get following message, using firebug:

"f is undefined"

I got this working first using addJSONData, but had to replace it because I want to preserve the local sorting.

Thanks in advance.

Brecht
  • 355
  • 2
  • 4
  • 7
  • You can try to use `$("#prices").jqGrid('setGridParam', {...});` instead of `$("#prices").setGridParam({...});`. You should replace `rowNum: -1` to `rowNum: 10000` (some large **positive** number). I recommend you additionally to use `jquery.jqGrid.src.js` instead of `jquery.jqGrid.min.js` till you have no errors. In the case the names will be readable (the name "f" say nothing). Additionally I recommend you **never** use `afterInsertRow` and use `gridview:true` instead. It can improve performance of the jqGrid dramatically. – Oleg Jul 25 '11 at 12:47
  • Thanks for your answer. I modified the options and used the source file to locate the exact problem. The message comes more clear: `obj is undefined [Break On This Error] ret = obj[expr];`. Place where this happens: `getAccessor : function(obj, expr)`. Didn't manage to find the cause yet. – Brecht Jul 25 '11 at 13:23
  • The arguments of the function: expr: "setGridParam". The object is apparently the problem, but as this is the first time I explore the source code of the jQgrid, it isn't very clear what it should contain. – Brecht Jul 25 '11 at 13:34
  • Removing the datatype option ( `datatype: 'json'`) makes the error disappear, but the data doesn't get loaded into the grid. – Brecht Jul 25 '11 at 13:48
  • In what line of code the you have the error? You can see this in the debugger (Developer Tools of IE or Visual Studio). The method `getAccessor` will be used for two porpoise: 1) reading JSON data from the server response 2) in cases like `$("#prices").jqGrid('setGridParam',...` in the `$.fn.jqGrid` object. Probably there are an error in the parts of code which you **not included** in your question. – Oleg Jul 25 '11 at 13:52
  • `datatype: 'json'` is the correct option. The problem must be in other place. – Oleg Jul 25 '11 at 13:53
  • line 131. Do you mean the data access layer? I'll upload a some more detailed version together with the json response in a couple of minutes. – Brecht Jul 25 '11 at 14:01
  • Resources can be found here: http://tinyurl.com/3s7u8gt – Brecht Jul 25 '11 at 14:36

1 Answers1

6

After you uploaded the code all will be clear. Your main errors are the follwings:

  • you should include datatype: 'local' in the jqGrid. Default value is 'xml'.
  • the JSON data have named properties so you have to use jsonReader: { repeatitems: false } (see the documentation for details)
  • you use "ArivalCodeWay" in colModel and "ArrivalCodeWay" in the JSON data. So you should fix the name of the corresponding jqGrid column
  • to decode the date from the "\/Date(1312840800000)\/" format you should include formatter:'date' in the corresponding column.
  • In the same way I find good to include formatter:'int', sorttype:'int' in the 'Duration' column and sorttype:'number', formatter:'number', formatoptions: { decimalPlaces:4, thousandsSeparator: "," } in the 'Price' column.
  • if you use JSON.stringify you should include json2.js to be sure that your code will work in all web browsers.

The modified demo (including some other minor changed) you can find here. If you click on "Click me" button the grid contain will be loaded.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • 1
    Oleg, thanks for looking into my issue, really appreciated! The main problem was the JsonReader option which wasn't there. (didn't know of however I use jqgrid frequently). About ´datatype: 'local'´: I believe I read somewhere on stackoverflow that jqgrid automatically sets datatype to local when having loadonce to true. – Brecht Jul 25 '11 at 18:31
  • Oleg, if I may, I have a related question. I use addJsonData to fill some data in a grid. This disables the use of local sorting. I could implement this the same way as above, however I can't do a servercall to get my data as my data is received along with some other data of another component. Is it possible to enable local sorting when using addJsonData? I've read [your post](http://stackoverflow.com/questions/2660226/should-one-replace-the-usage-addjsondata-of-jqgrid-to-the-usage-of-setgridparam) about this matter, but I don't see any other solution than setting the url and reload the grid. – Brecht Jul 26 '11 at 12:20
  • @Brecht: If I understand you correct you can't modify the server code which provide the JSON data. It is not a problem when the JSON data has additional fields. jqGrid will get only the fields which you define in the columns and ignore the rest. Probably you can describe your problem better in the separate question. You can include the test JSON data which you receive from the server and the jqGrid which you use till now. I would try to answer how I would implement the filling jqGrid without the usage of `addJsonData` so that you can use local data paging, sorting and filtering/searching. – Oleg Jul 26 '11 at 12:42
  • [done](http://stackoverflow.com/questions/6831306/load-local-json-data-in-jqgrid-without-addjsonrows) – Brecht Jul 26 '11 at 14:09