gpt4 book ai didi

grails - 列是类的排序集时的Groovy表排序

转载 作者:行者123 更新时间:2023-12-02 14:46:48 26 4
gpt4 key购买 nike

我有一个列出几个承包商对象的 View 。最后一列来自地址,该地址是承包商对象中的排序集。我试图对这些列进行排序,但出现异常,即在类Contractors中未定义属性地址。我需要在 Controller 中以不同方式处理排序集吗?

<table width="100%">
<thead>
<tr>
<th></th>
<g:sortableColumn property="status" title="${message(code: 'contractor.status.label', default: 'Status')}" params="[statuses:statuses,name:name,nameOperator:nameOperator ?: 'CONTAINS']"/>
<g:sortableColumn property="contractorName" title="${message(code: 'contractor.contractorName.label', default: 'Name')}" params="[statuses:statuses,name:name,nameOperator:nameOperator ?: 'CONTAINS']"/>

<g:sortableColumn property="addresses.address" title='Address' params="[statuses:statuses,name:name,nameOperator:nameOperator ?: 'CONTAINS']"/>

</tr>
</thead>
<tbody>
<g:each in="${contractorList}" status="i" var="contractor">
<tr class="${(i % 2) == 0 ? 'odd' : 'even'}">

<td>
<g:link action="show" id="${contractor.id}">show</g:link>
&nbsp;|&nbsp;
<g:link action="edit" id="${contractor.id}">edit</g:link>
</td>
<td>${contractor.status}</td>
<td>${contractor.contractorName}</td>
<td>${contractor?.addresses?.address}</td>

</tr>
</g:each>
</tbody>
</table>

Controller 中的逻辑:
order(params.sort, params.order)

我对groovy / grails非常陌生,将不胜感激,谢谢!

最佳答案

我不确定是否有任何可用的可用标签,但这是我解决类似问题的方法。基本上,我必须接受排序参数并自己进行排序。

if (params.sort && params.sort == "state") {
tasks = Task.activeOnly.open.unassigned.list() //<--my query
tasks = sortByState(tasks,params) //<--custom sort
}

---这里的sortByState是:
private List sortByState(tasks, params){
tasks.sort{Task.STATES.indexOf(it.state)} //<-- this is my custom sorter
if (params.order == "DESC" ) {
tasks = tasks.reverse()
}
def begin = params.int('offset') //<-- next two lines are for paging offsets
def end = Math.min(begin+params.int('max'),tasks.size()-1)
return tasks[begin..end]
}

关于grails - 列是类的排序集时的Groovy表排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7909509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com