jquery - Treegrid with JSON data in jqgrid -
i testing trees in jqgrid, far able create below
i want have jqgrid demo page
i came below code, no idea how should go expanding each row in tree given json format
$('<table id="list2" cellspacing="0" cellpadding="0"></table></div>').appendto('#topics'); var grid = jquery("#list2"); grid.jqgrid({ datastr:topicjson, datatype: "jsonstring", height: "auto", pager: false, loadui: "disable", colnames: ["id","items","url"], colmodel: [ {name: "id",width:1,hidden:true, key:true}, {name: "elementname", width:150, resizable: false}, {name: "url",width:1,hidden:true} ], treegrid: true, caption: "jqgrid demos", expandcolumn: "elementname", autowidth: true, //width: 180, rownum: 200, //expandcolclick: true, treeicons: {leaf:'ui-icon-document-b'}, jsonreader: { repeatitems: false, root: "response" } });
json format
var topicjson={ "response": [ { "id": "1", "elementname": "grouping", "sub": [ { "subelementname": "simple grouping" }, { "subelementname": "may other grouping" } ] }, { "id": "2", "elementname": "customformater", "sub": [ { "subelementname": "image formatter" }, { "subelementname": "anchor formatter" } ] } ] };
you try use tree grid absolutely wrong formatted data. should see tree grid grid additional hidden columns defines tree structure. names of columns depends on value of treegridmodel
parameter. recommend use treegridmodel: "adjacency"
. in case names of hidden columns be:
level, parent, isleaf, expanded, loaded, icon
in case of treegridmodel: 'nested' there lft
, rgt
instead of parent
column.
because every item of tree (root nodes , subitems) represents grid's row placed in grid every item have have id. in original version of topicjson
variable defined ids root elements (elements of level 0).
we can modify original example following:
var topicjson={ "response": [ { "id": "1", "elementname": "grouping", level:"0", parent:"", isleaf:false, expanded:false, loaded:true }, { "id": "1_1", "elementname": "simple grouping", level:"1", parent:"1", isleaf:true, expanded:false, loaded:true }, { "id": "1_2", "elementname": "may other grouping", level:"1", parent:"1", isleaf:true, expanded:false, loaded:true }, { "id": "2", "elementname": "customformater", level:"0", parent:"", isleaf:false, expanded:true, loaded:true }, { "id": "2_1", "elementname": "image formatter", level:"1", parent:"2", isleaf:true, expanded:false, loaded:true }, { "id": "2_1", "elementname": "anchor formatter", level:"1", parent:"2", isleaf:true, expanded:false, loaded:true } ] }, grid; $('<table id="list2"></table>').appendto('#topics'); grid = jquery("#list2"); grid.jqgrid({ datastr: topicjson, datatype: "jsonstring", height: "auto", loadui: "disable", colnames: [/*"id",*/"items","url"], colmodel: [ //{name: "id",width:1, hidden:true, key:true}, {name: "elementname", width:250, resizable: false}, {name: "url",width:1,hidden:true} ], treegrid: true, treegridmodel: "adjacency", caption: "jqgrid demos", expandcolumn: "elementname", //autowidth: true, rownum: 10000, //expandcolclick: true, treeicons: {leaf:'ui-icon-document-b'}, jsonreader: { repeatitems: false, root: "response" } });
you can see results of modification on demo here:
in example set expanded:true
property customformater
node demonstrate can specify nodes should directly displayed expanded.
i removed hidden column id
tree grid because id saved additionally id
attribute of corresponding <td>
element. if don't plan make column visible recommend place id
property in input data of tree (in topicjson
).
one more important remark. grid rows filled in same order in in input data. have place child nodes of node after parent. placed corresponding change request in trirand forum. requirement strict order of input data tree grid changed somewhere later.
updated: have sorting work correctly 1 have use parent:null
or parent:"null"
instead of parent:""
see here more details.
Comments
Post a Comment