文辉居士

Grails: filter data in a Grails table dynamically

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: '   ')}" onclick="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)
    }
}

 



posted on 2012-03-14 10:29  restService  阅读(463)  评论(0编辑  收藏  举报

导航


我是有底线的赠送场