下载ztree
image.png
html页面引入
jquery写法
$(function () {
initZTree();
});
function initZTree() {
$.ajax({
url: "/admin/depart/get-depart/",
type: "post",
dataType: "json",
async: false,
success: function(data) {
var setting = {
async: {
enable: true,
url: "/admin/depart/get-depart/",
autoParam: ["id", "pid", "name"],
},
data: {
simpleData: {
enable: true,
idKey: 'id',
pIdKey: 'pid',
rootPId: 0
}
},
callback: {
onAsyncSuccess: function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
var menuIds = "{{ $ids }}".split(',');
for(var i = 0; i < treeNode.children.length; i++) {
for(var j = 0; j < menuIds.length; j++) {
//第二种方法node.checked = true;
//再更新节点zTree.updateNode(node);
if(treeNode.children[i].id == menuIds[j])
treeNode.children[i].checked = true
zTree.updateNode(treeNode.children[i]);
}
}
},
//onCheck: click
},
check: {
enable: true,
chkboxType: { "Y": "ps", "N": "ps" }
},
view: {
//showIcon: false
}
};
var zTree = $.fn.zTree.init($("#zTree"), setting, data);
var menuIds = "{{ $ids }}".split(','); //后端返回选中ids
for(var i = 0; i < menuIds.length; i++) {
//zTree.checkNode(node, true, true);
var node = zTree.getNodeByParam("id", menuIds[i]);
if(node != null) {
zTree.checkNode(node, true)
}
}
}
});
}
HTML 页面 写法
部门
{{-- --}}
{{--
页面效果
image.png