kobey
kobey

Reputation: 174

Play Framework, Dynamic statement inside for loop

I'm trying to loop over a list of objects and display them in a table but i want to do some calculations for each object. I would like to be able to use that variable on multiple locations inside the for loop.
I can't find the right syntax for defining a dynamic variable inside of a for loop.

The value that i want to calculate is the percentage for the progress bar.

<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>

I get this compilation error,

not found: value progressPercentage

Is there a right syntax or is this just not possible?

Upvotes: 0

Views: 51

Answers (1)

gpgekko
gpgekko

Reputation: 3616

There are two ways to do what you're trying to do (AFAIK):

  • Declaring a scoped variable with defining inside the for loop.

    @for((job,index) <- jobList.zipWithIndex) {
        @defining(job.getTotalTaskCount/job.getFinishedTaskCount.toDouble) { progressPercentage =>
            // Rest of your template structure here, use like @progressPercentage
        }
    }
    
  • Declaring a reusable code block (like the one you're using), but outside the for loop (passing the job in as an argument).

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

Upvotes: 1

Related Questions