【问题标题】:How to create a variable inside of a play twirl template? [duplicate]如何在播放旋转模板中创建变量? [复制]
【发布时间】:2016-11-17 15:46:23
【问题描述】:

我有一个模型传递给我的视图,看起来像:

IndexModel(productSales: Seq[ProductSale])

case class ProductSale(id: Int, name: String, soldAt: java.time.ZonedDateTime)

现在我想显示产品销售列表,但我为 Day 添加了标题,然后列出了当天的所有销售,如下所示:

2016 年 7 月 10 日

产品 1

产品 2

2016 年 7 月 11 日

产品 24 产品 2234

我能想到的唯一方法是要求我将当前日期存储在一个变量中,所以伪代码是:

@currentDay = -1
@for(ps <- model.ProductSales) {

  @if(currentDay != ps.SoldAt.getDayOfMonth) {
     <h1>@ps.soldAt</h1>
     @currentDay = ps.SoldAt.getDayOfMonth
  }

  <li>@ps.name - @ps.id </li>

}

我应该如何使用 twirl 来做到这一点?

【问题讨论】:

    标签: scala playframework twirl


    【解决方案1】:

    尝试(未测试):

    @for(currentDate <- model.productSales.map(_.soldAt).distinct) {
      <h1>@currentDate</h1>  
      @for(ps <- model.productSales.filter(_.soldAt == currentDate) {
        <li>@ps.name - @ps.id</li>
      }
    }
    

    如果你想声明一个变量,你需要使用@defining(){...} - 在这里阅读更多:https://www.playframework.com/documentation/2.5.x/JavaTemplates#Declaring-reusable-values

    【讨论】:

      猜你喜欢
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      相关资源
      最近更新 更多