当列是类的排序集时 Groovy 表排序

发布于 2024-12-11 20:11:26 字数 1902 浏览 0 评论 0原文

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

<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>

控制器中的逻辑:

order(params.sort, params.order)

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

I have a a view that lists several contractor objects. The last column is from addresses which is a sorted set in the contractor object. I am trying to sort those columns and I get an exception that the property addresses is not defined in class Contractors. Do I need to handle the sorted set differently in the 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>
                             | 
                            <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>

The logic in the controller:

order(params.sort, params.order)

I am very new to groovy/grails any help would be appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强者自强 2024-12-18 20:11:26

我不确定是否有任何现成可用的东西,但这是我解决类似问题的方法。基本上我必须拦截排序参数并进行自己的排序。:

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]
    }

I am not sure if there is anything availabel out of the box for this but here is how I solved my similar issue. Basically I had to interecept sort param and do my own sorting.:

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

--- where sortByState is this:

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]
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文