Jquery easyui tree 一些常见操作

遍历treeNode
function Travel(treeID){//参数为树的ID,注意不要添加#
    var roots=$('#'+treeID).tree('getRoots'),children,i,j;
    for(i=0;i<roots.length;i++){
        alert(roots[i].text);
        children=$('#'+treeID).tree('getChildren',roots[i].target);
        for(j=0;j<children.length;j++)alert(children[j].text);
    }
}

/**
 * 初始化tree
 */
$(function(){
    $('#tt2').tree({
        checkbox: true,
        url: 'tree_data.json',
        onClick: function(node){
            $(this).tree('toggle', node.target);
        },
        onContextMenu: function(e, node){
            e.preventDefault();
            $('#tt2').tree('select', node.target);
            $('#mm').menu('show', {
                left: e.pageX,
                top: e.pageY
            });
        }
    });
});

/**
 * 刷新节点
 */
function reload(){
    var node = $('#tt2').tree('getSelected');
    if (node) {
        $('#tt2').tree('reload', node.target);
    }
    else {
        $('#tt2').tree('reload');
    }
}

/**
 * 获取孩子节点
 */
function getChildren(){
    var node = $('#tt2').tree('getSelected');
    if (node) {
        var children = $('#tt2').tree('getChildren', node.target);
    }
    else {
        var children = $('#tt2').tree('getChildren');
    }
    var s = '';
    for (var i = 0; i < children.length; i++) {
        s += children[i].text + ',';
    }
    alert(s);
}

/**
 * 获取选中复选框节点内容
 */
function getChecked(){
    var nodes = $('#tt2').tree('getChecked');
    var s = '';
    for (var i = 0; i < nodes.length; i++) {
        if (s != '')
            s += ',';
        s += nodes[i].text;
    }
    alert(s);
}

/**
 * 获取选中节点
 */
function getSelected(){
    var node = $('#tt2').tree('getSelected');
    alert(node.text);
}

/**
 * 折叠指定节点
 */
function collapse(){
    var node = $('#tt2').tree('getSelected');
    $('#tt2').tree('collapse', node.target);
}

/**
 * 展开指定节点
 */
function expand(){
    var node = $('#tt2').tree('getSelected');
    $('#tt2').tree('expand', node.target);
}

/**
 * 折叠所有节点
 */
function collapseAll(){
    var node = $('#tt2').tree('getSelected');
    if (node) {
        $('#tt2').tree('collapseAll', node.target);
    }
    else {
        $('#tt2').tree('collapseAll');
    }
}

/***
 * 展开所有节点
 */
function expandAll(){
    var node = $('#tt2').tree('getSelected');
    if (node) {
        $('#tt2').tree('expandAll', node.target);
    }
    else {
        $('#tt2').tree('expandAll');
    }
}

/**
 * 追加节点
 */
function append(){
    var node = $('#tt2').tree('getSelected');
    $('#tt2').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 = $('#tt2').tree('getSelected');
    $('#tt2').tree('remove', node.target);
}

/**
 * 更新节点
 */
function update(){
    var node = $('#tt2').tree('getSelected');
    if (node) {
        node.text = '<span style="font-weight:bold">new text<\/span>';
        node.iconCls = 'icon-save';
        $('#tt2').tree('update', node);
    }
}

/**
 * 判断是否是叶子节点
 */
function isLeaf(){
    var node = $('#tt2').tree('getSelected');
    var b = $('#tt2').tree('isLeaf', node.target);
    alert(b)
}

/**
 * 根据类型获取节点
 * @param type
 * @returns {string}
 * @constructor
 */
function GetNode(type){
    var node = $('#tt2').tree('getChecked');
    var chilenodes = '';
    var parantsnodes = '';
    var prevNode = '';
    for (var i = 0; i < node.length; i++) {

        if ($('#tt2').tree('isLeaf', node[i].target)) {
            chilenodes += node[i].text + ',';

            var pnode = $('#tt2').tree('getParent', node[i].target);
            if (prevNode != pnode.text) {
                parantsnodes += pnode.text + ',';
                prevNode = pnode.text;
            }
        }
    }
    chilenodes = chilenodes.substring(0, chilenodes.length - 1);
    parantsnodes = parantsnodes.substring(0, parantsnodes.length - 1);

    if (type == 'child') {
        return chilenodes;
    }
    else {
        return parantsnodes
    };
};

function getNodes(){
    alert(GetNode('fnode') + "," + GetNode('child'));
}

function doNode(){
    var c="";
    var p="";
    $(".tree-checkbox1").parent().children('.tree-title').each(function(){
        c+=$(this).parent().attr('node-id')+",";
    });
    $(".tree-checkbox2").parent().children('.tree-title').each(function(){
        p+=$(this).parent().attr('node-id')+",";
    });
    var str=(c+p);
    str=str.substring(0,str.length-1);
    alert(str);
}

 

posted @ 2017-10-20 20:30  月眸  阅读(2932)  评论(0编辑  收藏  举报