Grails: filter data in a Grails table dynamically

本文介绍如何使用Grails框架实现数据过滤功能,特别是针对日期字段的筛选。作者通过示例展示了如何设置搜索条件并应用到数据集上,同时讨论了在实际应用中遇到的问题及解决思路。

I have a table, with a series of events, name of my class is Entry.

Here is a picture of my table

is in Spanish, but the basics are the same so it shouldn't be a problem. (the filter HTML code is not yet so its a mess but it works)

and here are the results that I get when I look for the entry.

Now my problem is that I need the results to be shown/filtered into the same table from pic1, so it would be basically like a table update applying the filters.

If you need more info here is the link to my old question. Thanks proflux!

I have a bunch of data and I need a data filter using Grails

Most of the search code is there,

Any help would be greatly appreciated! -Fernando

UPDATE:

I have a problem filtering the dates though... I have two dates... One is the date the event is going to take place, and the other one is lastUpdated which I think is a keyword for grails for the last time you modified the Event. Any help related to filtering dates would be greatly appreciated.

I need to show everything on the first table starting from today's date. And if I want to find something from the past I should be able to use the filter to find it.

Any ideas on how to do this?

UPDATE:

So here is my list.gsp

and here is my searchResults.gsp with the filters applied for the word "Ruta"

So basically everything looks nice and pretty but the date filters are not working.

Here is the code in the controller that is not filtering the dates

def searchResults = { 
    def entryCriteria = Entry.createCriteria() 
    def results = entryCriteria.list { 
        if(params?.proyectoRuta) { 
            ilike("proyectoRuta","%${params.proyectoRuta}%")
            } 

        }
        if(params?.day) {
            eq("fechaCambio", params.day)
        } 
        render(view:'searchResults', model:['results':results]) 
} 

 

is filtering the word but not the dates

proyectoRuta would be the title and fechaCambio would be the date shown in the first column. I have not tried to filter the lastUpdated date yet.

UPDATE:

Ok so here is my controller: Since is a lot of code I will only post the important defs

def search = { 
render(view:'search')

}

def searchResults = {
def entryCriteria = Entry.createCriteria()
def results = entryCriteria.list {
if(params?.fechaCambioD && params?.fechaCambioH) {
between("fechaCambio", params.fechaCambioD, params.fechaCambioH)
}

if(params?.lastUpdatedD && params?.lastUpdatedH) {
between("lastUpdated", params.lastUpdatedD, params.lastUpdatedH)
}

if(params?.proyectoRutaN) {
ilike("proyectoRuta","%${params.proyectoRutaN}%")
}
}
render(view:'searchResults', model:['results':results, 'proyectoRutaN':params?.proyectoRutaN, 'fechaCambioD':params?.fechaCambioD, 'fechaCambioH':params?.fechaCambioH, 'lastUpdatedD':params?.lastUpdatedD, 'lastUpdatedH':params?.lastUpdatedH])
}

And here is the searchResults.gsp

<tbody>
                <g:each in="${results}">

                        <td><g:formatDate format="dd-MMMM-yyyy" date="${it.fechaCambio}" /></td>
                        <td><b>${it.proyectoRuta}</b></td>
                        <td>${it.summary}</td>
                        <td><g:formatDate format="dd-MMM-yyyy HH:mm z" date="${it.lastUpdated}" /></td>
                        <td>
                        <g:form>
                            <g:hiddenField name="id" value="${it?.id}" />
                            <span class="simple"><g:actionSubmit class="editar" action="edit" value="${message(code: 'default.button.editar.label', default: '   ')}" /></span>
                            <span class="simple"><g:actionSubmit class="eliminar" action="delete" value="${message(code: 'default.button.eliminar.label', default: '   ')}" οnclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Esta seguro que desea Eliminar?')}');" /></span>
                        </g:form>
                        </td>
                    </tr>
                </g:each>
            </tbody>

 Try this using the same gsp for both the results and the search criteria...

<html>
<body>
    <h1>Search Criteria</h1>

    <g:form controller="entry" action="searchResults">
        Title: <g:textField name="title" value="${title}" />
        Date: <g:datePicker name="startTime" value="${startTime}" precision="day" />
        <g:submitButton name="submit" value="Search" />
    </g:form>
    <g:if test="${results}">
        <h1>Search Results</h1>
        <table>
            <tr>
                <th>Title</th>
                <th>Start Date</th>
            </tr>
            <g:each in="${results}">
                <tr>
                    <td>${it.title}</td>
                    <td>${it.startTime}</td>
                </tr>
            </g:each>
        </table>
    </g:if>
</body>
</html>

 And then for your controller closures:

def search = {
        render(view:'search')
    }

    def searchResults = {
        def entryCriteria = Entry.createCriteria()
        def results = entryCriteria.list {
            if(params?.title) {
                ilike("title", "%${params.title}%")
            }
        }
        render(view:'search', model:['results':results, 
                                     'title':params?.title, 
                                     'startTime':params?.startTime])
    }

 

This is from a quick demo i wrote really quick, but from the info I gave you on the other question and the Grails Reference Documentation on using criteria queries, you should be able to figure it out. Let me know if you run into any problems.

Update:

Try something like this for your between criteria:

def entryCriteria = Entry.createCriteria()
def results = entryCriteria.list {
    if(params?.title) {
        ilike("title", "%${params.title}%")
    }
    if(params?.day) {
        between("day", params.day, params.day+1)
    }
}

 



转载于:https://www.cnblogs.com/zengsong-restService/archive/2012/03/14/2395490.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值