scala - Get a string from an object in the template -


imagine have case class so:

   case class team(_id: option[bsonobjectid], name: string = "", city: string = "", country: string = "") 

and passing template (in play 2.5/scala/reactivemongo 0.11.14) this:

   @(teams : seq[models.team])     ...        @for(team <- teams){             <tr>                 <td>@team._id</td>                 <td>@team.name</td>                 <td>@team.city</td>                 <td>@team.country</td>             </tr>       }     ... 

i need @team._id (which type option[bsonobjectid]) string - bsonobjectid characters string. have been trying declare reusable values documented here can't seem right. ordinarily use flatmap isn't working expected within template. help!


following on seems should use method within case case class @marcospereira suggested. little stuck (unsurprisingly!) on syntax in regex expression. trying make string - bsonobjectid("59654f33b17946eac2323b3e") 59654f33b17946eac2323b3e. have:

def idasstring = _id.flatmap(bson => """\".*?(")""".r.findfirstin(bson.tostring)).getorelse("") 

but returns quotation marks, e.g. "59654f33b17946eac2323b3e". mentioned don't want - can can't quite syntax right.

you can like:

case class team(_id: option[bsonobjectid] = none, name: string = "", city: string = "", country: string = "") {      def idasstring(): string = _id.getorelse("")  } 

and then:

<td>@team.idasstring()</td> 

notice i've add none default _id. can have "view helper" this:

package helpers  object optionviewhelpers {   def optionvaluetostring(o: option[_]): string = o match {     case some(v) => string.valueof(v)     case _ => ""   } } 

and view:

@import helpers._  <td>@{optionviewhelpers.optionvaluetostring(team._id)}</td> 

the advantage here can reuse in other scenarios.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -