一、zTree展示到指定層級(jí)的數(shù)據(jù)
1.在項(xiàng)目開(kāi)發(fā)過(guò)程中,組織結(jié)構(gòu)樹(shù)只想展示到第三級(jí)的數(shù)據(jù)暖侨,數(shù)據(jù)源的數(shù)據(jù)結(jié)構(gòu)無(wú)數(shù)據(jù)層級(jí)信息椭住,無(wú)奈只能從前端下手崇渗。
解決思路:zTree渲染數(shù)據(jù)后每個(gè)節(jié)點(diǎn)有層級(jí)level,只要遍歷每個(gè)節(jié)點(diǎn)京郑,把不要的層級(jí)數(shù)據(jù)剔除掉即可宅广。
2.代碼如下:
/**
** zTreeObj,zTree對(duì)象
** treeLvl些举,展示到treeLvl+1級(jí)的數(shù)據(jù)
**/
funtion showTreeDataByLvl(zTreeObj, treeLvl) {
//獲取zTree所有節(jié)點(diǎn)
var nodes = zTreeObj.transformToArray(zTreeObj.getNodes());
for (var i = 0; i < nodes.length; i++) {
if (nodes[i] == treeLvl) {
//移除指定層級(jí)的所有子節(jié)點(diǎn)
zTreeObj.removeChildNodes(nodes[i]);
}
}
}
二跟狱、zTree展開(kāi)到指定層級(jí)的數(shù)據(jù)
1.項(xiàng)目開(kāi)發(fā)過(guò)程中,菜單樹(shù)有多個(gè)層級(jí)户魏,想要指定展開(kāi)到某一級(jí)節(jié)點(diǎn)驶臊。
2.代碼如下:
/**
** isRoot挪挤,是否是根節(jié)點(diǎn)
** zTreeObj,zTree對(duì)象
** childNodes关翎,遍及節(jié)點(diǎn)
** treeLvl扛门,zTree展開(kāi)到treeLvl+2級(jí)的數(shù)據(jù)
**/
function showTreeMenuNum(isRoot, zTreeObj, childNodes, treeLvl) {
if (isRoot) {
var rootNodes = zTreeObj.getNodes();
showTreeMenuNum(false, zTreeObj, rootNodes, treeLvl);
} else {
if (!isEmpty(childNodes) && !isEmpty((len = childNodes.length)) && len > 0) {
if(treeLvl < childNodes[0].level) {
return;
}
for (var i = 0; i < len; i++) {
zTreeObj.expand(childNodes[i], true, false, false, true);
var child = childNodes[i].children;
showTreeMenuNum(false, zTreeObj, child , treeLvl);
}
}
}
}
/**
** 判斷給定的對(duì)象obj是否為空
**/
function isEmpty(obj) {
try {
return typeof(obj) == 'undefined' || null = obj || obj.replace(/\s/g, '') == '';
} catch (e) {
return false;
}
}
3.異步加載數(shù)據(jù)使用如下:
callback : {
/*樹(shù)異步加載成功后操作*/
onAsyncSuccess :function() {
//zTreeObj.expandAll(true);
showTreeMenuNum(true, zTreeObj, zTreeObj, 3);
}
}