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_Related Related Topics sp_TopicIcon
Issue Sorting Json in jqGrid 3.6.1
03/09/2010
23:17
Avatar
Neb Okla
Columbus, OH, USA
Member
Members
Forum Posts: 10
Member Since:
03/09/2010
sp_UserOfflineSmall Offline

I've been given a task to fix a broken sort for primary school grade levels in the US.

Students begin school in Kindergarten (represented in the grid as KG), then advance to 1st, 2nd, 3rd grades etc. until they reach 11th and 12th grades.

Presently, sorting on the "Grade Level" column results in a sort order like this:

1

11

12

9

KG

It should be sorted as follows:

KG

1

9

11

12

I've implemented similar grids elsewhere with a datatype of 'local'.  Setting a sorttype of 'int' seems to be working there with regard to KG, 1st, and 2nd grades (though I still need to test with grades 11 and 12).

It seemed obvious to set sorttype to 'int' to try to fix this issue, but it seems to have no effect (though I have verified that the sorttype is specified appropriately in the colModel.

I intend to explore this in more detail but I figured that getting the idea out there over our long holiday weekend might generate some ideas to point me in the right direction when I return to this issue on Tuesday.

The datatype of the code I'm attempting to fix is 'json'.  I'll include a sample below:

jQuery("#list").jqGrid({

url: '/Home/StudentGridData/',

datatype: 'json',

mtype: 'POST',

colNames: ['ID','First Name','Last Name', 'Status','Grade Level', 'Enroll Date'],

colModel: [

{ name: 'ID', index: 'ID', width: 60, align: 'left' },

{ name: 'FirstName', index: 'FirstName', width: 150, align: 'left'},

{ name: 'LastName', index: 'LastName', width: 150, align: 'left'},

{ name: 'Status', index: 'Status', width: 200, align: 'left'},

{ name: 'GradeLevel', index: 'GradeLevel', width: 100, align: 'left', sorttype: 'int'},

{ name: 'EnrollDate', index: 'EnrollDate', width: 150, align: 'left'}],

pager: jQuery('#pager'),

rowNum: 25,

rowList: [5, 10, 20, 50],

sortname: 'ID',

sortorder: "desc",

viewrecords: true,

width:'70%',

caption: 'Student',

postData: {'ID':" ",'firstName':"ALL", 'lastName':"ALL"}

} );

When I discover a solution I'll post it here.

07/09/2010
22:28
Avatar
Neb Okla
Columbus, OH, USA
Member
Members
Forum Posts: 10
Member Since:
03/09/2010
sp_UserOfflineSmall Offline

I've updated the code as follows but I'm still having trouble with the 'GradeLevel' field sort. It's still sorting like text even though I set it to sort as int.

jQuery("#list").jqGrid({
    url: '/Home/StudentGridData/',
    datatype: 'json',
    loadComplete: function () {
        jQuery("#gridid").jqGrid('setGridParam', { datatype: 'local' });
    },
    onPaging: function (which_button) {
        jQuery("#gridid").jqGrid('setGridParam', { datatype: 'json' });
    },
    mtype: 'POST',
    colNames: ['ID','First Name','Last Name', 'Status','Grade Level','Enroll Date'],
    colModel: [
                        { name:ID',index:'ID',width:60,align:'left',sorttype:'int'},
                        { name:'FirstName',index:'FirstName',width:150,sorttype:'text',align:'left'},
                        { name:'LastName',index:'LastName',width:150,sorttype:'text',align:'left'},
                        { name:'Status',index:'Status',width:200,sorttype:'text',align:'left'},
                        { name:'GradeLevel',index:'GradeLevel',width:100,sorttype:'int',align:'left'},
                        { name:'EnrollmentDate',index:'EnrollmentDate',width:150,sorttype:'date',datefmt:'d/m/Y',align:'left'}],
    rowNum: 25,
    rowList: [5, 10, 20, 50],
    height: 'auto',
    pager: jQuery('#pager'),
                sortname: 'ID',
    viewrecords: true,
    sortorder: "desc",
    caption: 'Student',
    width: '70%',
    postData: {'ID':" ",'firstName':"ALL", 'lastName':"ALL"}

   } );

07/09/2010
23:11
Avatar
OlegK
Germany
Member
Members
Forum Posts: 1255
Member Since:
10/08/2009
sp_UserOfflineSmall Offline

I recommend you to use jqGrid 3.7.2 or jqGrid 3.8 beta from http://github.com/tonytomov/jq.....d/tree/dev. Nobody (probably only Tony) have the version or will be test the retro version specially for you.

In the current version there there are nice parameter loadonce:true. It switch datatype: 'json' to datatype: 'local' for you. Moreover in the current version there are full local sorting, paging and searching support. So your server can just send back the whole data.

Moreover local sorting and server side paging is not logical, because sorting are means through all data and not inside a page only. If you already implemented server side paging, that server side sorting will be only some lines of cod (probably only one line).

If you will have problem with the usage of last version or find a bug you will be quickly receive help from developers and from community. Why you should invest your time in tricks with the very old version? A lot of problems and improvement are alredy exist in the new version.

Best regards
Oleg

08/09/2010
22:49
Avatar
Neb Okla
Columbus, OH, USA
Member
Members
Forum Posts: 10
Member Since:
03/09/2010
sp_UserOfflineSmall Offline

OlegK said:

I recommend you to use jqGrid 3.7.2 or jqGrid 3.8 beta from http://github.com/tonytomov/jq.....d/tree/dev. Nobody (probably only Tony) have the version or will be test the retro version specially for you.


Understood, I updated the code to jqGrid 3.7.2

I tried the loadonce: true setting you mentioned and it seemed to break, displaying only one record (I'm not sure where it's from yet).

As for the other issues I described above, the upgrade alone didn't resolve them.

I have misgivings at returning all rows because this could amount to tens of thousands of results.

09/09/2010
00:31
Avatar
OlegK
Germany
Member
Members
Forum Posts: 1255
Member Since:
10/08/2009
sp_UserOfflineSmall Offline

To be able to help you you should post the JSON data which makes the problem: 2-3 line of data wold be OK (if you want test local paging you can send 6 rows and set rowNum: 5).

One typical problem could be that you used character ' instead of " in your "JSON" data or used unquoated properties names. The JSON formant is described under http://www.json.org. You can very easy test your data on http://www.jsonlint.com. New version of jqGrid and jQuery use internal JSON parser of the web browser which allow only correct JSON data.

If you don't find the error in your application now and not goes now to the jqGrid 3.7.2 you will be do the same work later. So I continue recommend you invest your time in upgrading now to stay to together with the time. After you will have success you will be really happy.

Best regards
Oleg 

09/09/2010
17:47
Avatar
Neb Okla
Columbus, OH, USA
Member
Members
Forum Posts: 10
Member Since:
03/09/2010
sp_UserOfflineSmall Offline

OlegK said:

To be able to help you you should post the JSON data which makes the problem: 2-3 line of data wold be OK (if you want test local paging you can send 6 rows and set rowNum: 5).


The data returned validatates as "Valid JSON" in JSONLint.  Here is a sample with 3 rows of data:

{
    "total": 33,
    "page": 1,
    "records": 805,
    "rows": [
        {
            "i": 123456,
            "cell": [
                "\u003ca href="\\Home\\SelectTest?studentId=123456&gradeLevel=KG      "\u003e123456\u003c/a\u003e",
                "Thomas",
                "Jefferson",
                "PENDING ",
                "KG",
                ""
            ]
        },
        {
            "i": 234567,
            "cell": [
                "\u003ca href="\\Home\\SelectTest?studentId=234567&gradeLevel=9       "\u003e234567\u003c/a\u003e",
                "Benjamin",
                "Franklin",
                "ENROLLED",
                "1",
                "02/25/2010"
            ]
        },
        {
            "i": 345678,
            "cell": [
                "\u003ca href="\\Home\\SelectTest?studentId=345678&gradeLevel=11      "\u003e345678\u003c/a\u003e",
                "George",
                "Washington",
                "ENROLLED",
                "11",
                "04/01/2010"
            ]
        }
    ]
}

The JSON is generated as a System.Web.Mvc.JsonResult object as part of a .NET MVC project so it seems that the formatting is valid.

The main issue I'm trying to resolve is to ensure that the grade level fields sort as integers instead of text (values KG, 1, and 11) in the data above.  I have another jqGrid with values ranging from KG to 2 and it seems to be sorting properly in local mode.

As a secondary issue I'd like the date to sort properly as well.  It's also sorting as text at the moment.

09/09/2010
23:36
Avatar
Neb Okla
Columbus, OH, USA
Member
Members
Forum Posts: 10
Member Since:
03/09/2010
sp_UserOfflineSmall Offline

After digging into the server-side code more today, it seems that the sorting symptioms described above may be caused by a server side sort coupled with a client side sort.  I became suspicious when I was able to successfully change the "KG" values in my data to zeros both on the server and client side, but even this had no effect on the search.

I'm hoping that jqGrid can handle paging of 1,000 records or so client side.  I think I read something about maybe 100,000 records being it's limit in another thread?

10/09/2010
00:26
Avatar
OlegK
Germany
Member
Members
Forum Posts: 1255
Member Since:
10/08/2009
sp_UserOfflineSmall Offline

Which id has you table element of the jqGrid: "list" or "gridid"? In your code you use both.

Your JSON data has "i" instead of "id" (for example "i": 123456).

The data which you pasted can not be verified in http://www.jsonlint.com b.....se of lines like

"u003ca href="HomeSelectTest?studentId=123456&gradeLevel=KG      "u003e123456u003c/au003e",

Whit values was before you paste in in the forum I can only guess, but it is far from an integer value how you declare in the ID column.

The first row which you has in your "JSON" data has the value "KG" as integer GradeLevel and "" as a date in format 'd/m/Y'. I understand that jqGrid has a problem to read the data.

Best regards
Oleg 

10/09/2010
15:36
Avatar
Neb Okla
Columbus, OH, USA
Member
Members
Forum Posts: 10
Member Since:
03/09/2010
sp_UserOfflineSmall Offline

OlegK said:

Which id has you table element of the jqGrid: "list" or "gridid"? In your code you use both.


The table element id is "list".  I accidentally pasted "gridid" from some sample code but I changed this to "list" in my code.  It didn't seem to have the desired effect so I removed those lines.

10/09/2010
17:06
Avatar
Neb Okla
Columbus, OH, USA
Member
Members
Forum Posts: 10
Member Since:
03/09/2010
sp_UserOfflineSmall Offline

OlegK said:

Your JSON data has "i" instead of "id" (for example "i": 123456).


I've corrected this in the code, it is now "id".

10/09/2010
17:08
Avatar
Neb Okla
Columbus, OH, USA
Member
Members
Forum Posts: 10
Member Since:
03/09/2010
sp_UserOfflineSmall Offline

OlegK said:

The data which you pasted can not be verified in http://www.jsonlint.com b.....se of lines like

"u003ca href="HomeSelectTest?studentId=123456&gradeLevel=KG      "u003e123456u003c/au003e",

Whit values was before you paste in in the forum I can only guess, but it is far from an integer value how you declare in the ID column.


Looks like the original developer was passing HTML within the JSON to make the ID's linkable.  I'm working on refactoring this so formatter:'showlink' is used instead.

10/09/2010
17:10
Avatar
Neb Okla
Columbus, OH, USA
Member
Members
Forum Posts: 10
Member Since:
03/09/2010
sp_UserOfflineSmall Offline

OlegK said:

The first row which you has in your "JSON" data has the value "KG" as integer GradeLevel and "" as a date in format 'd/m/Y'. I understand that jqGrid has a problem to read the data.

Best regards
Oleg 


I could replace the "KG" with "0" and use a formatter to address that issue.  What do you reccomend in cases where the data contains an empty date to avoid confusing jqGrid?

10/09/2010
20:35
Avatar
OlegK
Germany
Member
Members
Forum Posts: 1255
Member Since:
10/08/2009
sp_UserOfflineSmall Offline

If you will use integer data for 'ID' column and replace name:ID' to name:'ID' in the last version of the JavaScript code which you posted, the grid will be filled with data: http://www.ok-soft-gmbh.com/jq.....idData.htm (also with empty data and "KG" as integer).

Regards
Oleg

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
59 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