layui tree实现获取子节点所有值的实例代码 发布时间:2024/12/06 layui tree layui tree实现获取子节点所有值的实例代码,具体代码如下: layui.use(['tree', 'form'], function(){ var tree = layui.tree; var form = layui.form; // 模拟数据 var data = [ { name: '节点1', id: 1, children: [ {name: '节点1-1', id: 11, children: [{name: '节点1-1-1', id: 111}]}, {name: '节点1-2', id: 12} ] } // ... 其他节点数据 ]; // 初始化树 tree.render({ elem: '#test' // 绑定元素 ,data: data ,showCheckbox: true // 显示复选框 ,id: 'id' // 设置节点id的属性名 ,oncheck: function(obj){ var childIds = getAllChildIds(obj.data, []); console.log(childIds); } }); // 递归获取所有子节点的ID function getAllChildIds(node, ids) { if (node.children && node.children.length > 0) { node.children.forEach(function(child) { ids.push(child.id); getAllChildIds(child, ids); }); } return ids; } });复制代码