【问题标题】:map to form select Scala Play2 using reactivemongo使用 reactivemongo 映射以形成选择 Scala Play2
【发布时间】:2013-03-14 20:36:51
【问题描述】:

我有一个 Play2 模板:

files: Option[List[(String, reactivemongo.api.gridfs.ReadFile[reactivemongo.bson.BSONValue])]]

我想以下拉形式选择显示文件,但前提是文件存在!

我该怎么做?

【问题讨论】:

  • 优先在控制器中转换这个非常复杂的对象(关注点分离)以满足您的需求
  • 并去掉List周围的OptionOption本质上是一个包含0或1个元素的List。

标签: scala playframework-2.0


【解决方案1】:

正如 Julien Lafont 所说,您应该考虑转换此列表,然后再将其提供给您的模板。例如,如果您只想要读取文件的列表(您可以在其上调用 filenameid):

val fileList = files.toList.flatten.map(_._2) // fileList is a List[ReadFile[BSONValue]]]

如果你只想获取文件名和它们的 id(假设它们的 id 是BSONObjectIDs),你可以这样写:

val fileList = files.toList.flatten.map { file =>
  val id = file._2.id match {
    case oid: BSONObjectID => oid.stringify
    case id => id.toString
  }
  id -> file._2.filename
}
// fileList is a List[(String, String)] where the first element of the tuple is a string version of the id and the second is the name of the file.

【讨论】:

  • filesWithId.toList.flatten.map(._1) 不起作用说无法解析符号 ._1 但 val newid = filesWithId.toList.map(._1) 确实
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
  • 2015-01-06
  • 2016-02-24
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
  • 2014-05-04
相关资源
最近更新 更多