我正在使用 Grails 的 sortablecolumn 功能对 gsp 中的表列进行动态排序。我有 3 个表,其中包含由 Controller 代码传递的 sql 行。
我的 Controller 代码是:
def ABC{
def A= AService.a()
def B=BService.b()
def C=CService.b()
render(view:list,model:[A:A,B:B,C:C])
}
AService、BService 和 CService 包含 sql 代码并返回行。
我的list.gsp代码是:
<body>
<div class="body">
<h1>ABC</h1>
<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>
<div class="list">
<table>
<thead>
<tr>
<g:sortableColumn property="name" title="NAME" />
<g:sortableColumn property="id" title="ID" />
<th> </th>
</tr>
</thead>
<tbody>
<g:each in="${A}" status="i" var="thisRecord">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
<td>${thisRecord.name}</td>
<td>${thisRecord.id}</td>
</tr>
</g:each>
</tbody>
</table>
</div>
</div>
</body>
</html>
还有另外两个表。每当我单击前端的列时,表格都无法正确排序。有不对的地方请指正。
最佳答案
排序通常发生在服务器端。因此,排序参数被传递到 Controller 中,查询函数采用排序参数对数据进行排序。 View 层不进行排序。您的 Controller 预计会返回排序后的数据。在您的示例中,您需要添加代码以在通过 render 方法返回数据之前对数据进行排序。
关于javascript - 如何对包含 Controller 传递的sql行的gsp中的表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28205173/