简体   繁体   中英

Playframework: how change the value of a twirl parameters without reload the entire page?

Supose I have a view that receive a list of things like that:

@(notices: Seq[Notice])

@for( not <- notices) {
   <tr> not.title </tr>
}
....

and I have a method in a controller that its call by ajax and changes the values of the list. I know that de method can return a Result and re-render the page with the new values, like:

public Result editNotice() {
   /*change the list */
   return ok(list.render(notices);
}

But I want to refresh only the table where the list is.

How can refresh the list in the view without reload the entire page?

Just create smaller view for rendering just the table part and use it in the editNotice action, then after receiving the AJAX response replace the existing one with JavaScript (probably jQuery)

To make sure, that you don't need to create the same table markup in two views keep in mind that you can use include template from the other one, like showed in the docs .

so your AJAX action will look like:

public Result editNotice() {
   return ok(table.render(notices);
}

and your list.scala.html :

@(notices: Seq[Notice])

@table(notices)
....

and your table.scala.html :

@(notices: Seq[Notice])

<table>
  @for(not <- notices) {
    <tr><td>@not.title</td></tr>
  }
</table>

Other solution

Is returning the JsonNode - array of objects - from the editAction and building new table with JS.

Finally I resolved this returning the Result from the controller, but replacing the content in the html with javascript and a div.

Controller:

public Result editNotice() {
   return ok(table.render(notices);
}

Html: list.scala.html

@()

<div id="table">
</div>
<li>
    <a class="ajax-call" href="#table" onclick="$('#table').load('/editNotice';"/>
</li>

Html: table.scala.html

@(notices: Seq[Notice])
  <table>
     @for(not <- notices) {
       <tr><td>@not.title</td></tr>
     }
  </table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM