0

I am using jqgrid treeview with an asp.net-mvc backend and i see the GUI lets you click on other columns for sorting but i can't find any documentation or examples on how to implement it.

When I click a column heading, my "Loading . . " notification shows up but none of my server side actions are getting called.

I used httpwatch and i don't see any calls trying to hit the server.

Does jqgrid treeview support sorting (this would just be sorting the top level in the tree)?

Here is my code:

$("#treegrid").jqGrid({
    url: '/MyController/GetData' + GetQuery(),
    datatype: 'json',
    footerrow: true,
    userDataOnFooter: true,
    mtype: 'GET',
    rowNum: 2000,
    colNames: ["ID", "Description", "Total 1"],
    colModel: [{
        name: 'id',
        index: 'id',
        width: 1,
        hidden: true,
        key: true
    }, {
        name: 'desc',
        width: 240,
        index: 'desc',
        hidden: false,
        sortable: true
    },
            {
                name: 'total1',
                sorttype: 'int',
                index: 'total1',
                unformat: originalValueUnFormatter,
                formatter: bookMapFormatter,
                align: "right",
                width: 60,
                hidden: false,
                sortable: true
            }],
    treeGridModel: 'adjacency',
    height: 'auto',
    loadComplete: function (data) {


    },
    pager: "#ptreegrid",
    treeGrid: true,
    ExpandColumn: 'desc',
    ExpandColClick: true,
    caption: "My Treegrid"
});


 function bookMapFormatter(cellvalue, options, rowObject) {
     return booksFormatterEx(cellvalue, options, rowObject, "MyAction");
 }

function booksFormatterEx(cellvalue, options, rowObject, actionName) {
    var regularURL = '/MyController/' + actionName + GetQuery() + '&key=' + rowObject[0];
    return "<a target='_blank' href='" + regularURL + "' >" + cellvalue + "</a>";

}

Also if its helpful, i have serialized my json results (using the .net JavascriptSerializer class) (this is just the top level view as i go back to the server when users drill down)

 {"page":1,
 "total":1,
 "records":2,
 "userdata":{"desc":"Total:","total1":"10,753"},
 "rows":[{
 "id":"Group1","cell":["Group1","1 - 1 System(s)",723, 0, null, false, false, true]},
 {"id":"Group2","cell":["Group2","2 - 2 System(s)",2120, 0, null, false, false, true]},
 {"id":"Group3","cell":["Group3","3 - 3 System(s)",2017, 0, null, false, false, true]},
 {"id":"Group4","cell":["Group4","4 - 4 System(s)",1181, 0, null, false, false, true]},
 {"id":"Group5","cell":["Group5","5 - 5 System(s)",4712, 0, null, false, false, true]}]}
leora
  • 188,729
  • 360
  • 878
  • 1,366

2 Answers2

2

Tree grid has SortTree method which will be used to sort grid locally. No call to the server will be done. If the user click on the column header the same method will be called.

For better understanding I take your attention on one line from the jqGrid code:

if( ts.p.loadonce || ts.p.treeGrid) {ts.p.datatype = "local";}

which will be executed in case of usage datatype: 'json' or datatype: 'json'. So after the first loading of Treegrid the datatype will be changed to datatype: 'local'.

Depend on the other jqGrid data (loaded:true or loaded:false) the expanding of the tree node will be locally or the request will be send to the server. In any way the sorting of the data will be always implemented locally.

The only thing which if important to have correct sorting: one should use parent: "null" or parent: null in the data (not parent: "") for the root nodes (see here for details).

UPDATED: Additionally to the problem with parent: "null" or parent: null (which you not have in the JSON data) you have the problem with the usage of HTML inside of JSON response from the server. The JSON data which you posted are equivalent to the following:

{
    "page": 1,
    "total": 1,
    "records": 2,
    "userdata": {
        "desc": "Total:",
        "bookmapBooks": "10,753"
    },
    "rows": [
        {"id": "Group1", "cell": ["Group1", "<b>COMMODITIES</b> - 19  System(s)",         "<b>723</b>"]},
        {"id": "Group2", "cell": ["Group2", "<b>CREDIT</b> - 30 System(s)",               "<b>2,120</b>"]},
        {"id": "Group3", "cell": ["Group3", "<b>EQUITIES</b> - 23 System(s)",             "<b>2,017</b>"]},
        {"id": "Group4", "cell": ["Group4", "<b>MORTGAGE PRODUCTS</b> - 33 System(s)",    "<b>1,181</b>"]},
        {"id": "Group5", "cell": ["Group5", "<b>RATES AND CURRENCIES</b> - 40 System(s)", "<b>4,712</b>"]}
    ]
}

As I described before, the datatype of the grid will be changed to the 'local' after the first data load. The internal data parameter will be filled with the JSON data exactly in the format like you has in the JSON input. So for the column 'total1' you would has data like "<b>723</b>" or "<b>4,712</b>" which corresponds not the sorttype: 'int'. So adding "0", "null" for level: 0, parent: null will not full solve the problem of the grid sorting. The string "<b>4,712</b>" can't been converted to int and so can't be sorted as integer.

One more small remark. You probably want to use "userdata":{"desc":"Total:","total1":"10,753"} instead of "userdata":{"desc":"Total:","bookmapBooks":"10,753"}.

With respect of the usage of sorttype as function you could solve your problem and change the original grid

to the sortable grid

You can use the sorttype in the following form:

