Playframework:如何在不重新加载整个页面的情况下更改旋转参数的值?

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

假设我有一个视图接收到类似这样的列表:

@(notices: Seq[Notice])

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

我在控制器中有一个方法,它由 ajax 调用并更改列表的值。我知道 de 方法可以 return 一个结果并使用新值重新呈现页面,例如:

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

但我只想刷新列表所在的 table。

如何在不重新加载整个页面的情况下刷新视图中的列表?

只需创建较小的视图以仅呈现 table 部分并在 editNotice 操作中使用它,然后在收到 AJAX 响应后将现有视图替换为 JavaScript(大概是jQuery)

为确保您不需要在两个视图中创建相同的 table 标记,请记住您可以使用另一个视图中的 include 模板一个,如 the docs 中所示。

因此您的 AJAX 操作将如下所示:

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

和你的 list.scala.html:

@(notices: Seq[Notice])

@table(notices)
....

和你的 table.scala.html:

@(notices: Seq[Notice])

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

其他解决方案

正在从 editAction 返回 JsonNode - 对象数组 - 并使用 JS 构建新的 table。

最后我解决了这个从控制器返回结果的问题,但是用 javascript 和 div 替换了 html 中的内容。

控制器:

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>