Forum

November 2nd, 2014
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_TopicIcon
autodetection of input JSON format
28/02/2013
12:33
Avatar
OlegK
Germany
Member
Members
Forum Posts: 1255
Member Since:
10/08/2009
sp_UserOfflineSmall Offline

Hello Tony,

one common error of newcommer is the usage of wrong JSON format or incorrect usage of jsonReader. Two formats are very common:

1) jsonReader: {repeatitems: false}. One produces the data in the format, but don't includes jsonReader: {repeatitems: false} as jqGrid option. One could include simple test whether items which represent the row as array or not. Directly after reading of root element (see here) or later one can include testing of the type of the row and to use either the reader from array or the reader from named object.

2) jsonReader: {repeatitems: false, root: function (obj) { return obj; }}, loadonce: true. The case is very common especially for newcommer or for the case when somebode try to include jqGrid in exsiting web project. One can test directly at the beginning of addJSONData whether the input data parameter is array. In the case one can use root defined as the function before.

It seems to me that introduction of autodetection of input format of JSON data could be very practical feature of jqGrid from the user's point of view.

Best wisches
Oleg 

05/03/2013
15:06
Avatar
OlegK
Germany
Member
Members
Forum Posts: 1255
Member Since:
10/08/2009
sp_UserOfflineSmall Offline

Just one remark. The simple answer about the usage of

jsonReader : { repeatitems: false }

has high score. Just now it was increated to one more point. It means that one more peaple made the same error, have received exception and spend his time in searching of solution of the problem. Exactly the fact pushed me to post the feature request about dynamical detection of the input format. Only if the used repeatitems value is definitive wrong one can change it and be able do load the data instead of producing exception and showing "loading..." message forever.

Rest regards
Oleg 

06/03/2013
10:01
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

Hello Oleg,

This good recommendation, but I do not see a needed place where we can do this.

look here and here  and then here

Regards

For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.

06/03/2013
12:48
Avatar
OlegK
Germany
Member
Members
Forum Posts: 1255
Member Since:
10/08/2009
sp_UserOfflineSmall Offline

Hello Tony,

to be more specific I made some relatively simple modification of jquery.jqGrid.src.js which you can find here. It is not final code and I modified only code of addJSONData, but it shows how the implementation could be. All the following demos use almost the same code

$('#list').jqGrid({
    url: 'Dynamic0.json',
    datatype: 'json',
    colNames: ['Name', 'Street', 'City', 'Id'],
    colModel: [
        { name: 'Id', width: 55, align: 'center', key: true},
        { name: 'Name', width: 120 },
        { name: 'Street', width: 190 },
        { name: 'City', width: 90 }
    ],
    height: "auto",
    gridview: true
}); 

where only the url parameter will be changed from 'Dynamic0.json' till 'Dynamic4.json'. How you can see there (demo0, demo1, demo2, demo3, demo4) are no jsonReader parameter in the grid, but all different JSON data will be successfully read and displayed.

Dynamic0.json: (standard JSON)

{"page":1,
"total":37,
"records":722,
"rows":
[
    {"id":1,"cell":[1, "Sample name 1","Sample Street 2","Sample City 3"]},
    {"id":2,"cell":[2, "Sample name 2","Sample Street 2","Sample City 3"]}
]

Dynamic1.json: (JSON which typically require jsonReader: {cell: ""})

{"page":1,
"total":37,
"records":722,
"rows":
[
    [1, "Sample name 1","Sample Street 2","Sample City 3"],
    [2, "Sample name 2","Sample Street 2","Sample City 3"]
]

Dynamic2.json: (JSON which typically require jsonReader: {cell: "", root: function (obj) {return obj;}})

[
    [1, "Sample name 1","Sample Street 2","Sample City 3"],
    [2, "Sample name 2","Sample Street 2","Sample City 3"]
]

Dynamic3.json: (JSON which typically require jsonReader: {repeatitems: false, root: function (obj) {return obj;}})

[
    {"Id":1, "Name":"Sample name 1", "Street":"Sample Street 2", "City":"Sample City 3"},
    {"Id":2, "Name":"Sample name 2", "Street":"Sample Street 2", "City":"Sample City 3"}

Dynamic4.json: (JSON which typically require jsonReader: {repeatitems: false})

{
"page":1,
"total":37,
"records":722,
"rows":[
    {"Id":1, "Name":"Sample name 1", "Street":"Sample Street 2", "City":"Sample City 3"},
    {"Id":2, "Name":"Sample name 2", "Street":"Sample Street 2", "City":"Sample City 3"}
]
}

Even more complex exotical JSON data like

[
    [1, "Sample name 1","Sample Street 2","Sample City 3"],
    {"Id":2, "Name":"Sample name 2", "Street":"Sample Street 2", "City":"Sample City 3"}
]

(see demo5) will be successfully readed.

As I wrote before the code from jquery.jqGrid.src-dynamicReader.js is still not final. One should make some changes to read the last part of the data in case of usage loadonce: true, make close modifications to set pager to possible the best values, make modifications of addXmlData… Nevertheless I hope you understand which modification I try to suggest.

If you want I can post modifications from jquery.jqGrid.src-dynamicReader.js as pull request.

Best regards
Oleg

22/03/2013
14:00
Avatar
OlegK
Germany
Member
Members
Forum Posts: 1255
Member Since:
10/08/2009
sp_UserOfflineSmall Offline

Hello Tony,

I posted suggested changes as the pull request.

Best regards
Oleg 

22/03/2013
17:00
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

Thanks Oleg,

I will check this during the weekend.

Best Regards

Tony

For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
40 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

OlegK: 1255

markw65: 179

kobruleht: 144

phicarre: 132

YamilBracho: 124

Renso: 118

Member Stats:

Guest Posters: 447

Members: 11373

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 8

Topics: 10592

Posts: 31289

Newest Members:

, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information