Jqgrid Tree View Adjacencey
Asked Answered
C

1

4

I am using Jqgrid Tree View model in ma application and what i can see is that it shows error as object or property is not supported i have included grid.Treeview.js and other Jqgrid script file. I dont know what can be the issue. And when i checked the sample application in net for adjacency tree view and i tried the same thing but in asp.net with local data which i did not get. Can any one help me how to do the same. Thanks in advance

This is the sample code that im using and more over i dont whether will it work or not.

var myTreeGrid = new Ext.us.tree.TreeGrid({
    columns: columnsConfig,
    rootVisible: false,
    root: rootNode,
    loader: new Ext.ux.tree.TreeGridLoader({preloadChildren: true})
});
var rootNode = $('#treegridsamp').jqgrid({
    treeGrid: true,
    treeGridModel: 'adjacecncy',
    ExpandColumn: 'name',
    datatype: "local",
    mtype: 'Get',
    colNames: ['id','Name','MenuId','Menu Name'],
    colModel: [
        {name:'RowId',index:'RowId',width:300,fixed:true},
        {name:'Name',index:'Name',width:300,fixed:true},
        {name:'MenuId',index:'MenuId',width:300,fixed:true},
        {name:'MenuName',index:'MenuName',width:300,fixed:true},
    ],
    root:[
        {id:"1",Name:"Main Menu", MenuId:"1",MenuName:"Menu1"},
        {id:"2",Name:"Main Menu1",MenuId:"2",MenuName:"Menu2"},
        {id:"3",Name:"Main Menu2",MenuId:"3",MenuName:"Menu3"}
    ],
    pager: '#dvtreegridsamp',
    Caption: 'Sample Tree View Model'
})
$("#treegridsamp").jqGrid('navGrid', '#dvtreegridsamp',
    { edit: false, add: false, del: false, search: false, refresh: false });
