本文主要是介绍zTree获得选择节点的值以及回显选中,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<script>
function filter(node) { //过滤器直选中2级节点累加return (node.level == 2 && node.checked == true);
}var setting = { //设置check: {enable: true},callback: {onCheck: onCheck
}};function onCheck(e, treeId, treeNode){ //这是将所有选中节点的名字 用,分割做拼接 用于持久化到数据库var zTree = $.fn.zTree.getZTreeObj("tree").getNodesByFilter(filter);var choose = "";for (var i=0;i<zTree.length;i++) {if(zTree[i].name !=null)choose +=(i == (zTree.length-1))?zTree[i].name:zTree[i].name+",";}$("#ztreesnames").val(choose);treeIds();
}function treeIds(){ //这是将选中的节点的id用;分割拼接起来,用于持久化到数据库var zTreeO = $.fn.zTree.getZTreeObj("tree").getNodesByFilter(filter);var idListStr = "";for (var i = 0; i < zTreeO.length; i++) {if (zTreeO[i].id != null) {idListStr+= (i == (zTreeO.length-1))?zTreeO[i].id:zTreeO[i].id+";";}};$("#ztreeids").val(idListStr);
};$(document).ready(function(){ /**这是处理回显选中的方法*/$.fn.zTree.init($("#tree"), setting, zNodes); var zTreeObj = $.fn.zTree.getZTreeObj("tree")var zTree = zTreeObj.getCheckedNodes(false);var pid= "${personCode}"; /**此处数据前后必须拼接;*/for (var i = 0; i < zTree.length; i++) {if (pid.indexOf(";" + zTree[i].id + ";") != -1) {zTreeObj.expandNode(zTree[i], true); //展开选中的zTreeObj.checkNode(zTree[i], true); }}onCheck();
});
</script>
<html>
<input type="hidden" id="ztreeids"/>
<input type="hidden" id="ztreesnames"/>
</html> 这篇关于zTree获得选择节点的值以及回显选中的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!