Grails:动态过滤 Grails 表中的数据 [英] Grails: filter data in a Grails table dynamically

查看:18
本文介绍了Grails:动态过滤 Grails 表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,上面有一系列事件,我的班级名称是 Entry.

这是我的桌子的照片

是西班牙语,但基本内容相同,所以应该没有问题.(过滤器 HTML 代码还没有,所以它是一团糟,但它可以工作)

这是我查找条目时得到的结果.

现在我的问题是我需要将结果显示/过滤到来自 pic1 的同一个表中,所以它基本上就像应用过滤器的表更新.

如果您需要更多信息,这里是我的旧问题的链接.感谢大量!

这是我的 searchResults.gsp,其中包含应用于Ruta"一词的过滤器

所以基本上一切看起来都很漂亮,但日期过滤器不起作用.

这是控制器中不过滤日期的代码

def searchResults = {def entryCriteria = Entry.createCriteria()def 结果 = entryCriteria.list {如果(参数?.proyectoRuta){ilike("proyectoRuta","%${params.proyectoRuta}%")}}如果(参数?.天){eq(fechaCambio",params.day)}渲染(视图:'搜索结果',模型:['结果':结果])}

过滤单词但不过滤日期

proyectoRuta 将是标题,fechaCambio 将是第一列中显示的日期.我还没有尝试过滤 lastUpdated 日期.

更新:

好的,这是我的控制器:由于代码很多,我只会发布重要的定义

def search = {渲染(视图:'搜索')}定义搜索结果 = {def entryCriteria = Entry.createCriteria()def 结果 = entryCriteria.list {if(params?.fechaCambioD && params?.fechaCambioH) {之间(fechaCambio",params.fechaCambioD,params.fechaCambioH)}if(params?.lastUpdatedD && params?.lastUpdatedH) {之间(lastUpdated",params.lastUpdatedD,params.lastUpdatedH)}如果(参数?.proyectoRutaN){ilike("proyectoRuta","%${params.proyectoRutaN}%")}}渲染(视图:'searchResults',模型:['结果':结果,'proyectoRutaN':params?.proyectoRutaN,'fechaCambioD':params?.fechaCambioD,'fechaCambioH':params?.fechaCambioH,'lastUpdatedD':params?.lastUpdatedD, 'lastUpdatedH':params?.lastUpdatedH])}}

这里是 searchResults.gsp

<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:表格><g:hiddenField name="id" value="${it?.id}"/><span class="simple"><g:actionSubmit class="editar" action="edit" value="${message(code: 'default.button.editar.label', default: '&nbsp;&nbsp;&nbsp;')}"/></span><span class="simple"><g:actionSubmit class="eliminar" action="delete" value="${message(code: 'default.button.eliminar.label', default: '&nbsp;&nbsp;&nbsp;')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Esta seguro que desea Eliminar?')}');"/></span></g:form></td></tr></g:每个></tbody>

解决方案

尝试对结果和搜索条件使用相同的 gsp...

<身体><h1>搜索条件</h1><g:form controller="entry" action="searchResults">标题:<g:textField name="title" value="${title}"/>日期:<g:datePicker name="startTime" value="${startTime}" precision="day"/><g:submitButton name="submit" value="搜索"/></g:form><g:if test="${results}"><h1>搜索结果</h1><表格><tr><th>Title</th><th>开始日期</th></tr><g:each in="${results}"><tr><td>${it.title}</td><td>${it.startTime}</td></tr></g:每个></g:if>

然后是你的控制器闭包:

def search = {渲染(视图:'搜索')}定义搜索结果 = {def entryCriteria = Entry.createCriteria()def 结果 = entryCriteria.list {如果(参数?.标题){ilike("title", "%${params.title}%")}}渲染(视图:'搜索',模型:['结果':结果,'title':params?.title,'startTime':params?.startTime])}

这是来自我写得非常快的快速演示,但是根据我在另一个问题上给您的信息和 Grails 参考文档 关于使用标准查询,您应该能够弄清楚.如果您遇到任何问题,请告诉我.

更新:

为您的标准之间尝试这样的事情:

def entryCriteria = Entry.createCriteria()def 结果 = entryCriteria.list {如果(参数?.标题){ilike("title", "%${params.title}%")}如果(参数?.天){之间(天",params.day,params.day+1)}}

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!

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: '&nbsp;&nbsp;&nbsp;')}" /></span>
                            <span class="simple"><g:actionSubmit class="eliminar" action="delete" value="${message(code: 'default.button.eliminar.label', default: '&nbsp;&nbsp;&nbsp;')}" 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)
    }
}

这篇关于Grails:动态过滤 Grails 表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