Search code examples
grailsgrails-ormgrails-2.4

Grails 2.4.4 Dynamic One-to-Many Forms


I've met an already known old topic that oddly enough is badly documented (if it is even documented). I've been trying to create a dynamic table for a form with a one-to-many relationship in Grails 2.4.4, and for that I had to base myself on a tutorial for 2.1

I'm using writetable jQuery plugin to help me develop a good looking responsive HTML for the form, and it works like a charm. It's working exactly as expected and generating this HTML excerpt when I delete one row:

Example 1: Trying to delete row 1 (zero-based) (Pressing delete button on row 1)

<input type='hidden' name='faixas[1].deleted' value='true'/>

Given the classes:

package mobvida

class Questionario {
    String titulo
    int score = 0
    List faixas
    List questoes

    static belongsTo = [usuario:Usuario]
    static hasMany = [ questoes : Questao,
                       faixas : Faixa ]

    static constraints = {
    }

    static mapping = {
        faixas cascade: "all-delete-orphan"
    }
}

class Faixa {
    int limiteInf
    int limiteSup
    String nome

    boolean deleted

    static transients = [ 'deleted' ]
    static belongsTo = [questionario: Questionario]
    static constraints = {
    }

    def String toString() {
        return "${nome} (${limiteInf} - ${limiteSup})"
    }
}

In the QuestionarioController.groovy file I have the following under the controller class:

@Transactional
def update(Questionario questionarioInstance) {
    if (questionarioInstance == null) {
        notFound()
        return
    }

    questionarioInstance.faixas.removeAll{(it.deleted || (it == null))}
    // questionarioInstance.questoes.removeAll{(it.deleted || (it == null))}

    log.info "${questionarioInstance}"

    for (it in questionarioInstance.faixas) {
        log.info "${it}(${it.id}) state: ${it.deleted}"
    }
    //...

I wouldn't delete anything, even with the html added to the page before submitting. With the log instructions on the update() method, I was able to see that the row I was trying to delete held the attribute deleted as false. Question is... What am I missing here for it to delete?


Solution

  • static constraints = {        
            deleted bindable: true
        }