Search code examples
grailsgrails-controller

how to bind/update Whole InstanceList in Controller?


I am new bie in grails and i have following query.

Problem Domain:

  • Parent Domain Class has a boolean transient property "isValid".

  • Child Domain has a boolean property "isValid".

  • Parent/list.gsp allow users to alter isValid property of Parent Instance in ParentInstanceList, over an update function in ParentController, which i can further use to set each Parents children's property "isValid".

Parent Domain Class

class Parent {

String name
boolean isValid = false

static hasMany = [children:Child]
static transients =['isValid']

}

Child Domain Class

class Child {

String name
boolean isValid
Parent parent

static belongsTo = [parent:Parent]

}

parent/list.gsp

    <g:form method="post" action="update" >
    <div class="list">
        <table>
            <thead>
                <tr>

                    <g:sortableColumn property="id" title="${message(code: 'parent.id.label', default: 'Id')}" />

                    <g:sortableColumn property="name" title="${message(code: 'parent.name.label', default: 'Name')}" />
                     <g:sortableColumn property="isValid" title="${message(code: 'parent.isvalid.label', default: 'isValid?')}" />

                </tr>
            </thead>
            <tbody>
            <g:hiddenField name="parentInstanceList" value="${parentInstanceList }"/>
            <g:each in="${parentInstanceList}" status="i" var="parentInstance">

                <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">

                    <td><g:link action="show" id="${parentInstance.id}">${fieldValue(bean: parentInstance, field: "id")}</g:link></td>

                    <td>${fieldValue(bean: parentInstance, field: "name")}</td>
                    <td>
                    <g:checkBox name="isValid" value="${parentInstance?.isValid }"/>
                    </td>

                </tr>
            </g:each>
            </tbody>
        </table>
    </div>
    <div class="paginateButtons">
        <g:paginate total="${parentInstanceTotal}" />
    </div>

    <div class="buttons">
        <span class="button">
            <g:actionSubmit class="update" action="update" value="${message(code: 'default.button.update.label', default: 'update')}" />
        </span> 
    </div>

Parent/list.gsp snapshot : http://img156.imageshack.us/i/parentlistview.png/

Query: how can i track down which parentInstance's property "isValid" is not checked/unchecked that i can easily set parent's children property "isvalid" .

def parentInstanceList  = params.parentInstanceList
    parentInstanceList.each {
        Parent parent = it
        parent.children.each{
            Child child = it
            child.isValid=parent.isValid
        }
    }

So far i am able to retrieve ids of all parents over params.parentInstanceList but i can not figure it out how can i bind changed properties of each parentInstance. if there is a single parentInstance then one can easily do it

Parent parentInstance = Parent.get(params.id)
 parentInstance.properties = params

thanks in advance

Rehman


Solution

  • I prefer to render for each line a specifically named checkbox:

    <g:checkBox
            name="Parent_${parentInstance.id}"
            value="${parentInstance.isValid ? 'on' : 'off'}" />
    

    and then collect all the checked checkboxes:

    def update = {
        Collection<Parent> checked = params.collect{ Map.Entry that ->
            that.value != "on" ? null : getParentForCheckbox(that.key)
        }.findAll{ it != null }
    
        ... // and then assign 'isValid' by hand
    }
    

    You could use array properties, that it, name the checkboxes like parentInstanceList[0], and then assign properties = params to some object with parentInstanceList property, but it's error prone: you have no guarantee that the parentInstanceList you rendered in HTML will be the same as the that you assign properties to.