我正在尝试循环对象列表并将它们显示在表格中,但我想对每个对象进行一些计算。我希望能够在 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>
我收到此编译错误,
not found: value progressPercentage
是否有正确的语法或者这根本不可能?
最佳答案
有两种方法可以完成您想要做的事情(据我所知):
使用
defining
声明作用域变量在 for 循环内。@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) }
关于java - Play框架,for循环内的动态语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47394910/