bootstrap-treeview

var reloadDepartment = function(){
	$.ajax({
        url:"../../department/findAll",
        type:"POST",
        contentType:'application/json',
        async: false,
        data: JSON.stringify({"helpId":"hello"}),//随便往后台传一个值
        success:function(result){
            if(result!=null){
				topNode.id = result.data[0].id;
				topNode.text = result.data[0].text;
                $("#departName").click(function() {
                    let options = {
                        levels : 1,
                        data : result.data,
                             multiSelect:true,
                        showCheckbox: 1,//这里之所以写1,是因为我引得js源码里定义1为true
                        multiSelect: 1,//这里之所以写1,是因为我引得js源码里定义1为true
                        collapseIcon:"treegrid-expander treegrid-expander-expanded",
                        expandIcon:'treegrid-expander treegrid-expander-collapsed',
                        onNodeSelected : function(event, data) {
                            $("#departName").val(data.text);
                            $("#depart_id").val(data.id);
                            $("#tree").hide();//选中树节点后隐藏树
                            //ajax判断
                            checkNameAndDepartment();
                        },
                        onNodeChecked: function(event, node) { //选中节点
                            var selectNodes = getChildNodeIdArr(node); //获取所有子节点
                            if (selectNodes) { //子节点不为空,则选中所有子节点
                                $('#tree_ul_id').treeview('checkNode', [selectNodes, { silent: true }]);
                            }
                            var parentNode = $("#tree_ul_id").treeview("getNode", node.parentId);
                            setParentNodeCheck(node);
                        },
                        onNodeUnchecked: function(event, node) { //取消选中节点
                            var selectNodes = getChildNodeIdArr(node); //获取所有子节点
                            if (selectNodes) { //子节点不为空,则取消选中所有子节点
                                $('#tree_ul_id').treeview('uncheckNode', [selectNodes, { silent: true }]);
                            }
                        }
                    };
                    $('#tree').treeview(options);
                    $('#tree').treeview('getChecked');
                    $('#tree').click(function (event) {
                        stopPropagation(event);
                        console.log($('#tree').treeview('getChecked'));
                    });
                });
            }
        },
    });
    departmentList( 'searchall', '');
};
function getChildNodeIdArr(node) {
    var ts = [];
    if (node.nodes) {
        for (x in node.nodes) {
            ts.push(node.nodes[x].nodeId);
            if (node.nodes[x].nodes) {
                var getNodeDieDai = getChildNodeIdArr(node.nodes[x]);
                for (j in getNodeDieDai) {
                    ts.push(getNodeDieDai[j]);
                }
            }
        }
    } else {
        ts.push(node.nodeId);
    }
    return ts;
}

function setParentNodeCheck(node) {
    var parentNode = $("#tree").treeview("getNode", node.parentId);
    if (parentNode.nodes) {
        var checkedCount = 0;
        for (x in parentNode.nodes) {
            if (parentNode.nodes[x].state.checked) {
                checkedCount ++;
            } else {
                break;
            }
        }
        if (checkedCount === parentNode.nodes.length) {
            $("#tree").treeview("checkNode", parentNode.nodeId);
            setParentNodeCheck(parentNode);
        }
    }
}

let $table = $('#department');
let topNode = {id:0,text:''};
// 格式化按钮
function operateFormatter(value, row, index) {
    return [
        '编辑 ',
        (row.id==1?'' : '删除')
    ].join('');

}
$('.modal').draggable();
$('.modal').css('background','rgba(0,0,0,0)');
//初始化编辑删除按钮的方法
window.operateEvents = {
        'click .RoleOfdelete': function (e, value, row, index) {
            del(row);
        },
        'click .RoleOfedit': function (e, value, row, index) {
            $.ajax({
                    "url":"../../department/findParentName",
                    type:"GET",
                    dataType:"json",
                    data: {
                        parent_id:row.id
                    },
                    success:function(result){
                        $('#departName').val(result.name);
                    }
            });
            update(row);
        }
};
//点击空白关闭tree控件
$('#modal-default2').click(function(event){
    $('#tree').css("display","none");
});
$('#departName').click(function (event) {
    stopPropagation(event);
    $('#tree').css("display","block");
});
function stopPropagation(e) {
    e = e || window.event;
    if(e.stopPropagation) { //W3C阻止冒泡方法
        e.stopPropagation();
    } else {
        e.cancelBubble = true; //IE阻止冒泡方法
    }
}
function update(row) {
    $('#modal-default2 .modal-title').text('编辑部门');
    if (row.id==1){
        $("#first_department").css("display",'none');
        $('#depart_id').val();
    }else {
        $("#first_department").css("display",'block');
    }
    $('#name').val(row.name);
    $('#description').val(row.description);
    $('#edit_id').val(row.id);
    $('#depart_id').val(row.parent_id);
    $('#tree').hide();
    $("#Vname").text('');
    $("#VdepartName").text('');
    $("#Vdescription").text('');
    $("#modal-default2").modal();
}
function del(row) {
    $('#del_id').val(row.id);
    $('#modal-deldepart').modal();
}

你可能感兴趣的:(#,Jquery)