Question

I have 3 domain classes:

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 = {
    }
}

my PriceList update gsp is following:

<%@ 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>

update in PriceList Controller is following:

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

Everything is working fine except sorting. When I click sortablecolumn on sku or description sorting is not working (rows are sorted randomly). I stuck with this sorting. Thank you for help. Regards,

Was it helpful?

Solution 2

Maybe it will be useful for somebody. I have changed my update method in PriceList controller as follows:

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]
}

now sorting with child works perfect.

OTHER TIPS

To get the sorting work, you will need to implement a list method in your controller.. The list code will look like this:

def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [priceListInstanceList: PriceList.list(params), priceListInstanceTotal:    
        PriceList.count()]
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top