使用reactivemongo选择Scala Play2



我有一个Play2模板:

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

我想显示的文件在下拉表单选择,但只有当文件在那里!

我该怎么做呢?

正如Julien Lafont所说,在将此列表提供给模板之前,您应该考虑对其进行转换。例如,如果您只想要一个读取文件列表(可以调用filenameid):

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

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

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.

最新更新