How to clear the layui tree
First create a tree box:
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;"> <legend>基本树</legend> </fieldset> <div style="display: inline-block; width: 180px; height: 210px; padding: 10px; border: 1px solid #ddd; overflow: auto;"> </div>
<fieldset class="layui-elem-field layui-field-title" style="max-width:90%"> <legend>基本树</legend> </fieldset> <div style="display: inline-block; width: 180px; height: 210px; padding: 10px; border: 1px solid #ddd; overflow: auto;"> <ul id="demo1"></ul> </div> <script> //Demo layui.use(['tree', 'layer'], function(){ var layer = layui.layer ,$ = layui.jquery; layui.tree({ elem: '#demo1' //指定元素 ,target: '_blank' //是否新选项卡打开(比如节点返回href才有效) ,click: function(item){ //点击节点回调 layer.msg('当前节名称:'+ item.name + '<br>全部参数:'+ JSON.stringify(item)); console.log(item); } ,nodes: [ //节点 { name: '树干' ,id: 2 ,spread: true } ] }); }); </script>
Add branches to the original trunk:
layui.use(['tree', 'layer'], function(){ var layer = layui.layer ,$ = layui.jquery; layui.tree({ elem: '#demo1' //指定元素 ,target: '_blank' //是否新选项卡打开(比如节点返回href才有效) ,click: function(item){ //点击节点回调 layer.msg('当前节名称:'+ item.name + '<br>全部参数:'+ JSON.stringify(item)); console.log(item); } ,nodes: [ //节点 { name: '树干' ,id: 2 ,spread: true ,children: [ { name: '树杈1' ,id: 21 ,spread: true }, { name: '树杈2' ,id: 22 } ] } ] });
Add branches based on the previous ones:
layui.tree({ elem: '#demo1' //指定元素 ,target: '_blank' //是否新选项卡打开(比如节点返回href才有效) ,click: function(item){ //点击节点回调 layer.msg('当前节名称:'+ item.name + '<br>全部参数:'+ JSON.stringify(item)); console.log(item); } ,nodes: [ //节点 { name: '树干' ,id: 2 ,spread: true ,children: [ { name: '树杈1' ,id: 21 ,spread: true ,children: [ { name: '树枝' ,id: 211 } ] }, { name: '树杈2' ,id: 22 ,children: [ { name: '树枝' ,id: 221 } ] } ] } ] });
Add leaves based on the previous one:
layui.tree({ elem: '#demo1' //指定元素 ,target: '_blank' //是否新选项卡打开(比如节点返回href才有效) ,click: function(item){ //点击节点回调 layer.msg('当前节名称:'+ item.name + '<br>全部参数:'+ JSON.stringify(item)); console.log(item); } ,nodes: [ //节点 { name: '树干' ,id: 2 ,spread: true ,children: [ { name: '树杈1' ,id: 21 ,spread: true ,children: [ { name: '树枝' ,id: 211 ,children: [ { name: '树叶1' ,id: 2111 }, { name: '树叶2' ,id: 2112 }, { name: '树叶3' ,id: 2113 } ] } ] }, { name: '树杈2' ,id: 22 ,children: [ { name: '树枝' ,id: 221 } ] } ] } ] });
Add a clear button:
<button class="layui-btn">清空</button>
Click the clear button and call the click event to clear the tree
$(".layui-btn").click(function(){ $('ul li').remove(); });
Method/Step 2
Complete code :
layui
For more technical articles related to Layui, please visit the Layui Framework Tutorial column to learn!
The above is the detailed content of How to clear the layui tree. For more information, please follow other related articles on the PHP Chinese website!