【问题标题】:zip style @repeat over nested formzip 样式 @repeat 覆盖嵌套形式
【发布时间】:2012-10-14 15:24:05
【问题描述】:

@repeat 非常有用;但是,我遇到了嵌套表单的障碍。

我需要为比赛日程制作一个表格,其中包含 2 个属性、日程数据(比赛日期、时间、地点、对手)和提交团队备注(例如“由于 1 月 7 日的冬季风暴,比赛有已移至 1 月 9 日在...夏威夷 ;-)")

表单映射基于:

case class Schedule(
  composite: Seq[Composite], 
  note: Seq[ScheduleNote]
)

然后在我拥有的模板中显示表单:

@repeat(_form("composite"), min=numGames) { f=>
  @inputDate(f("gameDate"), 'id-> "gameDate", '_label-> "Game Date")
  ....
}
@repeat(_form("note"), min=numGames) { f=>
  @inputDate(f("gameNote"), '_label-> "Game Notes")
  ....
}

当然游戏笔记需要和游戏数据配对,上面不会发生这种情况,因为看起来我需要@repeat单独组合游戏数据和笔记。

这将是非常非常好的: @repeat(_form("composite").zip(_form("note")), min=numGames) { case(fc,fn)=>

在嵌套的表单元素之上。

无论如何我可以解决这个问题吗?看起来 at the source 似乎不是,但也许通过我的库的皮条客这是可能的(或者,因为我是针对 2.1 构建的,所以在框架支持似乎是一个限制之前修改一些东西)

【问题讨论】:

    标签: forms scala nested playframework-2.0 repeat


    【解决方案1】:

    编辑
    实际上,我最初的尝试使生成的字段数量增加了一倍;这个生成正确数量的字段:

    object repeat2 {
      import play.api.data.Field, play.api.templates.Html
      def apply(field: (Field,Field), min: Int = 1)(f: (Field,Field) => Html) = {
        field match{ case(a,b)=>
          (0 until math.max(
            if (a.indexes.isEmpty) 0 else a.indexes.max + 1, min)
          ).map(i => f.apply(a("["+i+"]"), b("["+i+"]")) )
        }
      }
    }
    

    如果编辑表单正确映射表单数据值,仍待定......

    原创
    实验,编译:

    // in a form utility object
    object repeat2 {
      import play.api.data.Field, play.api.templates.Html
      def apply(field: (Field,Field), min: Int = 1)(f: Field => Html) = {
        field match{ case(a,b)=>
          (0 until math.max(
            if (a.indexes.isEmpty) 0 else a.indexes.max + 1, min)
          ).map(i => f(a("["+i+"]")) + f(b("["+i+"]")) )
        }
      }
    }
    
    // then, importing above in a template
    @repeat2( (_form("composite"), _form("note")), min=5) { f=>
      @inputDate(f("gameDate"), 'id-> "gameDate", '_label-> "Game Date")
      ...
      @inputDate(f("gameNote"), '_label-> "Game Notes")
    }
    

    并根据需要一起生成游戏数据和笔记。

    至于它是否适用于表单编辑,待定 ;-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多