0

I am trying to pass params.sort to my controller.

 if (params.sort && params.order == "asc") {
     pricesInPriceList = row.prices.sort{it.material."${params.sort}"}
 }

 if (params.sort && params.order == "desc"){
     pricesInPriceList = row.prices.sort{it.material."${params.sort}"}.reverse()
 }

 [priceListInstance: row, pricesInPriceList: pricesInPriceList]

It works fine with following gsp:

<tr>
    <g:sortableColumn property="sku" title="SKU" />
    <g:sortableColumn property="description" title="Description"  />
</tr>

Now if I change my gsp to following:

<tr>
   <g:sortableColumn property="material.sku" title="SKU" />
   <g:sortableColumn property="material.description" title="Description"  />
</tr>

and my controller part to:

if (params.sort && params.order == "asc") {

    pricesInPriceList = row.prices.sort{it."${params.sort}"}
}


if (params.sort && params.order == "desc"){
    pricesInPriceList = row.prices.sort{it."${params.sort}"}.reverse()
}

Why this is happening? now my params.sort has a value material.sku for example however if I want to evaluate it."${params.sort}" it does not work. But if I will change my params.sort to sku and then change my controller to it.material."${params.sort}" everything is working. Where I am making mistake? Thank you.

Robert
  • 525
  • 1
  • 6
  • 21
  • what is `it`, where you're getting it, what it contains? – Igor Artamonov Jan 05 '14 at 16:24
  • Take a look at http://stackoverflow.com/questions/7784276/grails-accessing-nested-fields-using-gstrings or http://stackoverflow.com/questions/4077168/access-object-properties-in-groovy-using – Becca Gaspard Jan 05 '14 at 21:18
  • @beccagaspard thank you. This point me to the core of the problem. But still I need to inject my material instance (which is not big difference by declaring this in controler). I have view with parent.child.property in one column and property in second one. The most elegant solution would be to have params.sort no matter if this is parent.child or parent property and use the same code. – Robert Jan 06 '14 at 12:54

2 Answers2

0

from your modified gsp code :

<tr>
   <g:sortableColumn property="material.sku" title="SKU" />
   <g:sortableColumn property="material.description" title="Description"  />
</tr>

one most possible reason why your code is not working is

1.property="material.sku" did not match the property of the field and i am sure you never
define the property name in that way. cause the

  property - name of the property relating to the field  

2.if you are trying to read property from message source, then you could do as follows

<tr>
     <g:sortableColumn property="sku" title="SKU" titleKey="material.sku" />
     <g:sortableColumn property="description" title="Description" titleKey="material.sku"/>
   </tr>

Hope this may help you.

Regards
Motilal

Motilal
  • 266
  • 1
  • 9
  • Hello @Motilal.Thank you for your input but this does not work. I need to find a way for sorting views with parent.child and parent properties in one row. Regards, – Robert Jan 06 '14 at 12:54
0

This sort will work for both cases - params.sort = 'parent' or params.sort = 'parent.child'

row.prices.sort{ params.sort.tokenize('.').inject(it){v, k -> v."$k"} }

It's essentially the same as the solution for this question: Grails accessing nested fields using gstrings

Community
  • 1
  • 1
Becca Gaspard
  • 1,165
  • 7
  • 19