zTree非同步增加刪除樹節點
阿新 • • 發佈:2019-02-13
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE> ZTREE DEMO - Simple Data</TITLE>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../css/demo.css" type="text/css">
<link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../../../js/jquery.ztree.core-3.5.js"></script>
<!-- <script type="text/javascript" src="../../../js/jquery.ztree.excheck-3.5.js"></script>
<script type="text/javascript" src="../../../js/jquery.ztree.exedit-3.5.js"></script>-->
<SCRIPT type="text/javascript">
<!--
var setting = {
data: {
simpleData: {
enable: true
}
}
};
var zNodes =[
{ id:1, pId:0, name:"父節點1 - 展開", open:true},
{ id:11, pId:1, name:"父節點11 - 摺疊"},
{ id:12, pId:1, name:"葉子節點111"},
{ id:13, pId:1, name:"葉子節點112"},
{ id:2, pId:0, name:"父節點1 - 展開", open:true},
{ id:21, pId:1, name:"父節點11 - 摺疊"},
{ id:22, pId:1, name:"葉子節點111"},
{ id:23, pId:1, name:"葉子節點112"},
{ id:3, pId:0, name:"父節點1 - 展開", open:true},
{ id:31, pId:1, name:"父節點11 - 摺疊"},
{ id:32, pId:1, name:"葉子節點111"},
{ id:33, pId:1, name:"葉子節點112"},
];
$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"), setting, zNodes);
});
//-->
</SCRIPT>
</HEAD>
<BODY>
<h1>最簡單的樹 -- 簡單 JSON 資料</h1>
<h6>[ 檔案路徑: core/simpleData.html ]</h6>
<div class="content_wrap">
<div class="zTreeDemoBackground left">
<ul id="treeDemo" class="ztree"></ul>
</div>
</div>
<input type="button" value="動態增加樹節點" onclick="addTreeNode()">
<input type="button" value="動態刪除樹節點" onclick="deleteTreeNode()">
</BODY>
</HTML>
<script>
var i = 111;
function addTreeNode(){
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
var treeNode = zTree.getNodeByParam("id",3,null);
zTree.addNodes(treeNode, {id:i++, pId:3,name:"ljj"});
}
function deleteTreeNode(){
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
var treeNode = zTree.getNodeByParam("id", --i, null);
zTree.removeNode(treeNode);
}
</script>
<HTML>
<HEAD>
<TITLE> ZTREE DEMO - Simple Data</TITLE>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../css/demo.css" type="text/css">
<link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../../../js/jquery.ztree.core-3.5.js"></script>
<!-- <script type="text/javascript" src="../../../js/jquery.ztree.excheck-3.5.js"></script>
<script type="text/javascript" src="../../../js/jquery.ztree.exedit-3.5.js"></script>-->
<SCRIPT type="text/javascript">
<!--
var setting = {
data: {
simpleData: {
enable: true
}
}
};
var zNodes =[
{ id:1, pId:0, name:"父節點1 - 展開", open:true},
{ id:11, pId:1, name:"父節點11 - 摺疊"},
{ id:12, pId:1, name:"葉子節點111"},
{ id:13, pId:1, name:"葉子節點112"},
{ id:2, pId:0, name:"父節點1 - 展開", open:true},
{ id:21, pId:1, name:"父節點11 - 摺疊"},
{ id:22, pId:1, name:"葉子節點111"},
{ id:23, pId:1, name:"葉子節點112"},
{ id:3, pId:0, name:"父節點1 - 展開", open:true},
{ id:31, pId:1, name:"父節點11 - 摺疊"},
{ id:32, pId:1, name:"葉子節點111"},
{ id:33, pId:1, name:"葉子節點112"},
];
$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"), setting, zNodes);
});
//-->
</SCRIPT>
</HEAD>
<BODY>
<h1>最簡單的樹 -- 簡單 JSON 資料</h1>
<h6>[ 檔案路徑: core/simpleData.html ]</h6>
<div class="content_wrap">
<div class="zTreeDemoBackground left">
<ul id="treeDemo" class="ztree"></ul>
</div>
</div>
<input type="button" value="動態增加樹節點" onclick="addTreeNode()">
<input type="button" value="動態刪除樹節點" onclick="deleteTreeNode()">
</BODY>
</HTML>
<script>
var i = 111;
function addTreeNode(){
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
var treeNode = zTree.getNodeByParam("id",3,null);
zTree.addNodes(treeNode, {id:i++, pId:3,name:"ljj"});
}
function deleteTreeNode(){
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
var treeNode = zTree.getNodeByParam("id", --i, null);
zTree.removeNode(treeNode);
}
</script>