|
I have a list of people I'd like to be user-sortable by various criteria. One of these is currentAge - I have a column displaying the calculated current age, and ideally I'd like the user to be able to click on the column header and sort by this. There is no actual 'currentAge' property (it wouldn't be too useful), and instead there's a 'getCurrentAge' method, which works fine for displaying the (calculated) value but not for sorting by (I get an error message saying there's not such property).
Now, clearly in this case I could instead simply sort by date of birth and the effect would be the same, but there will be other cases where a handy stand-in property is not available. So, two questions... Is this possible, by some not-well-documented means? And secondly, if possible, is it advisable? That is, would sorting on some calculated value be lousy from a performance point of view? Obviously it would depend on the size of the collection, but maybe if Hibernate is smart enough to do this kind of thing at the db level it would be OK? |
|
Hi John
You can create derived poperties in grails either by defining a getter or using formula. If you can find out the current age by using some sql method then I you will be able to sort your data on the basis of derived fields.
For more information how to create formula and how to use them you can refer grails docs http://www.intelligrape.com/blog/2010/12/31/create-transients-using-formula-in-grails or read the blog http://www.intelligrape.com/blog/2010/12/31/create-transients-using-formula-in-grails
I hope it will help
Regards Uday Pratap Singh Intelligrape software (P) Ltd. On Wed, Jul 18, 2012 at 5:32 PM, John Moore <[hidden email]> wrote: I have a list of people I'd like to be user-sortable by various criteria. One |
|
Reference to grails docs is http://grails.org/doc/latest/guide/GORM.html , search for formula you will find out
Regards Uday Pratap Singh Intelligrape software (P) Ltd. On Wed, Jul 18, 2012 at 5:48 PM, Uday Pratap Singh <[hidden email]> wrote: Hi John |
|
I think you may have misread my post. I'm already doing this - I have a 'getCurrentAge' method in the domain class (declared in the static transients list). This cannot be used as the property for the sortableColumn tag, though - it throws and exception saying there is no such property.
|
|
Sorting on derived field can be done only if you have created it using formula, it doesn't work with getters.
I think something like following would work
class Person {
Date dob
Integer ageRegards Uday Pratap Singh Intelligrape software (P) Ltd. On Wed, Jul 18, 2012 at 6:27 PM, John Moore <[hidden email]> wrote: I think you may have misread my post. I'm already doing this - I have a |
|
Please accept my humble apologies - I actually misread YOUR post, not the othe way round! I've not actually come across the formula concept in Grails before, it seems very interesting. I'll check it out - thanks for the suggestion.
|
| Powered by Nabble | Edit this page |
