【问题标题】:Recursive Handlebars.js Template. How to determine the depth?递归 Handlebars.js 模板。如何确定深度?
【发布时间】:2016-04-05 13:12:30
【问题描述】:

经过数小时的搜索,我找不到我的问题的正确答案。我想用车把显示任意树深度。把手中的递归模板有一个很好的小提琴示例(https://jsfiddle.net/adamboduch/5yt6M/),但我无法获得树的深度。 @index 只告诉我每个元素的位置。

我的部分模板:

<script id="teamspeak_channel_recursive" type="text/x-handlebars-template">
   {{#each childChannel}}
   <tr class="viewer-table-row">
     <td class="viewer-table-cell">
       <span></span>
       <span class="viewer-label">{{name}}</span>
       <span></span>
     </td>
   </tr>
        {{#if childChannel}}
        {{> teamspeak_channel_recursive}}
        {{/if}}
    {{/each}}
 </script>

我想通过 css margin left 或 css class 根据 deph 来命名。我也尝试过使用参数,但根本不允许计算数字或进行任何数学运算。数学助手也没有帮助。据我所知,模板中的 Javascript 是被禁止的。

<script id="teamspeak_channel_recursive" type="text/x-handlebars-template">
   {{#each childChannel}}
   <tr class="viewer-table-row">
     <td class="viewer-table-cell">
       <span></span>
       <span class="viewer-label">{{name}}</span>
       <span></span>
     </td>
   </tr>
        {{#if childChannel}}
        {{> teamspeak_channel_recursive deph=({{../deph}}+1) }} <- dosen't work only statc values or the context itself is working
        {{/if}}
    {{/each}}
 </script>

简而言之,我被卡住了,除了使用有序列表并将显示模式设置为 tablecell 之外,我没有找到出路。但这不是我想要的。同样迭代上下文以在之前添加递归级别也不是一个好的选择。

模型(不完整):

type": "channel",
"childChannel": 
[
{
    "type": "channel",
    "childChannel": 
[
        {
            "type": "channel",
            "childChannel": null,
            "childClient": null,
            "name": "AFK Area"
        }
    ],
    "childClient": null,
    "name": "WoW / SWToR / GuildWars2 hype"
},
{
    "type": "channel",
    "childChannel": 
[
            {
                "type": "channel",
                "childChannel": null,
                "childClient": null,
                "name": "Rumidler (AFK)"
            }
        ],
        "childClient": null,
        "name": "FPS CS:GO Hype"
    }

],
"childClient": null,
"name": "Hall of Games"

【问题讨论】:

    标签: javascript html templates recursion handlebars.js


    【解决方案1】:

    我怀疑使用 Handlebars Partials 无法做到这一点。不过,递归助手可以解决问题。我已经使用您之前链接的 Fiddle 作为最小概念证明here 的基础。您可以更新以使用您自己的数据结构和 HTML,但它基本上看起来像这样:

    var listHelperOutput = '';   
    
    function recursiveList(stuff, depth){  
        listHelperOutput += '<ul>';
        stuff.forEach(function(el){             
            listHelperOutput += '<li>';
            listHelperOutput += el.name + ' (depth ' + depth + ')';
            if (el.items){              
                recursiveList(el.items, depth + 1);            
            }
            listHelperOutput += '</li>';
        });
        listHelperOutput += '</ul>';
        return new Handlebars.SafeString(listHelperOutput);
    }
    
    Handlebars.registerHelper('listHelper', recursiveList);
    

    在你的模板中调用它:

    {{listHelper items 1}}
    

    【讨论】:

    • 最后它更复杂,但你帮助我朝着正确的方向前进。谢谢!我将助手更改为块助手,因此我可以在模板中选择 html 元素和输出。它仍然与模型紧密耦合。如果有人问我可以在这里发布我完成的助手。
    猜你喜欢
    • 1970-01-01
    • 2014-05-13
    • 2021-03-23
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 2017-07-03
    相关资源
    最近更新 更多