Question

I'm newbie with jqGrid and I have build a jqGrid treeGrid with local datas and all is fine. But now I'm trying to do same thing with remote JSON datas and I can't obtain treeGrid display my datas. Here is treeGrid conf :

$("#"+subgrid_table_id).jqGrid({ 
    url:"sg511e.php?id="+row_id+"&btr="+btr, 
    datatype:"json",
    mType:'POST',
    loadui: "disable",
    colNames:['id','Prestations'],
    colModel:[
        {name:'id',index:'id',width:100,hidden:true},
        {name:'name',index:'name',width:785,sortable:false}
    ],
    pager: pager_id, 
    sortname: 'id', 
    sortorder: "asc", 
    hiddengrid:true,
    gridview: true,
    treeGrid:true,
    treeGridModel: "adjacency",
    treedatatype: 'json',
    ExpandColumn: 'name',
    jsonReader: {
        repeatitems: false,
        root: "rows"
    },

and JSON datas :

{
    "page": 1,
    "total": 1,
    "records": 1,
    "rows": [
        {
            "cell": {
                "id": "1",
                "name": "ECHANGEUR",
                "level": "0",
                "parent": "",
                "isLeaf": true,
                "expanded": false,
                "loaded": true
            }
        },
        {
            "cell": {
                "id": "1_1",
                "name": "Intervention Aller sur Site",
                "level": "1",
                "parent": "1",
                "isLeaf": true,
                "expanded": false,
                "loaded": true
            }
        },
        {
            "cell": {
                "id": "1_1_1",
                "name": "Date et heure d'arrivée sur le site",
                "level": "2",
                "parent": "1_1",
                "isLeaf": false,
                "expanded": true,
                "loaded": true
            }
        },
        {
            "cell": {
                "id": "1_1_2",
                "name": "Consignation de l'échangeur",
                "level": "2",
                "parent": "1_1",
                "isLeaf": false,
                "expanded": true,
                "loaded": true
            }
        }
    ]
}

I don't see what I'm wrong so I hope you can bring some help and explain my error. Many thanks in advance JiheL

Was it helpful?

Solution

First of all you should fix the JSON data:

  • replace "parent": "" to "parent": "null" in the root element
  • you should invert the values of isLeaf property: change all true values to false and change all false values to true
  • you should remove "cell" part from all items
  • The value "records": 1 don't corresponds 4 items of data. I suppose that the correct value should be "records": 4, but the best will be to remove the pager from the list of options of the grid. In the case setting of any page, total or records will be not important.

You can more simplify the data and remove the rows part from JSON data. In the case we have to change the root property of jsonReader to root: function (obj) { return obj; }. As the result you can use the following simple JSON data:

[
    {
        "id": "1",
        "name": "ECHANGEUR",
        "level": "0",
        "parent": "null",
        "isLeaf": false,
        "expanded": false,
        "loaded": true
    },
    {
        "id": "1_1",
        "name": "Intervention Aller sur Site",
        "level": "1",
        "parent": "1",
        "isLeaf": false,
        "expanded": false,
        "loaded": true
    },
    {
        "id": "1_1_1",
        "name": "Date et heure d'arrivée sur le site",
        "level": "2",
        "parent": "1_1",
        "isLeaf": true,
        "expanded": true,
        "loaded": true
    },
    {
        "id": "1_1_2",
        "name": "Consignation de l'échangeur",
        "level": "2",
        "parent": "1_1",
        "isLeaf": true,
        "expanded": true,
        "loaded": true
    }
]

The demo demonstrate the results of the changes. After extending the grid looks like on the picture below

enter image description here

The code which I used in the demo is:

$("#grid").jqGrid({
    url: "user2132268.json",
    datatype: "json",
    colNames: [ 'id', 'Prestations'],
    colModel: [
        {name: 'id', width: 100, key: true, hidden: true},
        {name: 'name', width: 785, sortable: false}
    ],
    sortname: 'id',
    sortorder: "asc",
    hiddengrid: true,
    gridview: true,
    treeGrid: true,
    treeGridModel: "adjacency",
    ExpandColumn: 'name',
    ExpandColClick: true,
    jsonReader: { repeatitems: false, root: function (obj) { return obj; } },
    height: "auto"
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top