Grails - 在scaffolding中使用hasMany关系排序g:sortableColumn

时间:2014-01-04 21:23:13

标签: sorting grails gsp scaffolding

我有3个域类:

class Material {
String sku
String description
String uom
String category
String type
double standardPrice

static hasMany = [prices: Price]

    static constraints = {
        sku(blank:false, nullable:false)
        description(blank:false, nullable:false)
    }
}

class Price {

double price

static belongsTo = [material: Material, priceList: PriceList]


    static constraints = {

    }
}

class PriceList {
String priceListName
Date validFrom
Date validTo
Boolean isValid


    //static belongsTo = [supplier: Supplier]
    static hasMany =[prices: Price]
    static constraints = {
    }
}

我的PriceList更新gsp如下:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="main"/>
<title>Update price list</title>
</head>
<body>

    <g:form name="priceListlUpdateForm" id="${priceListInstance.id}">
    <div class="buttons" role="navigation">
            <g:actionSubmit action="save" class="edit" value="Update"   />
            <g:actionSubmit action="index" class = "cancel" value = "Cancel" />
            <g:actionSubmit action="delete" class ="delete" value="Delete" />
    </div>
    <div class="body">
        <h1>Update price list</h1>
    </div>
    <table>
        <tbody>
            <td>Price list name</td>
            <td>
                <g:textField name ="priceListName" value="${priceListInstance.priceListName}" />
            </td>
        </tr>
        <tr>
            <td>Valid From</td>
            <td>
                <g:datePicker name="validFrom" precision = 'day' value = "${priceListInstance.validFrom}" />
            </td>
        </tr>
        <tr>
            <td>Valid To</td>
            <td>
                <g:datePicker name="validTo" precision ='day' value = "${priceListInstance.validTo}" />
            </td>
        </tr>
        </g:form>

    </tbody>


    </table>


    <div class="constent scaffold-list" role=main>
    <h1>Materials for price list</h1>
        <table>
         <thead>
            <tr>
                <g:sortableColumn property="sku" title="SKU" />
                <g:sortableColumn property="description" title="Description"  />
            </tr>
         </thead>
         <tbody>
            <g:each in="${pricesInPriceList}" status="i" var="pricesRow">
            <tr>
                <td>${pricesRow.material.sku}</td>
                <td>${pricesRow.material.description}</td>
            </tr>
            </g:each>
          </tbody>
        </table>
    </div>  

</body>
</html>
PriceList Controller中的

更新如下:

def update(long id) {
        PriceList row=PriceList.get(id)
        def pricesInPriceList = row.prices
        [priceListInstance: row, pricesInPriceList: pricesInPriceList]
        //render "this is update controller"
    }

除了排序外,一切正常。 当我在sku上单击sortablecolumn或描述排序不起作用时(行随机排序)。 我坚持这种排序。谢谢你的帮助。 的问候,

2 个答案:

答案 0 :(得分:0)

要进行排序工作,您需要在控制器中实现一个列表方法。列表代码如下所示:

def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [priceListInstanceList: PriceList.list(params), priceListInstanceTotal:    
        PriceList.count()]
    }

答案 1 :(得分:0)

也许这对某些人有用。 我在PriceList控制器中更改了我的更新方法,如下所示:

def update(long id) {
    PriceList row=PriceList.get(id)
    //default sorting

    def pricesInPriceList = row.prices.sort{it.material.sku}

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

    if (params.sort && params.order == "desc"){
        pricesInPriceList.asList().sort{it.material."${params.sort}"}.reverse()
    }
    [priceListInstance: row, pricesInPriceList: pricesInPriceList]
}

现在与孩子一起排序完美。