Quantcast

sortableColumn with calculated property?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

sortableColumn with calculated property?

John Moore
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?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: sortableColumn with calculated property?

Uday Pratap Singh
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.

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

--
View this message in context: http://grails.1312388.n4.nabble.com/sortableColumn-with-calculated-property-tp4631732.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: sortableColumn with calculated property?

Uday Pratap Singh
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

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.

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

--
View this message in context: http://grails.1312388.n4.nabble.com/sortableColumn-with-calculated-property-tp4631732.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: sortableColumn with calculated property?

John Moore
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: sortableColumn with calculated property?

Uday Pratap Singh
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 age

static mapping = { age formula: 'sql_formula_to_get_age(dob)' } }

You can define sql_formula_to_get_age() function within your database, or write the same code here.

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

--
View this message in context: http://grails.1312388.n4.nabble.com/sortableColumn-with-calculated-property-tp4631732p4631739.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: sortableColumn with calculated property?

John Moore
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.
Loading...