1

I have a problem getting my jqgrid to display the data in a treegrid format.

The jqGrid is defined as follows:

$("#grid").jqGrid({
                dataType: 'local',
                colNames: columnHeaders,
                colModel: columnModel,
                gridview: true,
                treeGrid: true,
                treedatatype: 'local',
                loadonce: true,
                treeGridModel: 'adjacency',
                ExpandColumn: 'DFECode',
                ExpandColClick: true,
                height: 'auto',
                caption: 'Multi Site Support',
                autowidth: true,
                shrinktofit: true,
                multiselect: false,
                sortable: false,
                ignorecase: true,
                rowNum: 20,
                scroll: true,
                loadComplete: function () { gridLoadComplete(); },
                onSelectRow: function (id) { gridOnSelectRow(id); },
                jsonReader: { repeatitems: false },
                onCellSelect: function(rowId, colId, cellContent, evt) {gridOnCellSelect(rowId, colId, cellContent, evt)}

            });

With the column headers defined by a call to an MVC action method which returns the following Json:

[ "SiteID", "DFECode", "Site Name", "Role1_ID", "Admin", "Role2_ID", "Support", "Action" ]

The jqGrid Model is defined like this (again generated from an MVC controller action):

[{
        "name": "SiteID",
        "index": "SiteId",
        "width": "0",
        "hidden": true,
        "sortable": false
    },
    {
        "name": "DFECode",
        "index": "DFECode",
        "width": "120",
        "hidden": false,
        "sortable": false
    },
    {
        "name": "SiteName",
        "index": "SiteName",
        "width": "200",
        "hidden": false,
        "sortable": false
    },
    {
        "name": "Role1_ID",
        "index": "Role1_ID",
        "width": "0",
        "hidden": true,
        "sortable": false
    },
    {
        "name": "Role1_Value",
        "index": "Role1_Value",
        "width": "90",
        "hidden": false,
        "sortable": false,
        "formatter": "checkbox",
        "align": "center",
        "editable": true,
        "edittype": "checkbox",
        "formatoptions": {
            "disabled": false
        }
    },
    {
        "name": "Role2_ID",
        "index": "Role2_ID",
        "width": "0",
        "hidden": true,
        "sortable": false
    },
    {
        "name": "Role2_Value",
        "index": "Role2_Value",
        "width": "90",
        "hidden": false,
        "sortable": false,
        "formatter": "checkbox",
        "align": "center",
        "editable": true,
        "edittype": "checkbox",
        "formatoptions": {
            "disabled": false
        }
    },
    {
        "name": "Action",
        "index": "Action",
        "width": "90",
        "hidden": false,
        "sortable": false,
        "formatter": "updateButtonFormatter",
        "editable": false,
        "formatoptions": {
            "disabled": false
        }
    }]

