做产品时发现有的树节点名称太长,想截取制定长度显示,其余用"..."省略显示,仔细翻阅一下ztree文档发现ztree提供了一个addDiyDom方法,这不就是自己想找的diy dom方法吗,发现ztree果然强大。
1.首先不要忘记在view中配置addDiy:addDiyDom
view: {
showLine: false,
showIcon: false,
selectedMulti: false,
dblClickExpand: false,
addDiyDom: addDiyDom
},
2.下面就可以按照自己的意愿来显示树节点了
function addDiyDom(treeId, treeNode) {
var spaceWidth = 5;
var switchObj = $("#" + treeNode.tId + "_switch"),
icoObj = $("#" + treeNode.tId + "_ico");
switchObj.remove();
icoObj.before(switchObj);
if (treeNode.level > 1) {
var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level)+ "px'></span>";
switchObj.before(spaceStr);
}
var spantxt=$("#" + treeNode.tId + "_span").html();
if(spantxt.length>15){
spantxt=spantxt.substring(0,15)+"...";
$("#" + treeNode.tId + "_span").html(spantxt);
}
}