-1

I have implemented jqgrid as.,

$(function () {
    $("#accountHeadList").jqGrid({
        //url: '../../jqGridHandler1.ashx',
        url: '/Personalize/GetAccountHeads',
        datatype: 'json',
        width  : 400,
        height : 300, 
        colNames: ['name', 'value'],
        colModel: [
                { name: 'name', width: 100, sortable: true },
                { name: 'value', width: 100, sortable: true },
        ],
        rowNum: 10,
        rowList: [10, 20, 30],
        pager: '#gridPager',
        viewrecords: true,
        sortorder: 'asc',
        multiselect: true,
        caption: 'Account Heads',         
    });

    $("#accountHeadList").jqGrid('navGrid', '#gridPager', { edit: false, add: false, del: false });
});

When I return a JSON as.,

{"rows":[{"id":0,"cell":["Accounts Receivable",""]},{"id":1,"cell":["Acd",""]},{"id":2,"cell":["Bank Accounts",""]},{"id":3,"cell":["Cash On Hand",""]},{"id":4,"cell":["Income Tax Payable",""]},{"id":5,"cell":["Interest Payable",""]},{"id":6,"cell":["Investment Premium Payable",""]},{"id":7,"cell":["Prepaid Expenses",""]},{"id":8,"cell":["Salary \u0026 Wage Payable",""]},{"id":9,"cell":["Salary Receivable",""]},{"id":10,"cell":["Short Term Borrowing",""]}],"page":1,"total":1,"records":10}

It is parsing well And I am getting data in the grid. Where as., If I pass the data as below., It fails to load data, simply empty tabular column appears in the Grid,(but with a same no of rows).

{"rows":[{"id":0,"cell":{"name":"Accounts Receivable","value":null}},{"id":1,"cell":{"name":"Acd","value":null}},{"id":2,"cell":{"name":"Bank Accounts","value":null}},{"id":3,"cell":{"name":"Cash On Hand","value":null}},{"id":4,"cell":{"name":"Income Tax Payable","value":null}},{"id":5,"cell":{"name":"Interest Payable","value":null}},{"id":6,"cell":{"name":"Investment Premium Payable","value":null}},{"id":7,"cell":{"name":"Prepaid Expenses","value":null}},{"id":8,"cell":{"name":"Salary \u0026 Wage Payable","value":null}},{"id":9,"cell":{"name":"Salary Receivable","value":null}},{"id":10,"cell":{"name":"Short Term Borrowing","value":null}}],"page":1,"total":1,"records":10}

I could see the difference of {} and [] in the cell, which may be the source of fault. But Why so?

Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77

1 Answers1

1

The server should either returns the data in the default format in which jqGrid expect the data or you should inform jqGrid about the format of your data using jsonReader.

In the feature request and in the pull request are already part of jqGrid. So if you use current version of jqGrid then many (but not all) wrong formatted input data could be still read by jqGrid. In any way it's important to know which version of jqGrid you use.

If you prefer to use object form of data in JSON response from the server then the server should return rows part of the answer in the form

"rows":[
    {"id":0,"name":"Accounts Receivable","value":null},
    {"id":1,"name":"Acd","value":null},
    {"id":2,"name":"Bank Accounts","value":null},
    ...
 ]

("cell" property should be removed). You will have to add jsonReader: {repeatitems: false} additionally if you use old version of jqGrid which not support autodetection of JSON input data.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I have `public struct JQGridResults{public int currentPage { get; set; } public int totalNoOfPages { get; set; } public int totalNoOfRecords { get; set; } public JQGridRow[] rows;}public struct JQGridRow { public long id { get; set; }public string name { get; set; } public string value { get; set; }}` which I will use in JQGrid. But to improve resuablity (because different classes will have different no of data members)., **can I use** `public struct JQGridRow{public long id { get; set; }public string[] cells { get; set; }}`. Because in your answer you said "cell" should be removed!! – Muthu Ganapathy Nathan Jun 09 '13 at 12:24
  • @EAGER_STUDENT: You don't need define any struct of class which you share. C# allows you to use *anonymous* classes: `return Json(new {page = ..., rows = (from item in acHeadEntries select new[] {...}).ToList()}`. You can use `select new {id = item.id, ...} ` in the same way. – Oleg Jun 09 '13 at 12:33
  • This way?? http://stackoverflow.com/questions/13087214/i-want-a-jqgrid-class-that-can-return-json-by-mvc#13088378., But you said using `cells` is not advisable. But they are using that. – Muthu Ganapathy Nathan Jun 09 '13 at 12:40
  • @EAGER_STUDENT: Sorry, but way I wrote is another. I wrote that the items of `rows` should either `{"id":0,"cell":["Accounts Receivable",""]}` or `{"id":0,"name":"Accounts Receivable","value":null}` but not a **mix from both**: `{"id":0,"cell":{"name":"Accounts Receivable","value":null}}` which you currently use. – Oleg Jun 09 '13 at 12:55
  • it is not 'they'. Actually you are using that :) Its your answer. And you missed `new` keyword near select in the above answer http://stackoverflow.com/questions/13087214/i-want-a-jqgrid-class-that-can-return-json-by-mvc#13088378 – Muthu Ganapathy Nathan Jun 09 '13 at 12:56
  • @EAGER_STUDENT: Thanks! I included missing `new` in [the answer](http://stackoverflow.com/a/13088378/315935). I wrote the code of the answer without testing it. The answer should generate the items `{"id":...,"cell":[...]}` like in [the answer](http://stackoverflow.com/a/5501644/315935) too. – Oleg Jun 09 '13 at 15:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31487/discussion-between-eager-student-and-oleg) – Muthu Ganapathy Nathan Jun 09 '13 at 18:35