开发者

Grails: How do I make my page load a filter when the page loads?

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/R开发者_如何学编程uta: </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?


For the fechaCambio in the criteria, what you can do is something like this:

def searchResults = { 
    def fromCal
    if(params?.fechaCambioD) {
      fromCal = Calendar.getInstance()
      fromCal.setTime(param?.fechaCambioD)
      fromCal.set(Calendar.HOUR_OF_DAY,0)
      fromCal.set(Calendar.MINUTE,0)
      fromCal.set(Calendar.SECOND,0)
      fromCal.set(Calendar.MILLISECOND,0)
    }
    def toCal
    if(params?.fechaCambioH) {

      toCal = Calendar.getInstance()
      toCal.setTime(param?.fechaCambioH)
      toCal.set(Calendar.HOUR_OF_DAY,23)
      toCal.set(Calendar.MINUTE,59)
      toCal.set(Calendar.SECOND,59)
      toCal.set(Calendar.MILLISECOND,999)
    }
    def entryCriteria = Entry.createCriteria()

    def results = entryCriteria.list {
        and{if(params?.fechaCambioD && params?.fechaCambioH) { 
            between("fechaCambio", fromCal.getTime(), toCal.getTime()) 
            } 

        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]) 
}

Again, this is the quick and dirty approach; not very elegant but if I understand your problem it should do what you want.


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