Ztree异步加载时怎么获得刚刚添加到树上的节点啊
我有这样一个需求,有这样一个查询按钮,执行下,就会把后台中查询的相关数据和它的父节点和父节点下的一级子节点数据都添加到现有的Ztree中,但是,添加完毕后,我怎么也得不到它的父节点。
<html> <head> <title>bomTreeLeftDictionary.jsp</title> <link rel="stylesheet" type="text/css" href="${path }/css/style.css"/> <link rel="stylesheet" href="${path}/JQuery zTree v3.1/css/demo.css" type="text/css"> <link rel="stylesheet" href="${path}/JQuery zTree v3.1/css/zTreeStyle/zTreeStyle.css" type="text/css"> <script type="text/javascript" src="${path}/JQuery zTree v3.1/js/jquery-1.4.4.min.js"></script> <script type="text/javascript" src="${path}/JQuery zTree v3.1/js/jquery.ztree.core-3.1.js"></script> <!--<script type="text/javascript" src="${path}/JQuery zTree v3.1/js/jquery.ztree.excheck-3.1.js"></script>--> <script type="text/javascript" src="${path}/JQuery zTree v3.1/js/jquery.ztree.exedit-3.1.js"></script> <script type="text/javascript"> var rootId = ${param.partId}; var rootName = ${param.partName}; var zNodes =[ { id:rootId, parentId:-1, name:rootName, open:false,isParent:true}]; var setting = { view: { selectedMulti: false }, async: { enable: true, url:"bomZTreeAction_searchSBomTreeChildrenNodes.do", autoParam:["id"], isSimpleData : true, treeNodeKey : "id", treeNodeParentKey : "parentId" }, callback: { onClick: zTreeOnClick } }; function zTreeOnClick(event, treeId, treeNode, clickFlag) { var actionURL = "/epcportal/repositoryViewAction_viewAllPart.action?childPartId="+treeNode.id; window.parent.frames["content"].location = actionURL; } // 查询树 var idIndex = -1; var ids = new Array(); function searchSBomPartIds(searchInputName) { var searchValue = $("#searchPart") //alert("searchTree -- searchValue : " + searchValue); setting.asyncUrl = "/epcportal/bomZTreeAction_searchSBomPartIds.action?bName='dd'" ; //alert(setting.asyncUrl); $.ajax({ type: "post", url: setting.asyncUrl, dataType: "json", async:true, success: function (data) { initSearchIds(data); } }); //zTreeObj = $.fn.zTree.init($("#tree"), setting, zNodes); } function initSearchIds(data){ var objs = eval(data); for(var i=0;i<objs.length;i++){ ids[i] = objs[i].ID; //alert("initSearchIds --i:"+ i+ " --ID:"+objs[i].ID); } if(objs.length > 0) { searchSBomPartInfosById(); } //alert("initSearchIds --ids:" +ids); } function searchSBomPartInfosById() { //alert("searchTree -- searchValue : " + searchValue); idIndex = idIndex + 1; if(idIndex == ids.length-1){ idInex = -1; } //alert("searchSBomPartInfosById: "+ids[idIndex]); setting.asyncUrl = "/epcportal/bomZTreeAction_searchSBomPartInfosById.action?id=" +ids[idIndex]; //alert("searchSBomPartInfosById -- setting.asyncUrl :" + setting.asyncUrl); $.ajax({ type: "post", url: setting.asyncUrl, dataType: "json", async:true, success:function (data){ addNodesToTree(data) } }); //zTreeObj = $.fn.zTree.init($("#tree"), setting, zNodes); } var rootNode; function addNodesToTree(data){ //alert(data); var objs = eval(data); var nodeId; var nodeName; var isParent; var pNodeId; var pNode; var newNode; var zTree = $.fn.zTree.getZTreeObj("treeDemo"); var rootNode = zTree.getNodeByParam("id",rootId,null); for(var i=0;i<objs.length;i++){ nodeId = objs[i].id; pNodeId = objs[i].parentId; isParent = objs[i].isParent; nodeName = objs[i].name; zTree.updateNode(objs[i]); zTree.reflash; //alert("addNodesToTree ---- newNodeId "+ i + " :" + nodeId + " pNodeId: "+ pNodeId); /* pNode = zTree.getNodeByParam("id",pNodeId,null); if(pNode == null) { //alert("addNodesToTree ---pNode : "+ i +" id: " + pNodeId+ " isNull"); pNode = zTree.getNodeByTId(pNodeId); if(pNode == null){ alert("addNodesToTree --- pNode : "+ i +" id: " + pNodeId+ " pNode == null"); continue; } } newNode = zTree.getNodeByParam("id",nodeId,null); if(newNode != null){ alert("new Node "+nodeId+" != null"); continue; } if(nodeId != rootNode.id){ //alert("add success : newNode Id :"+ nodeId); pNode = zTree.addNodes(pNode,{id:nodeId,parentId:pNodeId,isParent:isParent,name:nodeName}); zTree.reAsyncChildNodes(pNode, "add", true); zTree.selectNode(pNode); pNode.open=true; zTree.expandNode(pNode,true); pNode.zAsync = true; zTree.reflash; }*/ //alert("add success : newNode Id :"+ newNode.id); //pNode = zTree.addNodes(pNode,newNode); //alert("addNodesToTree -- : " +i+" : " + pNode.id); } } $(document).ready(function(){ $.fn.zTree.init($("#treeDemo"), setting,zNodes) }); </script> </head> <body> <div align="left" style="padding-left: 3px;font-size: 12; color: #81b432;"> <b>SBOM结构图册</b> </div> <table cellpadding="0" cellspacing="0" style="border:1px;"> <tr> <td valign="middle" style="font-size: 12;color: #81b432;"> 零部件名称: </td> <td valign="bottom"><input type="text" id="searchPart" name="searchPart" size="10" value=""></td> <td><img alt="查询" src="${path}/images/search.png" onclick="searchSBomPartIds('searchPart');"></img></td> </tr> </table> <div> <ul id="treeDemo" class="ztree"></ul> </div> </body> </html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你好,时间有限,无法详细看全你的代码,只是着重看了你异步加载后的操作。
1、新增节点请使用 addNodes 方法,不要使用updateNode 方法,这个updateNode只适用于更新已有节点数据的。
2、利用 addNodes 方法添加节点完全不需要 refresh 方法进行刷新
3、看来你已经知道 getNodeByParam 方法,应该可以随便获取任何需要得到的节点数据了。
先把以上功能调通,再看其他的问题吧。