Grails: filter data in a Grails table dynamically

前端 未结 2 820
臣服心动
臣服心动 2020-12-29 16:58

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

相关标签:
2条回答
  • 2020-12-29 17:39

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-29 17:52

    Sounds like you want to show the search results in a 'list' view, so they show up in a table just like when unfiltered. You can just reuse that view and pass in the filtered results as the instance list.

    Do you have views for your Entry domain object? If not, generate some scaffolding views with grails generate-view Entry. Then in your controller, make your searchResults method look something like this:

    def searchResults = {
        def entryInstanceList = Entry.list() // replace with actual filtering logic
        render(view:'list', model: [entryInstanceList: entryInstanceList])
    }
    
    0 讨论(0)
提交回复
热议问题