views:

293

answers:

2

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!

http://stackoverflow.com/questions/3096572/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! -Fernando

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?

IPDATE:

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>
+1  A: 

Sounds like you want to show the search results in a 'list' view, so they show up in a table just like when unfiltered. You can just reuse that view and pass in the filtered results as the instance list.

Do you have views for your Entry domain object? If not, generate some scaffolding views with grails generate-view Entry. Then in your controller, make your searchResults method look something like this:

def searchResults = {
    def entryInstanceList = Entry.list() // replace with actual filtering logic
    render(view:'list', model: [entryInstanceList: entryInstanceList])
}
ataylor
Yes I got everything right, thank you.I have one problem though, I will update the question for everyone to see...
fgualda87
+1  A: 

Hi Fernando,

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)
    }
}
proflux
I don't know if that's a good option... I gave searchResults.gsp the same HTML and CSS that I gave the list.gsp. So I think I need to work on the list.gsp to show the "results" filtering the dates from today on. Doest that make any sense?
fgualda87
lastUpdated is a special variable in Grails, but it may actually do just what you want; it will update that value to the current time each time the object is modified and saved.
proflux
It seem like you are just getting started in Grails and I want to help, but I don't want to help so much that you aren't learning for yourself. Consider your screen shot above (is that list.gsp?) and compare it with the gsp I provided. Both have a search form on top and results on the bottom. I can tell you that between the info from your other question, what you posted here, and what I posted here you have all the pieces to make your app do what you want it to do. You just need to figure out how to put it all together, let me know if you get stuck and I'll try to help you get unstuck.
proflux
I know lastUpdated is a special variable. And Yes, that is list.gsp up there, the thing is that when I apply the filter it redirects me to a new .gsp file (searchResults.gsp) so I made it just like list.gsp, the thing is that I don't know how to make the date filter... I'll upload a new picture of my list.gsp, how the searchResults.gsp looks too. Maybe I don't need to redirect it to a new gsp page but to update the list on the list.gsp?
fgualda87
Did you read the Grails documentation on using criteria queries like I mentioned in my answer? There are a couple specific examples of filtering by dates.
proflux
Should I read everything or a specific section?
fgualda87
I would read all of section 5.4.2 and then familiarize yourself with the list of possible comparators in section 5.4.1.
proflux
Can you post the filter code you're using that's not filtering the dates correctly?
proflux
ok, will do, thanks ^_^
fgualda87
Ok, I just updated the post.
fgualda87
How are you populating the dates in your database? I know that the datePickers we used yesterday on that form had a precision of 'day'. If you are storing hour/minute/second precision in your database that would be why the filters aren't working. If that's the case, you have a couple options i.) change the date picker to use precision='hour' or 'minute' and make the user select down that detail or ii.) use a between criteria to find dates between the day that is selected and the next day - see the 2nd example in section 5.4.2 under 'Querying Associations' sub-heading.
proflux
Ok , I wrote on the controller: results = entryCriteria.findByFechaCambioBetween( fechaCambioD, fechaCambioH ) which are the two dates used in the filter. But I get an error saying No such property: fechaCambioD for class: eval.EntryController How do I assign it to the ones used in the filter?? I tried using ${it.fechaCambioD} and ${params.fechaCambioD} and it didn't work either.
fgualda87
Yes, my precision is day... (for lastUpdated it shows hour and minute but in the filter just by using the day as precision is fine) and yes I used the between criteria, I just don't get how to call them from that def
fgualda87
ok is a different between criteria, lemme see if I can make it work using that one.
fgualda87
Oh, you can't combine the dynamic finders with the criteria like that... will update the answer with some code.
proflux
and the params?.day should be like that or params?.fechaCambio ?(I don't even know why I made the variables in spanish when I'm gonna be the only one seeing it -__-" )
fgualda87
It's still not filtering the dates. Maybe an and{ } ?
fgualda87
I notice in your findBy statement, you're using the variables fechaCambioD and fechaCambioH. In the criteria you posted you used params?.proyectoRuta for your title search. On your datePicker in the gsp, what is the name="xxxx" attribute set to?
proflux
oops, missed your earlier comment - yes, if you called the datePicker fechaCambio then YES, definitely you should use params?.fechaCambio instead of params?.day
proflux
yes I named it fechaCambio.but even though I'm doing it like that is not working...I have if(params?.fechaCambio) { between("day", "%${params.fechaCambioD}", "%${params.fechaCambioH}") } that should be ok...
fgualda87
Is the property in your domain class called fechaCambio also? If so then you need: between("fechaCambio", params.fechCambioD, params.fechaCambioH). You shouldn't need the quotes or the %'s in the between clause.
proflux
Nope, is not working... :/
fgualda87
and yes, it is called fechaCambio in my domain class too
fgualda87
Ok now im desperate... nothing seems to work :S
fgualda87
Can you post the domain class, controller method, and the g:datePicker code please?
proflux
proflux
YES!!!! Thank you so much!!
fgualda87
You are welcome!
proflux
I have one more question though... Is not remembering the filters... Like once it shows me the results, the datepickers go back to today's date... I know I saw that somewhere... Do you know how I can change it??
fgualda87
Like the default values to be the same as last time... and if =null leave it blank...?
fgualda87
2 changes to do that. 1.) on your fechaCambioD datePicker, add a value attribute <g:datePicker name="fechaCambioD" value="${fechaCambioH}" /> do the same for fechaCambioH 2.) on your render statement in the contoller, pass the original fechaCambioD and H values back in your model map: render(view:'searchResults', model:['fechaCambioD':params?.fechaCambioD, 'fechaCambioH':params?.fechaCambioH]That should do it..
proflux
but wouldn't that switch the values?? on 1.) wouldn't that assign the value of fechaCambioH to fechaCambioD?
fgualda87
ok, nvm, you made a mistake... I got it :D thanks... now i need to fix some details...
fgualda87
name="fechaCambioD" value="${fechaCambioD}" ... Not H...Also I have a edit and delete button in the last column... and when I click them it tells me the id was not found... How do I get them? ${it.id} ??
fgualda87
I think I answered my own question, LOL.So I'll ask you my last question to close this book. When I first go to the list before applying any filters, it shouldn't show anything from the past, even if they are there, the only way I should be able to get to them is by using the filter. So is like a filter that shows only what hasn't happened yet with precision day, but it should load automatically. I'm guessing I would have to do some coding in the def list in the controller, right?
fgualda87
No, is not working... :S I will update the post so you can see the code...
fgualda87
Yeah, just post the code for the controller and the gsp form when you get a chance and I'll take a look.
proflux
So the problem is in the searchResults that last g:hiddenField...If I change the code with the server up and I take that "?" from "it?.id" it works and I can edit it with no problem, but if I take the ? off before applying the filter it throws me an error... : Cannot get property 'id' on null object And it tells me the error is searchResults.gsp:44 which is the closing </g:form>
fgualda87
Wait, I'm lost now... I think I misunderstood what you wanted. the ? i actually the null check operator. If the object is not null, it tries to get the id on it. If it is null, it just skips it. So value="${it?.id}" will evaluate to value="" if it is null and value="123" or whatever the id value is. I don't quite understand the problem. Going to be offline for a while as I'm headed home and then for dinner; will check back later tonight.
proflux
I wrote something and for some reason it didn't post it. Yeah I agree, is a little confusing by now. I am missing 3 or 4 things so far, I need to show list.gsp with a filter applied... like that filter should always be on when list.gsp loads, it should only show events happening from today on, nothing from the past, I should only be able to get to the ones in te past using a filter. I need a clear button but that should be just a g:link to list.gsp so that should be easy. And I need to make the dropdown boxes in list.gsp blank for the dates except the first one that should show today's date.
fgualda87
I think I saw that somewhere, I'm gonna ask it here in another question anyways, but it should be simple, I know I saw it somewhere in my code. The other thing that I'm missing is the last column I have, is two icons, one for edit and one for deleting the item, when I filter the results, for some reason is not getting the id, it gives me an error saying it did not find a proyectoRuta (title) with the id , and I checked the message property and it should tell me the id number but here it just shows blank.
fgualda87
Okay, just post the link to the new question when you ask it and I'll take a look. I think you've solved the hardest part of your problem though, it shouldn't be too bad polishing it up a bit.
proflux
Yes I know... I have almost everything... The only thing that I'm missing is the it.id that for some reason is not working and the automatic filter when list.gsp loads showing only future events
fgualda87
here is the link to the new question http://stackoverflow.com/questions/3119559/grails-edit-and-delete-links-not-working
fgualda87