zTree 异步加载 添加子节点重复问题 .

最近写程序需要一颗可以一步加载的树,发现ztree功能很强大。搞了好久才知道怎么实现树节点的异步加载,

在这里记录下来以方便以后自己忘记了。代码如下:

  1. <spanstyle="font-size:14px;"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  2. <htmlxmlns="http://www.w3.org/1999/xhtml">

  3. <head>

  4. <metahttp-equiv="Content-Type"content="text/html; charset=GBK"/>

  5. <title></title>

  6. <#global path = request.getContextPath() >

  7. <!--以下为,需要引入的css和js类库-->

  8. <linkrel="stylesheet"href="${path}/ztree/css/zTreeStyle/zTreeStyle.css"type="text/css"/>

  9. <scripttype="text/javascript"src="${path}/ztree/js/jquery-1.4.4.min.js"></script>

  10. <scripttype="text/javascript"src="${path}/ztree/js/jquery.ztree.core-3.1.js"></script>

  11. <script>

  12. <!--

  13. var zTree;

  14. var productIframe;

  15. var setting = {

  16. async: {

  17. enable: true,//启用异步加载

  18. url:"${path}/pmproductAction!producttree.action", //异步请求地址

  19. autoParam:["id", "str1"], //需要传递的参数,为你在ztree中定义的参数名称

  20. otherParam:{"chk":"chk"}

  21. },

  22. view: {

  23. dblClickExpand: false,

  24. showLine: true,

  25. selectedMulti: false,

  26. expandSpeed: ($.browser.msie && parseInt($.browser.version)<=6)?"":"fast"

  27. },

  28. data: {

  29. simpleData: {

  30. enable: true

  31. }

  32. },

  33. callback: {

  34. onAsyncSuccess: onAsyncSuccess,

  35. beforeClick: function(treeId, treeNode)

  36. var zTree = $.fn.zTree.getZTreeObj("ztree");

  37. if (treeNode.isParent) {

  38. zTree.expandNode(treeNode);

  39. return false;

  40. } else {

  41. productIframe.attr("src",treeNode.url);

  42. return true;

  43. }

  44. }

  45. }

  46. };

  47. function onAsyncSuccess(event, treeId, treeNode, msg) {

  48. cancelHalf(treeNode);

  49. }

  50. function cancelHalf(treeNode) {

  51. var zTree = $.fn.zTree.getZTreeObj("ztree");

  52. treeNode.halfCheck = false;

  53. zTree.updateNode(treeNode); //异步加载成功后刷新树节点

  54. }

  55. $(document).ready(function(){

  56. //以下为第一次要加载的一级节点

  57. $.post("${path}/pmproductAction!producttree.action",{id:0,str1:"type"},function(result){

  58. $.fn.zTree.init($("#ztree"), setting, result);

  59. });

  60. productIframe = $("#products");

  61. productIframe.bind("load", loadReady);

  62. });

  63. function loadReady() {

  64. var bodyH = productIframe.contents().find("body").get(0).scrollHeight,

  65. htmlH = productIframe.contents().find("html").get(0).scrollHeight,

  66. maxH = Math.max(bodyH, htmlH), minH = Math.min(bodyH, htmlH),

  67. h = productIframe.height() >= maxH ? minH:maxH ;

  68. if (h <530) h = 530;

  69. productIframe.height(h);

  70. }

  71. //-->

  72. </script>

  73. </head>

  74. <body>

  75. <tableborder=0height=500pxalign=left>

  76. <tr>

  77. <tdwidth="20%"align="left"valign="top"style="BORDER-RIGHT: #999999 1px dashed">

  78. <ulid="ztree"class="ztree"style="overflow:auto;"></ul>

  79. </td>

  80. <tdwidth="80%"align="left"valign="top">

  81. <iframename="products"id="products"scrolling="auto"width="100%"height="500px"frameborder="0"></iframe>

  82. </td>

  83. </tr>

  84. </table>

  85. </body>

  86. </html>

  87. </span>


你可能感兴趣的:(ztree,Web,异步加载)