EasyUI tree常用方法

  • 初始化树
$('#EM_tree').tree({  
    data:treeData,//json格式数据
    checkbox: true,  //显示勾选框
    dnd:true,  
    onClick:function(node){//单击事件  
        }                       
        //$(this).tree('toggle', node.target);  
    },
    onLoadSuccess:function(){  //加载完成事件
        $("#EM_tree").tree("collapseAll");  //收起树节点
    }
})
  • 重载
function reload(){  
    var node = $('#EM_tree').tree('getSelected');  //当前选中节点
    //判断当前是否有选中节点
    if (node) {  
        $('#EM_tree').tree('reload', node.target);  
    }else {  
        $('#EM_tree').tree('reload');  //刷新整个树
    }  
}
  • 获取children
function getChildren(){  
    var node = $('#EM_tree').tree('getSelected');  
    if (node) {  
     //当前选中节点的children
        var children = $('#EM_tree').tree('getChildren', node.target);  
    }else {  
       //树的children
        var children = $('#EM_tree').tree('getChildren');  
    }  
    for (var i = 0; i < children.length; i++) {  
        var text = children[i].text;  
    }  
    alert(children.length);  
}
  • 获取勾选节点
function getChecked(){  
    var nodes = $('#EM_tree').tree('getChecked');  
    for (var i = 0; i < nodes.length; i++) {  
           var text = nodes[i].text;  
       }  
    alert(nodes.length);  
}
  • 获取当前选中节点
function getSelected(){  
    var node = $('#EM_tree').tree('getSelected');  
    alert(node.text);  
}  
  • 收起、展开节点
function collapse(){  
    var node = $('#EM_tree').tree('getSelected');  
    $('#EM_tree').tree('collapse', node.target);  
}  

function expand(){  
    var node = $('#EM_tree').tree('getSelected');  
    $('#EM_tree').tree('expand', node.target);  
}

function collapseAll(){  
    var node = $('#EM_tree').tree('getSelected');  
    if (node) {  
        $('#EM_tree').tree('collapseAll', node.target);  
    }else {  
        $('#EM_tree').tree('collapseAll');  
    }  
}  

function expandAll(){  
    var node = $('#EM_tree').tree('getSelected');  
    if (node) {  
        $('#EM_tree').tree('expandAll', node.target);  
    }else {  
        $('#EM_tree').tree('expandAll');  
    }  
}
  • 节点的增删改
function append(){  
    var node = $('#EM_tree').tree('getSelected');  
    $('#EM_tree').tree('append', {  
        parent: (node ? node.target : null),  
        data: [{  
            text: 'new1',  
            checked: true  
        }, {  
            text: 'new2',  
            state: 'closed',  
            children: [{  
                text: 'subnew1'  
            }, {  
                text: 'subnew2'  
            }]  
        }]  
    });  
}  

function remove(){  
    var node = $('#EM_tree').tree('getSelected');  
    $('#EM_tree').tree('remove', node.target);  
}  

function update(){  
    var node = $('#EM_tree').tree('getSelected');  
    if (node) {  
        node.text = 'new text<\/span>';  
        node.iconCls = 'icon-save';  
        $('#EM_tree').tree('update', node);  
    }  
}  

你可能感兴趣的:(easyUI)