I have altered the src/templates/scaffolding/renderEditor.template file in a grails project, in order to insert, the necessary html field boxes (and javascript code) to configure and use jquery autocomplete, in all the relationships "many-to-one". (The code is shown below)

The autogenerated autocomplete (_form.gsp) works correctly ... but I need to show the correct values (code and description) into the autocomplete textbox when a user edit a record using the scaffolding.

To do that, I need to identify two fields inside the domain: one for code and the other for the description.

To face this problem, I have tried to create two dummies constraints, using the plugin "constraints", the first one used like code, the second one used like a description. I don't like this solution, because the contrainsts could be used several times into the domain.

The code altered into the src/templates/scaffolding/renderEditor.template file is the following: (Note the two input boxes and Javascript code used for autocomplete):

private renderManyToOne(domainClass, property) {

if (property.association) {

/* ORIGINAL CODE inside comments

def sb = new StringBuilder()

sb << '<g:select'

// id is "x" and name is "x.id" as the label will have for="x" and "." in an id will confuse CSS

like("code", "${params.term}%") // term is the parameter send by jQuery autocomplete

like("longName", "${params.term}%")

like("shortName", "${params.term}%")

}

projections { // good to select only the required columns.

property("id")

property("code")

property("longName")

}

}

def loincSelectList = [] // aArray List to add each Loinc details

def clist = LOINC.createCriteria().list(query)

clist.each {

// Add to map. jQuery autocomplete expects the JSON object to be with id/label/value

def loincMap = [:]

loincMap.put("id", it[0])

// Label is text showed int he drop-down list

loincMap.put("label", it[1] + " : " + it[2])

// Values is the code to be returned when the user select an item from drop-down list

loincMap.put("value", it[1] + " : " + it[2])

// Add the row to the array list

loincSelectList.add(loincMap)

}

return loincSelectList

}

}

I want something like that inside the domain class:

<code>

static autocompleteAble = {

fields = ["code", "longName"]

}

</code>

Then access this array from the src/templates/scaffolding/renderEditor.template in order to get the field names (code and longName) and generate the correct html code in _forms.gsp and fix the problem.

Other solution? Any Ideas?

Many Thanks in Advance. ... and excuse my bad English.

网友答案:

Reading and testing and testing again ... I have found the answer, follow the steps: