开发者

grails - how to make filter recognize values from datePicker using Grails Calendar Plugin?

I was using regular datePicker from grails but I decided it was easier to use a textField with a calendar next to it and I was recommended to use Grails-UI.

My problem is that now, when I click to apply the filters, they do not work. Somehow they are not being recognized.

Any help would be greatly appreciated.

Here is the code for the controller:

def searchResults = {
  def fromCal
  if(params?.lastUpdatedD) {
fromCal = Calendar.getInstance()
fromCal.setTime(params?.lastUpdatedD)
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?.lastUpdatedH) {
toCal = Calendar.getInstance()
toCal.setTime(params?.lastUpdatedH)
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", params.fechaCambioD, params.fechaCambioH) 
} 

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

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 here is a piece of code in the search.gsp where the gui:datePicker is declared

<gui:datePicker name="fechaCambioH" value="${params?.fechaCambioH}" formatString="dd/MMM/yyyy"/>

If I change them to <<gui:datePicker name="fechaCambioH" value="${params?.fechaCambioH}" precision="day" default="none" noSelection="['':'']" years="${2010..2015}"/>/> it works perfectly fine but it gives me 3 drop down boxes for day month and year, and the idea is to use just one textField with a calendar next to it to help it look nicer.

Thanks in advance

UPDATE

Here is the code I have in the list.gsp

def list = {
    params.max = Math.min(params.max ? params.int('max') : 10, 100)
    [entryInstanceList: Entry.list(params), entryInstanceTotal: Entry.count()]
    def today = Calendar.getInstance()
    today.setTime(new Date())
    today.set(Calendar.HOUR_OF_DAY, 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])

So I wanted to know if I could开发者_如何学C do the same to sort the things that were filtered. Because now when I click on the title of each column, after being filtered, it orders every single item from the list. If I do it whenever I create new entries, it orders them from today on (only the things that you are seeing), but when I apply a filter and then order it sorts every single item on the list, not just what it being showed.

I think it sounds a little confusing, I hope you can help.

Thanks in advance!


The will pass the string value of the date to your controller. You'll need to parse the string that is sent to your controller into a Date object. Look at the parse() method of the Date class (http://groovy.codehaus.org/groovy-jdk/java/util/Date.html) and the SimpleDateFormat class (http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html). Use the documentation in SimpleDateFormat to create a format string for your date and then use the Date class to parse the String into a Date. For instance...

def dateFmt = "dd/MMM/yyyy"
def fechaCambioDateH 
def toCal
if(params?.fechaCambioH) {
   fechaCambioDateH = Date.parse(dateFmt, params?.fechaCambioH)
   toCal = Calendar.getInstance()
   toCal.setTime(fechaCambioDateH)
   // You may not need these anymore:
   toCal.set(Calendar.HOUR_OF_DAY,23)
   toCal.set(Calendar.MINUTE,59)
   toCal.set(Calendar.SECOND,59)
   toCal.set(Calendar.MILLISECOND,999)
}

Update:

Sorry, wouldn't fit in the comment:

Let me explain what's causing that behavior and you may be able to work through this one on your own; you seem like you're starting to get the hang of Grails. When you submit your filter form, you're submitting all your form data - date ranges, proyectoRuta, etc. Hopefully you already know this is a POST operation, the browser takes care of sending all that data to your server, Grails takes care of packaging it up into a convenient params object that you can access. However, when you click on the sort links, that's a GET operation. The browser is just sending the url to your back end. Basically, it's hitting your criteria search, but all the parameters you're checking for are null. So essentially you're getting the entire list of domain objects. At this point, your job gets a lot more complicated because you've just found yourself that we've all found ourselves in at one point or another: you need to manage state in a web app. The easiest way to do that is by storing data in the user's session. This introduces some complexity, more complexity since you've got your search logic spread out over a couple actions (default vs. filtered). You can basically either store the search results in the session and use a Comparator to sort them, or you can store the search criteria in the session and apply a different sort/order when someone clicks on the sort link.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