Play Framework,动态语句里面for循环

时间:2017-11-20 14:58:32

标签: java variables for-loop playframework

我正在尝试遍历一个对象列表并将它们显示在一个表中,但我想为每个对象做一些计算。我希望能够在for循环内的多个位置使用该变量。
我找不到在for循环中定义动态变量的正确语法。

我想要计算的值是进度条的百分比。

<table class="table table-striped table-bordered table-hover" id="dataTables-example">
            <thead>
                <tr>
                    <th scope="col">#</th>
                    <th scope="col">Job (id/name)</th>
                    <th scope="col">Place</th>
                    <th scope="col">Last Name</th>
                    <th scope="col">Progress</th>
                </tr>
            </thead>
            <tbody>
                @for((job,index) <- jobList.zipWithIndex) {
                    //
                    // this is where i'm stuck
                    //
                    @progressPercentage() = @{
                         job.getTotalTaskCount/job.getFinishedTaskCount.toDouble
                    }
                    <tr>
                        <th scope="row">@index</th>
                        <td>
                            <div>
                                <p><strong>@job.getJobName</strong></p>
                                <span><a href='@routes.JobDetailController.loadJob(job.getJobId)'>@job.getJobId</a></span>
                            </div>
                        </td>
                        <td>
                            <div>
                                <h5>@job.getZone</h5>
                                <p>@job.getContinent</p>
                            </div>

                        </td>
                        <td>
                            //
                            // Use of the variable
                            // 
                            <div class="progress progress-striped active">
                                <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="@progressPercentage()" aria-valuemin="0" aria-valuemax="100" style="width: 40%">
                                    <span class="sr-only">@progressPercentage()% Complete (success)</span>
                                </div>
                            </div>
                        </td>
                    </tr>
                }
            </tbody>
        </table>

我收到此编译错误,

  

未找到:value progressPercentage

是否有正确的语法或者这是不可能的?

1 个答案:

答案 0 :(得分:1)

有两种方法可以做你想做的事情(AFAIK):

  • 在for循环中声明带有defining的范围变量。

    @for((job,index) <- jobList.zipWithIndex) {
        @defining(job.getTotalTaskCount/job.getFinishedTaskCount.toDouble) { progressPercentage =>
            // Rest of your template structure here, use like @progressPercentage
        }
    }
    
  • 声明reusable code block(就像您正在使用的那个),但在for循环之外(将job作为参数传递)。

    @progressPercentage(job) = @{
        job.getTotalTaskCount/job.getFinishedTaskCount.toDouble
    }
    @for((job,index) <- jobList.zipWithIndex) {
        // Rest of your template structure here, use like @progressPercentage(job)
    }