Extjs复习笔记(十八)-- TreePanel

Grid这一块暂时就讲到这。这一节开始就是tree的相关内容了。


Ext.onReady(function(){
    var tree = new Ext.tree.TreePanel({
        renderTo:'tree-div',
        title: 'My Task List',
        height: 300,
        width: 400,
        useArrows:true, //true to use Vista-style arrows in the tree (defaults to false)
        autoScroll:true, //太多的时候出现滚动条
        animate:true, //有缓慢收缩的效果,默认是有的,所以这句可以不用写
        enableDD:true, //true to enable drag and drop
        containerScroll: true, //true to register this container with ScrollManager
        rootVisible: false, //不显示根结点
        frame: true,
        root: {
            nodeType: 'async' //creates a AsynTreeNode instead of a TreeNode
        },
        /*nodeType: 'async' --> This would be a good idea if you are basing a tree structure on in-memory data which has circular references. Obviously, you don't want all possible child nodes loading because that would cause an error. async means only load child nodes when they are needed.*/

        // auto create TreeLoader
        dataUrl: 'check-nodes.json', //写成一个文件 的格式,我上传了
        
        listeners: { //记住这个形式,加上这句后,当选中一个复选框时就会在当前项上加上删除线
            'checkchange': function(node, checked){
                if(checked){
                    node.getUI().addClass('complete'); 
                }else{
                    node.getUI().removeClass('complete');
                }
            }
        },
        
        buttons: [{
            text: 'Get Completed Tasks',
            handler: function(){
                var msg = '', selNodes = tree.getChecked();//getChecked():Retrieve an array of checked nodes, or an array of a specific attribute of checked nodes
                Ext.each(selNodes, function(node){ //Ext.each(array, function)是一个循环
                    if(msg.length > 0){
                        msg += ', ';
                    }
                    msg += node.text; //node是其中的默认参数,指selNodes中取出的每一个元素
                }); /*Function:
The function to be called with each item. If the supplied function returns false, iteration stops and this method returns the current index. This function is called with the following arguments:
item : Mixed
The item at the current index in the passed array
index : Number
The current index within the array
allItems : Array
The array passed as the first argument to Ext.each.*/
                Ext.Msg.show({
                    title: 'Completed Tasks', 
                    msg: msg.length > 0 ? msg : 'None',
                    icon: Ext.Msg.INFO,
                    minWidth: 200,
                    buttons: Ext.Msg.OK
                });
            }
        }]
    });

    tree.getRootNode().expand(true); //先获得根结点,再让它全部展开
});

 

其中check-nodes.json的内容如下:

 

[{
    text: 'To Do', 
    cls: 'folder',
    children: [{
        text: 'Go jogging',
        leaf: true,
        checked: false
    },{
        text: 'Take a nap',
        leaf: true,
        checked: false
    },{
        text: 'Climb Everest',
        leaf: true,
        checked: false
    }]
},{
    text: 'Grocery List',
    cls: 'folder',
    children: [{
        text: 'Bananas',
        leaf: true,
        checked: false
    },{
        text: 'Milk',
        leaf: true,
        checked: false
    },{
        text: 'Cereal',
        leaf: true,
        checked: false
    },{
        text: 'Energy foods',
        cls: 'folder',
        children: [{
            text: 'Coffee',
            leaf: true,
            checked: false
        },{
            text: 'Red Bull',
            leaf: true,
            checked: false
        }]
    }]
},{
    text: 'Remodel Project', 
    cls: 'folder',
    children: [{
        text: 'Finish the budget',
        leaf: true,
        checked: false
    },{
        text: 'Call contractors',
        leaf: true,
        checked: false
    },{
        text: 'Choose design',
        leaf: true,
        checked: false
    }]
}]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值