sorttype: function (val) {
    var strValue = $(val).text().replace(",",""); // replace thousandsSeparator
    return parseInt(strValue, 10);
}
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @ooo: You don't post enough information to reproduce your problem. One needs at least the JSON response from the server and probably the code of `Setup` function. The code of `bookMapFormatter` is also required. – Oleg Sep 19 '11 at 13:09
  • i have included bookMapFormatter in the question above. I have removed the Setup() method above as all that does is update some non related items on the page so its not pertinent for this question – leora Sep 19 '11 at 15:55
  • i have also dumped the top level json into the question as well so i think i have given you each of the things you asked for to clarify my issue above. Please confirm and let me know your thoughts. – leora Sep 19 '11 at 16:21
  • thanks for the update. I don't understand your statement "As I described before, the datatype of the grid will be changed to the 'local' after the first data load". Are you just talking about when i click sort (as I am always going back to the server for drilldown data, etc) – leora Sep 22 '11 at 23:01
  • @ooo: If you click on the column header the sorting will be done **locally** without any request to the server. The local data having `"4,712"` will be used for the sorting. The string `` can't be converted to int, so if you use `sorttype: 'int'` the sorting can't work correctly. – Oleg Sep 22 '11 at 23:05
  • understood . .i will go in and remove the html and make sure i am just passing in ints – leora Sep 23 '11 at 16:07
  • i removed all of the formatting and html out of my json from the server to make the json real integers. Now it sort of works (no pun intended) but i still have an issue. It does sort the column locally BUT still seems to try to call some server action as the "Loading . . ." popup shows up indefinitely and I can't sort again. Any ideas? – leora Sep 26 '11 at 12:07
  • @ooo: You should include the current JSON data and the jqGrid which you use. In [the demo](http://www.ok-soft-gmbh.com/jqGrid/LocalAdjacencyTreeSortingOOO.htm) which I made based on your previous data the problem not exist. We have to find the differences to solve your current problem. – Oleg Sep 26 '11 at 15:44
  • @ooo: Your current code have still no treegrid information like `parent: "null"`. Just compare your JSON with [LocalAdjacencyTreeSortingOOO.json](http://www.ok-soft-gmbh.com/jqGrid/LocalAdjacencyTreeSortingOOO.json) which I in my [demo](http://www.ok-soft-gmbh.com/jqGrid/LocalAdjacencyTreeSortingOOO.htm) – Oleg Sep 27 '11 at 10:50
  • i have updated the json in my question to reflect the removal of html. So, as you can, see the Total 1 column now comes in as integers. I haven't touched any of the javascript code based on your feedback – leora Sep 27 '11 at 11:00
  • please see updated json (once again), i realized i was chopping off the last few parameters of each line when i was trying to simplify the question. I hope this helps clarify what i am doing wrong as i now show: null,false,false,false on each row. – leora Sep 27 '11 at 11:04
  • @ooo: you still miss one parameter. `level` which should be *before* the `parent`: `"0","null","false","false","false"` or `0,null,false,false,false` and not just `null,false,false,false`. – Oleg Sep 27 '11 at 11:15
  • sorry, i was looking at the wrong code. I have once again updated the json and I do have the 0 listed as the "level" field. At the end of each row i have: 0, null, false, false, true. Apologies for the confusion on the last post. Any thoughts? – leora Sep 27 '11 at 11:27
  • @ooo: the `bookmapBooks` in the `userdata` part of JSON data should be replaced to `total1`, but it's not important. In [the demo](http://www.ok-soft-gmbh.com/jqGrid/LocalAdjacencyTreeSortingOOO1.htm) which uses new data I see no problems with the sorting. One can now use `sorttype: 'int'` instead of custom sorting with `sorttype` as function. – Oleg Sep 27 '11 at 11:44
  • bookmapBooks instead of total is just a typo. I fixed that in the question. I agree that the json data now looks the same as your example but something is triggering the "Loading . ." popup to show up. Since i am calling back to the server when the user click on the row to drill down. Is it possible that clicking on the header Is making jqgrid think that (on top of sorting), it wants to do a drill down request? I can't find anything else different in the json. – – leora Sep 27 '11 at 15:58
  • @ooo: I suppose that you make your tests with another grid as you posted in your question. For example you can do some additional things inside of `loadComplete`. If you have the demo online you can post the URL and I could debug it. In the case you should include `jquery.jqGrid.src.js` instead of `jquery.jqGrid.min.js`. If you don't has your demo online you can post more full code or send it me to oleg.kiriljuk@ok-soft-gmbh.com – Oleg Sep 27 '11 at 16:56
  • I figured out the issue. It turns out that LoadComplete callback was getting called when i clicked sort and the data in the callback was undefined and i have some js code try to access it . . I put a "if (data) around my loadcomplete code and now i am able to sort. .. mystery solved !!!!! – leora Oct 11 '11 at 17:48
0

Speaking from memory, everytime you click on a column header that were previously configured for sorting in the column model, a request is made to the url that you specified (also) in the configuration. Four parameters in the request are page, rows, sidx, and sord.

With these values in the params, you have enough information to ensure the data is returned in the order (including the paging) that you want.

Kevin Le - Khnle
  • 10,579
  • 11
  • 54
  • 80
  • i agree with you for regular jqgrid but in this case for treegrid, when using httpwatch, i don't see any server side requests being fired – leora Sep 19 '11 at 03:14