views:

130

answers:

1

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

+1  A: 

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.

proflux
I think I'm going to use the jQuery-UI, there's a lot of information online and for some reason it seems easier.
fgualda87
For some reason the calendar is not open up when I click the input field -__-" How do I declare the calendar in the controller? The grails plugin page says to put def testDatePicker = { } but that doesn't make a difference if I put it nothing happens :S
fgualda87
did you install the gui plugin? set the body css class to yui-skin-sam?
proflux
I need gui plugin in order to use jquery ui?
fgualda87
I installed grails ui but it was too limited, I could not change the text field sizes or anything, so I think jquery ui is better, no?
fgualda87
Hey I'm having a problem with the old stuff, I'm using the datepicker while i get the textfield and the calendar working, but when I apply the filter and click on the title on top of the new list... to like order it in ascending or descending order, it does not work, it shows me the whole list, no filter applied. I'm sure is something small and easy, do you know how to fix this?
fgualda87
Hmm, not really all that easy actually... you'll have to manage the filter data either in the session or make it so it's sent each time someone sends clicks on one of the sort links. Much more involved than I can describe here in the comment field.
proflux
can you help me with the code? :$
fgualda87
I have pretty limited time but if you try on your own and run into problems I will take a look when I can.
proflux
Yes I am doing that, I will post new code later when I get it to compile correctly.
fgualda87
I am using the calendar plugin, do you know where I can resize the input field by any chance?? It gives me an automatically 20 character field and I don't need it to be that long. I tried `\.grails\1.3.2\projects\eval\plugins\calendar-1.2.1\web-app\js\calendar.js` but I couldn't find it.
fgualda87
I've never used the calendar plugin before but from the documentation I don't see any way of directly specifying it.
proflux
I found it in the CalendarTagLib hidden in some folders, lol. Problem solved. I have a question... I will update code...For the list.gsp to show everything from today on you used the code up there, and I can sort that from ascending and descending order without showing everything on the database, is there any way to do something like that but using the values from the filter??
fgualda87
see updated post above...
proflux
great, it seems like storing the search criteria in the session and applying a different sort/order when someone clicks on the sort link is what I am looking for, but creating sessions seems more complicated, also, this is supposed to be running like 24/7 like a real web page, I don't know if that makes any difference. Do you have a link to a tutorial or somehting related to sessions?
fgualda87
or maybe just store the search results in the session
fgualda87
It works the same as session in java servlets; it's actually the same session. In your controllers you have access to a variable called session. Just add what you want to keep track of into the session and it will be there for you on subsequent calls. Each user has their own session. The container handles session management so when a user hits your site they get a session, and when they are idle for a while, the container destroys the session. So for instance to get started try doing something like session.results = results and then in later calls you can access session.results directly.
proflux
I don't quite get what you are saying, I'm new to the whole server and databases thing. Can you give me a little example? Once I get it I'll do everything from there.
fgualda87
I have a question...I have this at the top of each column in the searchResults.gsp ` <g:sortableColumn property="lastUpdated" title="Fecha Ultima Modificacion   " bean="Entry" update="entryInstanceList" />` What if I copy the results of the search into a list and at the end I change that `update="entryInstanceList"` to `update="newSearchList"` ? Do you think that would fix the sorting of the columns?
fgualda87
I'm not sure. I don't see the update attribute listed on the sortableColumn documentation. The places where I have seen the update parameter in Grails are on ajax components and it usually indicates the id of the div that should be updated with the ajax response. Since it isn't mentioned in the documentation I'm not sure what it does exactly. I am going to be working an extremely long day today because we've got a big release at work. Chances are I'll have some down time and if I do I'll try to put together a blog post on sorting/searching.
proflux
Thanks man! Good luck with everything!
fgualda87
http://grailsexample.com/confluence/display/blog/2010/07/20/Searching+With+Grails+-+Part+1and http://grailsexample.com/confluence/display/blog/2010/07/20/Searching+With+Grails+-+Part+2
proflux