0

I am new to playframework and to web programming, so the mixing of play variables, javascript and html is still a bit confusing for me.

In my case, I have a list of objects alarms: List[Alarm] coming from the controller of the view and I want to access one of the items of the list, but given that the index of the item come from a javascript function. Is that even possible?

I've tried the code below, but play's compiler do not understand the value of index in @{alarms(index).Patient.name}. Is there a way to do that? Or is it impossible because the list object is just used during the page creation and it cant be accessed later on by the javascript in a dynamic fashion.

@(alarms: List[Alarm], alarmForm: Form[Alarm]) 

@import helper._

@main("Alarm list") {

    <script id="showInfoScript" language="javascript" type="text/javascript">
   function showInfoFromAlarm(index) {
       $("#patientName").text(@{alarms(index).Patient.name});
    };
    </script>
Thomas
  • 2,751
  • 5
  • 31
  • 52

1 Answers1

0

It is not possible, as the value of index will be resolved on client side and the value of alarms is only available when generating the HTML response to be sent to the client.

What you should do is to send the values under alarms().Patient.name to the client too and use JavaScript from there.

Peter
  • 7,020
  • 4
  • 31
  • 51
  • Id in fact need to access the whole object (and its sub-objects) in the javascript function. It seems that the standard way of doing would be to send a json object, but when I tried simply to do a `@Json.toJson(alarm)`, I got an error that there were no serializer and that Id need to **"implement an implicit Writes or Format for this type"**. Do you by chance have any idea what is the simplest way to pass the object? – Thomas Mar 26 '14 at 13:07
  • Yes, that's normal, read more about this subject [here](http://stackoverflow.com/questions/10488950/play2-does-not-find-my-implicit-reads-or-format-for-json) – Peter Mar 26 '14 at 13:15
  • any idea how I can do that in java? I've just found solutions in scala for the serialization – Thomas Mar 26 '14 at 13:40
  • No, sorry, I have never used Java for these things. – Peter Mar 26 '14 at 13:42
  • How complex is your `Alarms` class? Make sure you are using `play.libs.Json` and not the Scala version if your project is Java based. – estmatic Mar 26 '14 at 15:09