当前位置: 首页 > >

ztree 根据子节点ID展开所有父节点

发布时间:

2019独角兽企业重金招聘Python工程师标准>>>



1、 ztree展开特定子节点的同时其余节点处于关闭状态。 原理: 利用ztree.getNodeByParam方法取得你特定的节点,然后利用expandNode()展开该节点及其父节点。 利用selectNode()方法选中该节点。效果如图:



?? 说明:默认只打开张三所属的部门,人事部是关闭状态。


?


2、 下午做了个demo,用上述思路,调试后发现无效,很是奇怪,后让同事帮我检查,原来我的数据源部分的根节点指定了open:true,导致,去掉就是想要的效果。


var setting = {
data: {
view: {
dblClickExpand: true,
showLine: false,
showTitle:true
},
key: {
name:"name",
title: "fullName"
},
simpleData: {
enable: true,
idKey:"id",
pIdKey:"pId"
}
},
callbck: {
onClick: ztreeOnClick
}
};

var empNode = [
{ id: 1, pId: 0, name: "总公司", ename: "Company", icon: "../../Content/zTreeStyle/img/diy/1_close.png", fullName: "Company" },
{ id: 11, pId: 1, name: "研发部", ename: "Develop", icon: "../../Content/zTreeStyle/img/diy/1_open.png", fullName: "Develop" },
{ id: 12, pId: 1, name: "人事部", ename: "Employee", icon: "../../Content/zTreeStyle/img/diy/1_open.png", fullName: "Employee" },
{ id: 111, pId: 11, name: "张三", ename: "zhangsan", fullName: "James san" },
{ id: 112, pId: 12, name: "李四", ename: "lisi", fullName: "James si" }
];


function ztreeOnClick(event, treeId, treeNode) {
alert(treeId + "," + treeNode.name);
}

$(function () {
$.fn.zTree.init($("#emptree"), setting, empNode);
SelectNode();
});

function SelectNode() {
var treeObj = $.fn.zTree.getZTreeObj("emptree");

var treenode = treeObj.getNodeByParam("id", 111, null);
treeObj.expandNode(treenode, true, true, true);
treeObj.selectNode(treenode);
}

?








转载于:https://my.oschina.net/garfieldzf/blog/143148



友情链接: