1

what kind of json reader I need to plot data like these in a jqgrid?

Thanks!

Oleg
  • 220,925
  • 34
  • 403
  • 798
user781065
  • 47
  • 1
  • 8

1 Answers1

0

You have strange questions and all about the jsonReader. In the current case you can use

jsonReader: {
    root: 'features',
    repeatitems: false
}

to read the data. The demo shows how the results can looks like:

enter image description here

UPDATED: How I understand, what you want really to do is to call some external URL which provide you back the JSON. Standard Ajax request can't be done to another server because of security reasons (see same origin policy). Fortunately the server sampleserver1.arcgisonline.com/ArcGIS supports JSONP requests. So to fill the grid with external data you can use the following code

$('#grid').jqGrid({
    url: 'http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/4/query',
    datatype: 'jsonp',
    postData: $.param({
        where: "1=1",
        returnGeometry: false,
        outFields: "ObjectID,NAME,STATE_NAME,CNTY_FIPS",
        f: "json"
    }),
    colModel: [
        {name: 'ObjectID', label: 'ID', width: 60, jsonmap: 'attributes.ObjectID'},
        {name: 'NAME', label: 'Name', width: 150, jsonmap: 'attributes.NAME'},
        {name: 'STATE_NAME', label: 'State', width: 120, jsonmap: 'attributes.STATE_NAME'},
        {name: 'CNTY_FIPS', label: 'FIPS', width: 60, jsonmap: 'attributes.CNTY_FIPS'}
    ],
    toppager: true,
    jsonReader: {
        root: 'features',
        repeatitems: false
    },
    loadonce: true,
    ignoreCase: true,
    height: 'auto'
});

See the new demo here.

UPDATED 2: To be able to use local searching/filtering one should modify the above code. It's better to replace postData which one sees above to the following parameters

ajaxGridOptions: { cache: true },
prmNames: {search: null, nd: null, sort: null, rows: null, order: null, page: null},
postData: {
    where: "1=1",
    returnGeometry: false,
    outFields: "ObjectID,NAME,STATE_NAME,CNTY_FIPS",
    f: "json"
}

See the corresponding demo where filterToolbar works.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks Oleg. My job is about GIS and web development, an important part of this is to render geospatial data in a grid, so I think my questions are not so strange... Probably I have to read the json reader documentation more accurate. Thank you again – user781065 Mar 06 '12 at 21:32
  • @user781065: You are welcome! I forgot to mention, that I used additionally `jsonmap` like `jsonmap: 'attributes.NAME'` in the answer. The official documentation you can find [here](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data). With respect of [jsonReader as function](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#jsonreader_as_function) one can read practically any JSON input. Additionally sometimes are helpful to use `beforeProcessing` callback. If the problem is solved you can mark the answer as "accepted". – Oleg Mar 06 '12 at 21:44
  • Oleg, did you try with the url I supplied in the first post? Using it the grid is always empty and firebug return status 200 OK for the request, but in red color... – user781065 Mar 07 '12 at 10:56
  • @user781065: Which `URL` you mean? Which your post you mean? This one or your first question on the stackoverflow? In my demo I used the exact copy of your data. – Oleg Mar 07 '12 at 11:23
  • Hi Oleg, look at the demo here: dl.dropbox.com/u/1070973/jqgrid_reader/index2.html – user781065 Mar 07 '12 at 12:01
  • @user781065: What you want can't be done with JSON requests, but one can use JSONP in the case. See my updated answer. – Oleg Mar 07 '12 at 12:36
  • thank you Oleg, I was managing to write a proxy in the meantime, but jsonp is a better solution. – user781065 Mar 07 '12 at 15:11
  • Hi, I need a little help to understand how to add a filterToolbar to this grid. I tried this: `$("#grid").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false });` but it doesn't work. – user781065 Apr 23 '13 at 14:01
  • @user781065: What exactly "not works"? Which `datatype` you use? Do you use `loadonce: true` so that searching will be local even if you use remote data? You should post more full description about your problem in new question. – Oleg Apr 23 '13 at 14:09
  • Hi Oleg, datatype is jsonp (like in the example) and loadonce is set to true, but when I type inside a filter field nothing happens. – user781065 Apr 23 '13 at 14:27
  • @user781065: I analysed the problem. One should change `postData` parameter how I described in **UPDATED 2** of my answer. – Oleg Apr 23 '13 at 14:54