The initial load of the data returns the following (once more from an MVC controller action:

[{
        "SiteID": "25966",
        "DFECode": "205",
        "SiteName": "Hammersmith and Fulham",
        "Role1_ID": 1,
        "Role1_Value": true,
        "Role2_ID": 2,
        "Role2_Value": false,
        "Action": "<button type='button' id='action_25966' class='btn btn-info select-tag btn-sm'>Update</button>",
        "level": "0",
        "parent": "",
        "isLeaf": false,
        "expanded": false,
        "loaded": true
    },
    {
        "SiteID": "224",
        "DFECode": "205-1034",
        "SiteName": "Randolph Beresford Early Years Centre(Hammersmith and Fulham)",
        "Role1_ID": 1,
        "Role1_Value": true,
        "Role2_ID": 2,
        "Role2_Value": false,
        "Action": "<button type='button' id='action_224' class='btn btn-info select-tag btn-sm'>Update</button>",
        "level": "1",
        "parent": "25966",
        "isLeaf": true,
        "expanded": false,
        "loaded": true
    },
    {
        "SiteID": "225",
        "DFECode": "205-1039",
        "SiteName": "Vanessa Nursery School(Hammersmith and Fulham)",
        "Role1_ID": 1,
        "Role1_Value": true,
        "Role2_ID": 2,
        "Role2_Value": false,
        "Action": "<button type='button' id='action_225' class='btn btn-info select-tag btn-sm'>Update</button>",
        "level": "1",
        "parent": "25966",
        "isLeaf": true,
        "expanded": false,
        "loaded": true
    },
    {
        "SiteID": "226",
        "DFECode": "205-1056",
        "SiteName": "James Lee Nursery School(Hammersmith and Fulham)",
        "Role1_ID": 1,
        "Role1_Value": true,
        "Role2_ID": 2,
        "Role2_Value": false,
        "Action": "<button type='button' id='action_226' class='btn btn-info select-tag btn-sm'>Update</button>",
        "level": "1",
        "parent": "25966",
        "isLeaf": true,
        "expanded": false,
        "loaded": true
    },
    {
        "SiteID": "227",
        "DFECode": "205-1059",
        "SiteName": "Bayonne Nursery School(Hammersmith and Fulham)",
        "Role1_ID": 1,
        "Role1_Value": true,
        "Role2_ID": 2,
        "Role2_Value": false,
        "Action": "<button type='button' id='action_227' class='btn btn-info select-tag btn-sm'>Update</button>",
        "level": "1",
        "parent": "25966",
        "isLeaf": true,
        "expanded": false,
        "loaded": true
    }]

I've looked at a few examples found on the internet including some on StackOverflow linking to some on Fiddler and I cannot work out for the life of me what I am doing wrong. I would like the data to be displayed as a tree but instead the data seems to be displayed in the standard grid format (please see the attached screen shot). (I am aware it needs some styling but I will do that once I can resolve my treegrid issue !!!)

Screen shot of jqGrid Tree view with incorrect layout

I'd be grateful if someone could take a look and show me the error of my ways.

Thanks @Oleg ...

The jqGrid I use is the latest one from nuget ...

I get the data using the following method and populate the jqGrid as follows ...

var treeData;
            $.getJSON("/UserMgmt/Home/GetTreeData", { userId: userId }).done(function (rawData) {
                if (rawData !== undefined && rawData !== null) {
                    treeData = $.parseJSON(rawData);

                    var grid = $("#grid");
                    grid[0].addJSONData({
                        total: treeData.length,
                        page: 1,
                        records: treeData.length,
                        rows: treeData
                    });

                    //  Set the correct rowId for retrieving the updated row data.
                    var rowIds = grid.getDataIDs();
                    for (var i = 0; i < rowIds.length; i++) {

                        var rowId = rowIds[i];
                        grid.setCell(rowId, "Action", "<input type='button' value='Update' id='action_" + rowId + "' class='btn btn-info select-tag btn-sm' />");
                    }
                }
            });

@Oleg ... The nuget package i use(d) is this one ...

Image of installed jqGrid nuget package

  • Which version of jGrid from which fork ([free jqGrid](https://github.com/free-jqgrid/jqGrid), [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or an old jqGrid in version <=4.7) you use? You use `dataType` instead of `datatype` and your code don't contains any `data` or `datastr` parameters. It's unclear how you fill the TreeGrid with the data. – Oleg Jan 11 '16 at 14:32
  • Cheers Oleg ... I use the latest jqGrid from nuget. I load the data using the following method ... – JulianHolling Jan 12 '16 at 08:50
  • What NuGet package you use? I publish my fork under https://www.nuget.org/packages/free-jqGrid/ and the latest version is 4.12.0. What do you use? By the way NuGet will be used in the future (ASP.NET 5) only for *server side* components. Thus you should use npm or bower (see [here](https://docs.asp.net/en/latest/client-side/index.html)). I publish free jqGrid on all the repositories (see [here](https://www.npmjs.com/package/free-jqgrid) and [here](http://bower.io/search/?q=free-jqgrid)) and on two CDNs (cdnjs.com and www.jsdelivr.com) for quick access from internet. – Oleg Jan 12 '16 at 09:22
  • See http://www.jsdelivr.com/projects/free-jqgrid and https://cdnjs.com/libraries/free-jqgrid , [the wiki article](https://github.com/free-jqgrid/jqGrid/wiki/Access-free-jqGrid-from-different-CDNs) and [the readme](https://github.com/free-jqgrid/jqGrid/blob/master/README.md). – Oleg Jan 12 '16 at 09:39

1 Answers1

0

You use the values from SiteID column in parent, but you don't informs jqGrid about that. Moreover it't unclear how you fill the grid with the data.

datatype: "jsonstring",
datastr: mydata,
jsonReader: { id: "SiteID" }

where mydata is the input data which you posted. Look at https://jsfiddle.net/rfwvovub/2/, which fixes the grid. I used free jqGrid in the demo, but the same, I think, should work with old versions of jqGrid too.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks @Oleg, adding id: "SiteID" to my jsonReader definition fixed the issue. Unfortunately I don't have enough reputation points to make a difference on the upvote ... but a brilliant answer. – JulianHolling Jan 12 '16 at 09:18
  • edited the question to include an image with the installed jqGrid nuget package. Once again, thankyou for your answer :) – JulianHolling Jan 12 '16 at 10:25
  • @JulianHolling: You used version old version 4.6 of jqGrid published two years ago. You can see on the image which you posted the second package "free-jqGrid". In the way you will install free jqGrid 4.12.0. It's compatible with the old version 4.6, but have a lot of new features, bug fixes and performance improvements. By the way the usage of `addJSONData` for filling the grid is not the best choice now. The usage of `setCell` in the loop is very bad too. Instead of `setCell` you can use custom formatter for the `Action` column. – Oleg Jan 12 '16 at 11:06
  • @JulianHolling: See https://jsfiddle.net/OlegKi/rfwvovub/3/ where I defined `formatter: "updateButtonFormatter"` as extension of `$.fn.fmatter`. You can see using Developer Tools of IE or Chrome that Action column uses `` with the `id` generated dynamically. [the demo](http://www.ok-soft-gmbh.com/jqGrid/OK/CustomActionButton.htm) and [this one](http://www.ok-soft-gmbh.com/jqGrid/OK/CustomActionButton1.htm) (see [the answer](http://stackoverflow.com/a/29735149/315935)) shows alternative way with the usage of custom action buttons, which I implemented in free jqGrid. – Oleg Jan 12 '16 at 11:11
  • Thanks once again @Oleg. I'll take a look at your suggestions and refactor accordingly. You seem to have done a lot of hard work on the jqGrid getting it to a much better place. – JulianHolling Jan 12 '16 at 13:45