Jqgrid Tree View Adjacencey

hkv*_*hkv 4 jqgrid treegrid

我在ma应用程序中使用Jqgrid树视图模型,我可以看到它显示错误,因为不支持对象或属性我已经包含grid.Treeview.js和其他Jqgrid脚本文件.我不知道可能是什么问题.当我在网络中检查示例应用程序的邻接树视图时,我尝试了同样的事情,但在asp.net中使用我没有得到的本地数据.任何人都可以帮助我如何做同样的事情.提前致谢

这是我使用的示例代码,而不是它是否会起作用.

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]);
}
Run Code Online (Sandbox Code Playgroud)

Ole*_*leg 12

您编写了一些简单的小错误,但是您遇到的主要问题是您的代码是为了添加简单的行而不是树节点.您可以进入官方演示页面并在"新版本3.4"下选择演示"Tree Grid Adjacency模型".

我编写的演示程序与trirand演示页面的演示中的演示完全相同,但只使用本地数据:

在此输入图像描述

在你情况下,你必须将物体从扩展mydata与性能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}
];
Run Code Online (Sandbox Code Playgroud)

这里我修改了一个小的id值,因为不应该在id中使用点.另外,我将expanded"Main Menu1" 的状态设置为true仅显示您可以在加载后立即自动扩展某些节点.

我将jqGrid定义修改为以下内容

$("#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"
});
Run Code Online (Sandbox Code Playgroud)

我隐藏了'id'列并减小了网格大小.添加我使用addJSONData方法的数据,因为它将设置树节点

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

结果你将收到

在此输入图像描述

你可以在这里看到演示.

更新:如果您使用jqGrid 4.0或更高版本,loaded:true如果您想要扩展,为树节点设置属性很重要.例如,在上面的示例中,"Main Menu1"项是一个node(isLeaf:false),应该显示expanded(expanded:true),因此应该loaded:true为项目定义添加:

{id:"2", Name:"Main Menu1", MenuId:"2", MenuName:"Menu2",
 level:"0", parent:"", isLeaf:false, expanded:true, loaded:true}
Run Code Online (Sandbox Code Playgroud)

有关更多示例,请参见此处,此处,此处此处.

更新2:要正确排序,必须使用parent:nullparent:"null"代替parent:""查看此处了解更多详细信息.

  • @hkv:就像我承诺的那样,我发布了[建议](http://www.trirand.com/blog/?page_id=393/discussion/jqgrid-treegrid-subgrid-rewrite-code-cleanup-keyboard-navigation-jquery- 1-5-x-tests /#p22443)在jqGrid代码中进行小的更改,因此可以直接使用`data`参数. (3认同)
  • @hkv:不客气!我看到你的个人资料中有0票.可能你不知道,从15个声望点开始,你有权对任何答案或问题进行投票.有[简单规则](http://stackoverflow.com/faq#howtoask):"当您看到问题的新答案时,请通过单击答案左侧的向上箭头来对有用的答案进行投票." .投票对于stackoverflow非常重要,我建议你使用它. (2认同)
  • @hkv:我想在trirand论坛上写一个小建议,在jqGrid代码中做一些小改动,允许在树形网格**中使用`data`而不需要另外调用`addJSONData`.所以我希望在下一版本的jqGrid中不再需要它. (2认同)