Play 框架 2 中的递归5.X

Recursion in Play framework 2.5.X

我有一个播放模板,我在其中循环遍历具有嵌套列表的对象列表,并使用嵌套 for 循环迭代显示项目。

-skillList 是一个列表[技能]

-技能是模型中定义的对象

-skillObject.getChildrenList() return 列表[技能]

我想做的是能够显示它,但使用某种类型的递归,这样如果 mylist 的嵌套级别发生变化,我就不必更改整个模板。那么有没有办法以递归的方式做到这一点?

<div class="custom-dd dd dd-nodrag" id="nestable_list_1">
<ol class="dd-list">
@for(skill <- skillList) {
    <li class="dd-item">
        <div class="dd-handle dd-nodrag row">
            <span class="dd-nodrag" id="content_@skill.getCleanUri()"> @skill.getLabel() </span>
        </div>

    <ol class="dd-list">
    @for((child, indexChild) <- skill.getChildrenList().zipWithIndex) {
        <li class="dd-item" data-id="@indexChild">
            <div class="dd-handle dd-nodrag row">
                <span id="content_@child.getCleanUri()"> @child.getLabel() </span>
            </div>

            <ol class="dd-list">
            @for((grandChild, indexGrandChild) <- child.getChildrenList().zipWithIndex) {
                <li class="dd-item" data-id="@indexGrandChild">
                    <div class="dd-handle dd-nodrag row">
                        <span id="content_@grandChild.getCleanUri()"> @grandChild.getLabel() </span>
                    </div>

                </li>
            }


            <div class="text-left addNew" id="meta-@index"> 
                <span>
                    <button class="btn btn-icon w-xs plusBtn
                    btn-primary waves-effect waves-light toggleButton" data-toggle="modal" 
                    data-target="#custom-width-modal"> <i class="fa fa-plus" aria-hidden="true"></i> 
                    </button> 
                </span>
            </div>
            @(index = index + 1)

            </ol>
            </li>
    }

    </ol>
    </li>
}

</ol>

提前感谢您的回答。

您可以创建一个单独的模板,将 List[Skill] 作为参数并在提供的列表中呈现 SkillObjects 和这些 SkillObjects 的子项。然后在主页模板的第一个嵌套级别使用该模板。喜欢关注-

SkillTemplate.scala.html

@(skills: List[Skill])()

<ol class="dd-list">
    @for((child, indexChild) <- skills.zipWithIndex) {
        <li class="dd-item" data-id="@indexChild">
            <div class="dd-handle dd-nodrag row">
                <span id="content_@child.getCleanUri()"> @child.getLabel() </span>
            </div>
            @SkillTemplate(child.getChildrenList())
            <div class="text-left addNew" id="meta-@index"> 
              <span>
                  <button class="btn btn-icon w-xs plusBtn
                    btn-primary waves-effect waves-light toggleButton" data-toggle="modal" 
                    data-target="#custom-width-modal"> <i class="fa fa-plus" aria-hidden="true"></i> 
                  </button> 
              </span>
            </div>
            @(index = index + 1)
         </li>   
    }
</ol>

我还没来得及测试