jqGrid树网格与本地数据

时间:2011-07-22 10:32:40

标签: javascript jquery jqgrid

树格的

jqGrid docs说:“目前jqGrid只能处理从服务器返回的数据。有一些技巧和文章描述了如何使用本地数据。”

足够公平,但我无法找到这样的文章。关于如何实现它的任何提示,最好等同于datatype=local

1 个答案:

答案 0 :(得分:8)

可能this旧回答可以帮到你。使用jqGrid的最新版本的演示,您可以找到here

更新:现在我更倾向于使用与datatype: "jsonstring"几乎相同的datatype: "local"。在这种情况下,需要使用datastr: mydata而不是data: mydata。另外,必须使用jsonReader as function。结果将有以下modified demo

您在下面找到的演示的相应代码

var mydata = [
    { id:"1", name:"Cash",   num:"100", debit:"400.00",credit:"250.00", balance:"150.00", enbl:"1",
      level:"0", parent:"",  isLeaf:false, expanded:false, loaded:true },
    { id:"2", name:"Cash 1", num:"1",   debit:"300.00",credit:"200.00", balance:"100.00", enbl:"0",
      level:"1", parent:"1", isLeaf:false, expanded:false, loaded:true },
    { id:"3", name:"Sub Cash 1", num:"1",debit:"300.00",credit:"200.00", balance:"100.00", enbl:"1",
      level:"2", parent:"2", isLeaf:true,  expanded:false, loaded:true },
    { id:"4", name:"Cash 2", num:"2",debit:"100.00",credit:"50.00", balance:"50.00", enbl:"0",
      level:"1", parent:"1", isLeaf:true,  expanded:false, loaded:true },
    { id:"5", name:"Bank\'s", num:"200",debit:"1500.00",credit:"1000.00", balance:"500.00", enbl:"1",
      level:"0", parent:"",  isLeaf:false, expanded:true, loaded:true },
    { id:"6", name:"Bank 1", num:"1",debit:"500.00",credit:"0.00", balance:"500.00", enbl:"0",
      level:"1", parent:"5", isLeaf:true,  expanded:false, loaded:true },
    { id:"7", name:"Bank 2", num:"2",debit:"1000.00",credit:"1000.00", balance:"0.00", enbl:"1",
      level:"1", parent:"5", isLeaf:true,  expanded:false, loaded:true },
    { id:"8", name:"Fixed asset", num:"300",debit:"0.00",credit:"1000.00", balance:"-1000.00", enbl:"0",
      level:"0", parent:"",  isLeaf:true,  expanded:false, loaded:true }
    ],
    grid = $("#treegrid");

grid.jqGrid({
    datatype: "jsonstring",
    datastr: mydata,
    colNames:["Id","Account","Acc Num","Debit","Credit","Balance","Enabled"],
    colModel:[
        {name:'id', index:'id', width:1, hidden:true, key:true},
        {name:'name', index:'name', width:180},
        {name:'num', index:'acc_num', width:80, align:"center"},
        {name:'debit', index:'debit', width:80, align:"right"},
        {name:'credit', index:'credit', width:80,align:"right"},
        {name:'balance', index:'balance', width:80,align:"right"},
        {name:'enbl', index:'enbl', width: 60, align:'center',
         formatter:'checkbox', editoptions:{value:'1:0'},
         formatoptions:{disabled:false}}
    ],
    height: 'auto',
    gridview: true,
    rowNum: 10000,
    sortname: 'id',
    treeGrid: true,
    treeGridModel: 'adjacency',
    treedatatype: "local",
    ExpandColumn: 'name',
    caption: "Demonstrate how to use Tree Grid for the Adjacency Set Model",
    jsonReader: {
        repeatitems: false,
        root: function (obj) { return obj; },
        page: function (obj) { return 1; },
        total: function (obj) { return 1; },
        records: function (obj) { return obj.length; }
    }
});

更新2 :应使用parent:"null"parent:null代替parent:""