var mydata=[
    {id:"1",    Name:"Main Menu",   MenuId:"1",MenuName:"Menu1"},
    {id:"2",    Name:"Main Menu1",  MenuId:"2",MenuName:"Menu2"},
    {id:"3",    Name:"Main Menu2",  MenuId:"3",MenuName:"Menu3"},
    {id:"1.1",  Name:"Sub Menu",    MenuId:"1",MenuName:"Menu1"},
    {id:"1.2",  Name:"Sub Menu1",   MenuId:"1",MenuName:"Menu1"},
    {id:"1.1.1",Name:"Sub Sub Menu",MenuId:"1",MenuName:"Menu1"},
    {id:"2.1",  Name:"Main Menu",   MenuId:"2",MenuName:"Menu2"},
    {id:"2.2",  Name:"Main Menu",   MenuId:"2",MenuName:"Menu2"},
    {id:"3.1",  Name:"Main Menu",   MenuId:"3",MenuName:"Menu3"},
    {id:"3.2",  Name:"Main Menu",   MenuId:"3",MenuName:"Menu3"},
];
for(var i=0;i<mydata.length;i++) {
    $("#treegridsamp").jqGrid('addRowData',i+1,mydata[i]);
}
Cheque answered 16/3, 2011 at 8:21 Comment(9)
could you append your question with the JavaScript and test data which can be used to reproduce your problem? Your current problem description is too general.Guayule
@Guayule : I have attached Please see to it and help me out to know this in a better way so that i can proceed furtherCheque
What is Ext.us.tree.TreeGrid and Ext.ux.tree.TreeGridLoader? Why you use $('#treegridsamp').jqgrid and not $('#treegridsamp').jqGrid?Guayule
Ext.us.tree.TreeGrid - Its said like some Ext method that will be passed to grid.treegrid.js and To load the same in the js i use TreeGridLoaded and '#treegridsamp' is my table id.Cheque
I meant that you use wrong case in $('#treegridsamp').jqgrid({treeGrid: true, treeGridModel: 'adjacecncy',... you should use $('#treegridsamp').jqGrid({... instead (jqGrid and not jqgrid).Guayule
Instead of the usage addRowData after the grid is defined. you should better use directly data:mydata parameter of jqGrid. The RowId column is undefined in your data. Do you want rename the column name to id?Guayule
Hey thanks dude that was the mistake and corrected it now i get the header alone and i have added "data:mydata" as you said but still no rows are shown up.. May be my root: data might have some bugs. So in this case what might be causing the problem or what i should do to rectify the same.. More over when i execute i can see the bug in Js saying that "no such method" error in Jqgrid.min.jsCheque
If possible can u provide the sample example in which i can understand how we can achieve the sameCheque
@hkv: I will try to make the example in the next time.Guayule
G
12

You code small simple errors, but the main problem which you have is that your code are made to add simple rows and not tree nodes. You can go on the official demo page and choose under "New in version 3.4" the demo "Tree Grid Adjacency model".

I wrote the demo which work exactly like the demo from the demo from the trirand demo page, but use only local data:

enter image description here

In you case you have to extend the objects from mydata with the properties level, parent, isLeaf, expanded:

var mydata = [
    {id:"1",Name:"Main Menu",MenuId:"1",MenuName:"Menu1",
     level:"0", parent:"", isLeaf:false, expanded:false},
    {id:"1_1",Name:"Sub Menu",MenuId:"1",MenuName:"Menu1",
     level:"1", parent:"1", isLeaf:false, expanded:false},
    {id:"1_1_1",Name:"Sub Sub Menu",MenuId:"1",MenuName:"Menu1",
     level:"2", parent:"1_1", isLeaf:true, expanded:false},
    {id:"1_2",Name:"Sub Menu1",MenuId:"1",MenuName:"Menu1",
     level:"1", parent:"1", isLeaf:true, expanded:false},
    {id:"2",Name:"Main Menu1",MenuId:"2",MenuName:"Menu2",
     level:"0", parent:"", isLeaf:false, expanded:true},
    {id:"2_1",Name:"Main Menu",MenuId:"2",MenuName:"Menu2",
     level:"1", parent:"2", isLeaf:true, expanded:false},
    {id:"2_2",Name:"Main Menu",MenuId:"2",MenuName:"Menu2",
     level:"1", parent:"2", isLeaf:true, expanded:false},
    {id:"3",Name:"Main Menu2",MenuId:"3",MenuName:"Menu3",
     level:"0", parent:"", isLeaf:false, expanded:false},
    {id:"3_1",Name:"Main Menu",MenuId:"3",MenuName:"Menu3",
     level:"1", parent:"3", isLeaf:true, expanded:false},
    {id:"3_2",Name:"Main Menu",MenuId:"3",MenuName:"Menu3",
     level:"1", parent:"3", isLeaf:true, expanded:false}
];

Here I modified a little id values, because points should not used in ids. Additionally I set the expanded status of the "Main Menu1" to true to demonstrate only that you can expanded some nodes automatically immediately after the loading.

I modified the jqGrid definition to the following

$("#treegridsamp").jqGrid({
    datatype: "local",
    data: mydata, // will not used at the loading,
                  // but during expanding/collapsing the nodes
    colNames:['id','Name','MenuId','Menu Name'],
    colModel:[
        {name:'id',index:'id',width:100,hidden:true},
        {name:'Name',index:'Name',width:150},
        {name:'MenuId',index:'MenuId',width:100},
        {name:'MenuName',index:'MenuName',width:100}
    ],
    height:'auto',
    //pager : "#ptreegrid",
    sortname: 'id',
    treeGrid: true,
    treeGridModel: 'adjacency',
    treedatatype: "local",
    ExpandColumn: 'Name',
    caption: "Sample Tree View Model"
});

I made the 'id' column hidden and reduced the grid size. To add the data I used addJSONData method because it will set the tree nodes

$("#treegridsamp")[0].addJSONData({
    total: 1,
    page: 1,
    records: mydata.length,
    rows: mydata
});

As the result you will receive

enter image description here

You can see the demo live here.

UPDATED: If you use jqGrid version 4.0 or higher it is important to set loaded:true property for the tree nodes if you want have expanded. For example in the above example the "Main Menu1" item is a node (isLeaf:false) which should be display expanded (expanded:true), so one should add loaded:true for the item definition:

{id:"2", Name:"Main Menu1", MenuId:"2", MenuName:"Menu2",
 level:"0", parent:"", isLeaf:false, expanded:true, loaded:true}

For more examples see here, here, here and here.

UPDATED 2: To have sorting work correctly one have to use parent:null or parent:"null" instead of parent:"" see here for more details.

Guayule answered 16/3, 2011 at 23:49 Comment(8)
@Oleg: Thanks for this beautiful stuff with your help i have learnt and understood adjacency model and i have completed my task after doing some modification as per my requirement....Cheque
@hkv: You are welcome! I have seen that you has 0 votes in your profile. Probably you don't know, that starting with 15 reputation points you have right to vote up any answer or question. There are the simple rule: "As you see new answers to your question, vote up the helpful ones by clicking the upward pointing arrow to the left of the answer.". Voting is very important for the stackoverflow and I recommend you to use it.Guayule
@hkv: I wanted to write in trirand forum small suggestion to make small changes in jqGrid code to allow to use data with tree grid without needed to make additional call of addJSONData. So I hope that in the next version of jqGrid it will be not more needed.Guayule
@hkv: Like I promised I posted the suggestion to make small change in jqGrid code, so the data parameter can be directly used.Guayule
Oleg, thank you so much for your clear, concise, detailed answer, your live example, and for following up weeks later with relevant info. I've been struggling with this topic for hours before finally finding this stack overflow page and you answered my question immediately. I wish I could up vote you more than once. Cheers, Lee.Ora
@Lee Richardson: You are welcome! I am glad to hear, that I could help you.Guayule
Excellent answer Oleg. Thanks for thaking your time to ilustrate the example.Coxalgia
@will824: You are welcome! I'm glad that my old answer could help you too.Guayule

© 2022 - 2024 — McMap. All rights reserved.