2014-01-05 27 views
0

我想将params.sort传递给我的控制器。如果我改变我的普惠制下Grails将点传递给控制器​​

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

现在:

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

和我的控制器部分:

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() 
} 

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] 

它正常工作与以下GSP为什么发生这种情况?现在我的params.sort有一个值material.sku例如但如果我想评估it."${params.sort}"它不起作用。但是,如果我将我的params.sort更改为sku,然后将我的控制器更改为it.material."${params.sort}",所有内容都可以正常工作。我在哪里犯错? 谢谢。

+0

是什么'it' ,你在哪里得到它,它包含什么? –

+0

看看http://stackoverflow.com/questions/7784276/grails-accessing-nested-fields-using-gstrings或http://stackoverflow.com/questions/4077168/access-object-properties-in-groovy -using –

+0

@beccagaspard谢谢。这就指出了问题的核心。但是我仍然需要注入我的材质实例(通过在控制器中声明它并没有太大区别)。我有一个列parent.child.property和第二个属性查看。最优雅的解决方案将是有params.sort,无论这是parent.child或父属性,并使用相同的代码。 – Robert

回答

0

该排序两种情况下工作 - params.sort = 'parent'params.sort = 'parent.child'

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

它本质上是一样的这个问题的解决方案:Grails accessing nested fields using gstrings

+0

谢谢。完美的作品。问候。 – Robert

0

从修改后的GSP代码:

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

一个最可能的原因,为什么你的代码不工作是

1.property =“material.sku”没有现场的属性相匹配,并我相信你永远不会
以这种方式定义属性名称。导致

property - name of the property relating to the field 

2.如果试图从消息源读取属性,那么你可以做如下

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

希望这可以帮助你。

问候
Motilal

+0

Hello @ Motilal.Thank您的输入,但这是行不通的。我需要找到一种方式来排序视图parent.child和父属性在一行。问候, – Robert