dr0zd
dr0zd

Reputation: 1378

jqGrid json data

Hello I have an example that works properly:

   $(function() { 
      $("#treegrid").jqGrid({ 
          url: 'tree2.json', 
          datatype: 'json', 
          mtype: 'GET', 
          colNames: ["ID", "Description", "Total"], 
          colModel: [{ 
              name: 'id', 
              index: 'id', 
              width: 1, 
              hidden: true, 
              key: true 
          }, { 
              name: 'desc', 
              index: 'desc', 
              hidden: false, 
              sortable: true 
          }, { 
              name: 'num', 
              index: 'num', 
              hidden: false, 
              sortable: true 
          }], 
          height: 'auto', 
          width: '500', 
          pager: "#ptreegrid", 
          caption: "Tree Grid Example" 
      }) 
  }); 

JSON data

{ 
    "page": 1, 
    "total": 1, 
    "records": 2, 
    "rows": [ 
       {"id": 1, "cell": ["1", "Source 1", "300"]}, 
       {"id": 2, "cell": ["2", "Source 2", "100"]}
    ] 
} 

How to order jqGrid to read this type of JSON data? Is it possible?

{ 
    "page": 1, 
    "total": 1, 
    "records": 2, 
    "rows": [ 
       {"id": 1, "cell": {"id":"1", "desc":"Source 1", "num":"300"}}, 
       {"id": 2, "cell": {"id":"2", "desc":"Source 2", "num":"100"}}
    ] 
} 

That is all Don't read next lines

Sorry, this is some text to go through alerts:) Oops! Your question couldn't be submitted because:

Your post does not have much context to explain the code sections; please explain your scenario more clearly.

================ Thanks fo help My final version is taken from your answers too

          jsonReader: {
              repeatitems: false,
          root: function (obj) { return obj; },
          page: function (obj) { return 1; },
          total: function (obj) { return 1; },
          records: function (obj) { return obj.length; }                  
          }

And I've changed JSON data

[ 
    {"id":"1", "desc":"Source 1", "num":"300"}, 
    {"id":"2", "desc":"Source 2", "num":"100"}
]

Thanks a lot!!!

Upvotes: 0

Views: 5956

Answers (2)

dr0zd
dr0zd

Reputation: 1378

Thanks fo help My final version is taken from your answers too

          jsonReader: {
              repeatitems: false,
          root: function (obj) { return obj; },
          page: function (obj) { return 1; },
          total: function (obj) { return 1; },
          records: function (obj) { return obj.length; }                  
          }

And I've changed JSON data

[ 
    {"id":"1", "desc":"Source 1", "num":"300"}, 
    {"id":"2", "desc":"Source 2", "num":"100"}
]

Thanks a lot!!!

Upvotes: 0

Oleg
Oleg

Reputation: 222017

Probably you mean the format of data:

{ 
    "page": 1, 
    "total": 1, 
    "records": 2, 
    "rows": [ 
       {"id":"1", "desc":"Source 1", "num":"300"}, 
       {"id":"2", "desc":"Source 2", "num":"100"}
    ] 
}

without duplication of id and the unneeded cell property. To read the data you need include jsonReader: {repeatitems: false} option of jqGrid (see the documentation).

Upvotes: 2

Related Questions