深度遍历树结构时用了递归函数:
function fillList() { $("tbody").html(""); var htmlStr=""; if (C_module&&C_module.length>0) { for (var i=0; i<C_module.length; i++) { deepTraverFill(C_module[i], htmlStr); } } $('.tree').treegrid(); function deepTraverFill(treeNode, htmlStr) { moduleArr.push(treeNode); if (!treeNode) return; htmlStr= "<tr class='treegrid-" + (treeNode.id) + ( (treeNode.pId=='0'||!treeNode.pId)?"":" treegrid-parent-" + (treeNode.pId)) + "'>" + "<td class='tdLeft'>" + treeNode.moduleName + "</td>" + "<td>" + treeNode.url + "</td>" + "<td>" + (treeNode.isValidate==1?"有效":"无效") + "</td>"+ "<td>"+ "<button type='button' class='btn btn-primary btn-xs' data-toggle='modal' data-target='#myModal' onclick=showUpdateModal('" + treeNode.id + "')>编辑</button>"+ "<button type='button' class='btn btn-danger btn-xs del' onclick=deleteData('" + treeNode.id + "')>删除</button>"+ "</td></tr>"; $("tbody").append(htmlStr); if (treeNode.moduleList&&treeNode.moduleList.length>0) { for (var i=0; i<treeNode.moduleList.length; i++) { deepTraverFill(treeNode.moduleList[i], htmlStr); } } } }
注:全息项目-权限管理