Grails:如何在页面加载时使页面加载过滤器? [英] Grails: How do I make my page load a filter when the page loads?

查看:226
本文介绍了Grails:如何在页面加载时使页面加载过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在index.gsp我有这个重定向到list.gsp,所以我想像的应该是这样的:

  $ {response.sendRedirect(entry / list)} 

textField和两个带有下拉框(DD-MMM-YYYY)
的日期选择器,它们应该默认从今天的日期过滤到无穷远。所以它应该只显示尚未发生的事件,但是旧的应该仍然在数据库中。



这里是我最后3个问题的背景资料链接。一个href =https://stackoverflow.com/questions/3096572/i-have-a-bunch-of-data-and-i-need-a-data-filter-using-grails>我有一堆数据,我需要一个使用Grails的数据过滤器

/ Grails-edit-and-delete-links-not-working> Grails:编辑和删除链接不工作



认为第二个是我的代码中的大部分。



任何帮助将不胜感激和良好的评价。谢谢! :)
再次特别感谢proflux的所有帮助!
$ b

更新



这里是list.gsp中的

 < g:each in =$ {entryInstanceList} status =ivar =entryInstance> 
< tr class =$ {(i%2)== 0?odd:even}>
< td>< g:formatDate format =dd-MMMM-yyyydate =$ {entryInstance.fechaCambio}/>< / td>
< td>< b> $ {fieldValue(bean:entryInstance,field:'proyectoRuta')}< / b>< / td>
< td> $ {fieldValue(bean:entryInstance,field:'summary')}< / td>
< td>< g:formatDate format =dd-MMM-yyyy HH:mm zdate =$ {entryInstance.lastUpdated}/>< / td>
< td>
< g:actionSubmit class =editaraction =editvalue =$ {message(code:'default.button.editar.label',default: '& nbsp;& nbsp;& nbsp;')}/>< / span>
< span class =simple>< g:actionSubmit class =eliminaraction =deletevalue =$ {message(code:'default.button.eliminar.label' '& nbsp;& nbsp;& nbsp;')}onclick =return confirm('$ {message(code:'default.button.delete.confirm.message',default:'Esta seguro que desea Eliminar ? ')}'); />< /跨度>
< / g:表格>
< / td>
< / tr>
< / g:每个>

