【问题标题】:Play Framework, Dynamic statement inside for loop播放框架,for循环内的动态语句
【发布时间】:2017-11-20 14:58:32
【问题描述】:

我正在尝试遍历对象列表并将它们显示在表格中,但我想对每个对象进行一些计算。我希望能够在 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>

我得到这个编译错误,

未找到:值 progressPercentage

有正确的语法还是不可能?

【问题讨论】:

    标签: java variables for-loop playframework


    【解决方案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)
      }
      

    【讨论】:

    • 完美答案!我知道这很简单,但这是我应该自己找到的。 :p 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 2020-06-11
    相关资源
    最近更新 更多