我有一个叫做 boundedArea
的对象,它在字段 children
中包含了一个 boundedArea
对象数组,我想创建一个无序列表树。
我有以下代码:
- for (var index = 0; index < rootAreas.length; index++) {
- var boundedArea = rootAreas[index];
div(class='panel panel-default')
div.panel-heading
| #{boundedArea.NAME}
div.panel-body
- printChildren(boundedArea, 0);
- }
-
- function printChildren(boundedArea, depth) {
- var children = boundedArea.children;
- if (children == null || children.length == 0) {
- return;
- }
ul
- for (var index = 0; index < children.length; index++) {
- var child = children[index];
li #{child.NAME}
- console.log("Printing %s child of %s", child.NAME, boundedArea.NAME);
- printChildren(child, depth + 1);
- }
- }
显然,这样做可以输出所有的值。然而,由于 ul
和 li
标签具有固定的缩进,它们不会嵌套,只会按顺序打印。
是否有任何方法可以动态设置缩进级别或强制嵌套这些标签?或者我应该使用完全不同的嵌套模型。
我尝试创建一个名为 "indent" 的javascript变量,每个深度级别填充两个空格,然后尝试使用 #{indent}
但结果只是创建了带有空格的标签,这不是我想要的。虽然这表明在某种程度上,这个想法周围的东西可能有效,因为它必须在某个级别上解决,然后被视为某种标记。