更新 $ b p>这里是searchResults代码,

  def searchResults = {
def entryCriteria = Entry.createCriteria()

def results = entryCriteria.list {
and {if(params?.fechaCambioD& amp; params?.fechaCambioH){
between(fechaCambio,params.fechaCambioD,params (lastUpdated,params.lastUpdatedD,params.lastUpdatedH){
}

if(params?.lastUpdatedD&


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

因为我已经在使用结果,并且是一个列表,我应该怎么做,给它另一个名字,并在结果之后把它放到模型中?如:

  render(view:'searchResults',model:['results':results,'otherResults':otherResults' proyectoRutaN':params?.proyectoRutaN,'fechaCambioD':params?.fechaCambioD,'fechaCambioH':params?.fechaCambioH,'lastUpdatedD':'','lastUpdatedH':params?.lastUpdatedH])

或者我应该在结果中定义它?



UPDATE

 < table>< tbody class =yui-skin-sam> 
< tr class =prop>
< td valign =topclass =name>< b> Proyecto / Ruta:< / b> & nbsp;& nbsp;< g:textField name =proyectoRutaNvalue =$ {proyectoRutaN}/>< / td>
< td>< / td>
< td valign =topclass =name>< b>实施细则:< / b>< / td>
< td>< / td>
< td valign =topclass =name>< b>最后一次修改:< / b>< / td>
< / tr>
< tr class =prop>
< td>< / td>
< td valign =top> Desde:< / td>
< td valign =top>< gui:datePicker name =fechaCambioDvalue =$ {params?.fechaCambioD}formatString =dd / MMM / yyyy/>< TD>
< td valign =top> Desde:< / td>
< td valign =top>< gui:datePicker name =lastUpdatedDvalue =$ {params?.lastUpdatedD}default =none/>< / td>
< / tr>
< tr class =prop>
< td>< / td>
< td valign =top> Hasta:< / td>
< td valign =top>< gui:datePicker name =fechaCambioHvalue =$ {params?.fechaCambioH}formatString =dd / MMM / yyyy/>< TD>
< td valign =top> Hasta:< / td>
< td valign =top>< gui:datePicker name =lastUpdatedHvalue =$ {params?.lastUpdatedH}default =none/>< / td>
< / tr>



由于某些原因,过滤器会显示我列表中的所有内容(不会过滤),过滤器中的textFields不会保存应用过滤器的日期。



任何关于如何解决这个问题的想法?

解决方案

不是在GSP上进行重定向,而是更新索引控制器方法来做这样的事情:

pre $ def $ index $ $ b $ redirect(action:'list')
}

这样,您的控制器逻辑全部在控制器中,而不是在GSP和控制器中。

你的第二个要求是有点棘手,但不是太糟糕。基本上,你需要更新你的标准逻辑。我假设你只关心两种情况:


  1. 你同时拥有fechaCambioD和fechaCambioH

  2. 你有fechaCambioD和没有fechaCambioH(你的默认 - 从今天直到时间结束)

你不关心寻找所有从开始到fechaCambioH的条目如果是这样的话,改变你的控制器逻辑为:

pre> def fechaCambioD = params?.fechaCambioD?:new Date()

def results = entryCriteria.list {
if(params?.fechaCambioD& amp; amp; ;& params?.fechaCambioH){
(fechaCambio,params.fechaCambioD,params.fechaCambioH)
}
else {
gte(fechaCambio,fechaComabioD) (lastUpdated,params.lastUpdatedD,params.lastUpdatedH)
($ params.lastUpdatedD) b}

if(params?.proyectoRutaN) {
ilike(proyectoRuta,%$ {params.proyectoRutaN}%)
}
}

所以我们基本上是:


  1. 如果你没有得到fechaCambioD参数,如果我们没有fechaCambioD和fechaCambioH,我们将查找所有fechaCambio大于当前时间的条目。

我相信应该可以工作,但是如果不让我知道的话...

更新:



好的,我的错误是,没有gte comaparator,只是ge大于或等于gt,比。由于您的搜索条件比较简单,您可以将该方法更改为:

  def list = {
(Calendar.HOUR,0)
today.set(Calendar.MINUTE,0)
today.set(Calendar.SECOND,0)
today.set(Calendar.MILLISECOND,0)
def results = Entry.findAllByFechaCambioGreaterThanEquals(today.getTime())
render(view:'list',model:['entryInstanceList':结果])
}

如果这行不通,请告诉我...


On index.gsp I have this to redirect it to list.gsp so I'm imagining is should be something like this:

${response.sendRedirect("entry/list")}

My filter is just one textField and two datePickers with drop down boxes (DD-MMM-YYYY) and they should be by default filtered from today's date to infinity. So it should show me only the events that have not happened yet, but the old ones should still be in the database.

Here are the links to my last 3 questions for background information

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

Grails: filter data in a Grails table dynamically

Grails: Edit and Delete links not working

I think the second one is the one with most of my code.

Any help would be greatly appreciated and well rated. Thanks! :) And a again special thanks to proflux for all the help!

UPDATE

Here is the in the list.gsp

<g:each in="${entryInstanceList}" status="i" var="entryInstance">
                    <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
                        <td><g:formatDate format="dd-MMMM-yyyy" date="${entryInstance.fechaCambio}" /></td>
                        <td><b>${fieldValue(bean: entryInstance, field: 'proyectoRuta')}</b></td>
                        <td>${fieldValue(bean: entryInstance, field: 'summary')}</td>
                        <td><g:formatDate format="dd-MMM-yyyy HH:mm z" date="${entryInstance.lastUpdated}" /></td>
                        <td>
                        <g:form>
                            <g:hiddenField name="id" value="${entryInstance?.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>

UPDATE

Here is the searchResults code,

def searchResults = { 
    def entryCriteria = Entry.createCriteria()

    def results = entryCriteria.list {
        and{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':'', 'lastUpdatedH':params?.lastUpdatedH]) 
}

And since I am already using results and is a list, what should I do, give it another name and at the end put it to model after results?? like:

render(view:'searchResults', model:['results':results, 'otherResults':otherResults, 'proyectoRutaN':params?.proyectoRutaN, 'fechaCambioD':params?.fechaCambioD, 'fechaCambioH':params?.fechaCambioH, 'lastUpdatedD':'', 'lastUpdatedH':params?.lastUpdatedH])

Or should I just define it inside results??

UPDATE

<table><tbody class="yui-skin-sam">
<tr class="prop">
    <td valign="top" class="name"><b>Proyecto/Ruta: </b> &nbsp;&nbsp;&nbsp;<g:textField name="proyectoRutaN" value="${proyectoRutaN}" /></td>
    <td></td>
    <td valign="top" class="name"><b>Fecha de Implementación: </b></td>
    <td></td>
    <td valign="top" class="name"><b>Fecha de Última Modificación: </b></td>
</tr>
<tr class="prop">
    <td></td>
    <td valign="top">Desde:</td>
    <td valign="top"><gui:datePicker name="fechaCambioD" value="${params?.fechaCambioD}"  formatString="dd/MMM/yyyy"/></td>
    <td valign="top">Desde:</td>
    <td valign="top"><gui:datePicker name="lastUpdatedD" value="${params?.lastUpdatedD}" default="none"/></td>
</tr>
<tr class="prop">
    <td></td>
    <td valign="top">Hasta:</td>
    <td valign="top"><gui:datePicker name="fechaCambioH" value="${params?.fechaCambioH}"  formatString="dd/MMM/yyyy"/></td>
    <td valign="top">Hasta:</td>
    <td valign="top"><gui:datePicker name="lastUpdatedH" value="${params?.lastUpdatedH}" default="none"/></td>
</tr>

For some reason when I apply the filters then it shows me everything on the list (it does not filter) and the textFields in the filter do not save the date from when the filter was applied.

Any ideas on how I can fix this?

解决方案

Instead of doing the redirect on the GSP, it would probably be better to update your index controller method to do something like:

def index {
  redirect(action:'list')
}

This way your controller logic is all in the controller and not in both the GSP and controller.

Your second requirement is a little trickier, but not too bad. Basically, you need up update your criteria logic. I'll assume that you only care about two cases:

  1. you have both fechaCambioD and fechaCambioH
  2. you have fechaCambioD and no fechaCambioH (your default - from today until the end of time)

And you don't care about finding all the entries from the beginning of time up until fechaCambioH

If that's the case, change your controller logic to:

def fechaCambioD = params?.fechaCambioD ?: new Date()

def results = entryCriteria.list {
    if(params?.fechaCambioD && params?.fechaCambioH) { 
        between("fechaCambio", params.fechaCambioD, params.fechaCambioH) 
    } 
    else {
      gte("fechaCambio", fechaComabioD)
    }

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

    if(params?.proyectoRutaN) { 
        ilike("proyectoRuta","%${params.proyectoRutaN}%")
        }            
    }

So we essentially:

  1. If you didn't get a fechaCambioD parameter, we're going to default fechaCambioD to the current time.
  2. If we don't have both fechaCambioD and fechaCambioH we're going to find all entries that have a fechaCambio greater than the current time.

I believe that should work, but if not let me know...

UPDATE:

Okay, my mistake, there is no gte comaparator, it's just ge for greater than or equal and gt for greater than. Since your search criteria is simpler for list, you can probably just change the method to:

def list = {
   def today = Calendar.getInstance()
   today.set(Calendar.HOUR, 0)
   today.set(Calendar.MINUTE, 0)
   today.set(Calendar.SECOND, 0)
   today.set(Calendar.MILLISECOND, 0)
   def results = Entry.findAllByFechaCambioGreaterThanEquals(today.getTime()) 
   render(view:'list', model:['entryInstanceList':results])
}

If this doesn't work let me know...

这篇关于Grails:如何在页面加载时使页面加载过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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