树形表格treegrid的使用心得

树形表格treegrid的使用心得

最近在做后台管理系统的界面的时候总是需要用到树形表格,相信大多数做后台系统都会有同样的需求。
今天就说一下最近使用easyui中的treegrid的心得。
- treegrid的语法

    #html
    <table id="tt"></table>
    #js
    $('#tg').treegrid({
       title:'TreeGrid with Footer',    //标题
       iconCls:'icon-ok',               //标题的图标
       width:700,                       //宽度
       height:250,                      //长度
       rownumbers: true,                //设置为true,则显示带有行号的列
       animate:true,                    //是否开启动画
       collapsible:true,                //是否可以折叠
       fitColumns:true,                 //设置为true,则会自动扩大或缩小列的尺寸以适应网格的宽度并且防止水平滚动。
       url:'treegrid_data2.json',       //获取数据的地址
       method: 'get',                   //请求远程数据方法的类型
       idField:'id',                    //指示那个字段是标识字段
       treeField:'name',                //定义树节点的字段
       showFooter:true,                 //定义是否显示行的底部
       columns:[[
           {title:'Task Name',field:'name',width:180},  //这一行就是treeField定义的树节点的列
           {field:'persons',title:'Persons',width:60,align:'right'},
           {field:'begin',title:'Begin Date',width:80},
           {field:'end',title:'End Date',width:80},
           {field:'progress',title:'Progress',width:120,
               formatter:function(value,row){  //在treegrid中formatter只能拿到两个参数
                   if (value){
                       var s = '<div style="width:100%;border:1px solid #ccc">' +
                               '<div style="width:' + value + '%;background:#cc0000;color:#fff">' + value + '%' + '</div>'
                               '</div>';
                       return s;
                   } else {
                       return '';
                   }
               }
           }
       ]]
   });

treegrid获取数据的方法

    treegrid获取数据的方法有两种
    一、直接写远程路径
    $('#tg').treegrid({
       url: 'treegrid_data2.json'
    });
    二、在获取数据后在赋值给data
    var treeData = {
        rows = [...]
    };
    $('#tg').treegrid({
       data: treeData
    });

rows中的数据结构


    一、这种数据结构是表面是平行的数据,根据id与_parentId来标识父子结构。
        id是根据idField这个参数自己设定的
        _parentId注意事项:
        如果没有父节点_parentId传null或者不传_parentId,不能传0(零)或者''(空字符串)
        这种数据结构中
             不论是url还是data,数据都必须是{...,rows=[]}的形式
        [{
            "id": 1,
            "name": "All Tasks",
            "begin": "3/4/2010",
            "end": "3/20/2010",
            "progress": 60,
            "iconCls": "icon-ok"
        },
        {
            "id": 2,
            "name": "Designing",
            "begin": "3/4/2010",
            "end": "3/10/2010",
            "progress": 100,
            "_parentId": 1,
            "state": "closed"
        },
        {
            "id": 3,
            "name": "Database",
            "persons": 2,
            "begin": "3/4/2010",
            "end": "3/6/2010",
            "progress": 100,
            "_parentId": 2
        }]
        二、这种结构就比较直观了,通过children字段来标识父子
        [{
            "id": 1,
            "name": "C",
            "size": "",
            "date": "02/19/2010",
            "children": [
                {
                    "id": 2,
                    "name": "Program Files",
                    "size": "120 MB",
                    "date": "03/20/2010",
                    "children": [
                        {
                            "id": 21,
                            "name": "Java",
                            "size": "",
                            "date": "01/13/2010",
                            "state": "closed",
                            "children": [
                                {
                                    "id": 211,
                                    "name": "java.exe",
                                    "size": "142 KB",
                                    "date": "01/13/2010"
                                },
                                {
                                    "id": 212,
                                    "name": "jawt.dll",
                                    "size": "5 KB",
                                    "date": "01/13/2010"
                                }
                            ]
                        }
                    ]
                }]
treegrid插件 当前选中的行: var config = { id: "tg1", width: "800", renderTo: "div1", headerAlign: "left", headerHeight: "30", dataAlign: "left", indentation: "20", folderOpenIcon: "images/folderOpen.gif", folderCloseIcon: "images/folderClose.gif", defaultLeafIcon: "images/defaultLeaf.gif", hoverRowBackground: "false", folderColumnIndex: "1", itemClick: "itemClickEvent", columns:[ {headerText: "", headerAlign: "center", dataAlign: "center", width: "20", handler: "customCheckBox"}, {headerText: "名称", dataField: "name", headerAlign: "center", handler: "customOrgName"}, {headerText: "拼音码", dataField: "code", headerAlign: "center", dataAlign: "center", width: "100"}, {headerText: "负责人", dataField: "assignee", headerAlign: "center", dataAlign: "center", width: "100"}, {headerText: "查看", headerAlign: "center", dataAlign: "center", width: "50", handler: "customLook"} ], data:[ {name: "城区分公司", code: "CQ", assignee: "", children:[ {name: "城区卡品分销中心"}, {name: "先锋服务厅", children:[ {name: "chlid1"}, {name: "chlid2"}, {name: "chlid3", children: [ {name: "chlid3-1"}, {name: "chlid3-2"}, {name: "chlid3-3"}, {name: "chlid3-4"} ]} ]}, {name: "半环服务厅"} ]}, {name: "清新分公司", code: "QX", assignee: "", children:[]}, {name: "英德分公司", code: "YD", assignee: "", children:[]}, {name: "佛冈分公司", code: "FG", assignee: "", children:[]} ] }; /* 单击数据行后触发该事件 id:行的id index:行的索引。 data:json格式的行数据对象。 */ function itemClickEvent(id, index, data){ window.location.href="ads"; } /* 通过指定的方法来自定义栏数据 */ function customCheckBox(row, col){ return ""; } function customOrgName(row, col){ var name = row[col.dataField] || ""; return name; } function customLook(row, col){ return "查看"; } //创建一个组件对象 var treeGrid = new TreeGrid(config); treeGrid.show(); /* 展开、关闭所有节点。 isOpen=Y表示展开,isOpen=N表示关闭 */ function expandAll(isOpen){ treeGrid.expandAll(isOpen); } /* 取得当前选中的行,方法返回TreeGridItem对象 */ function selectedItem(){ var treeGridItem = treeGrid.getSelectedItem(); if(treeGridItem!=null){ //获取数据行属性值 //alert(treeGridItem.id + ", " + treeGridItem.index + ", " + treeGridItem.data.name); //获取父数据行 var parent = treeGridItem.getParent(); if(parent!=null){ //jQuery("#currentRow").val(parent.data.name); } //获取子数据行集 var children = treeGridItem.getChildren(); if(children!=null && children.length>0){ jQuery("#currentRow").val(children[0].data.name); } } }
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值