Supose我有一个视图,它接收到一系列类似的东西:
@(notices: Seq[Notice])
@for( not <- notices) {
<tr> not.title </tr>
}
....
我在控制器中有一个方法,它通过ajax调用并更改列表的值。我知道de方法可以返回一个Result并用新值重新呈现页面,比如:
public Result editNotice() {
/*change the list */
return ok(list.render(notices);
}
但我只想刷新列表所在的表。
如何在不重新加载整个页面的情况下刷新视图中的列表?
只需创建较小的视图来渲染表部分,并在editNotice
操作中使用它,然后在收到AJAX响应后,用JavaScript(可能是jQuery)替换现有的
为了确保您不需要在两个视图中创建相同的表标记,请记住,您可以使用另一个视图中的include模板,如文档中所示。
所以你的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构建新表。
最后我解决了这个问题,从控制器返回Result,但用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>