Checkbox implementation

rorama

New member
I am still struggling with checkbox implementation since I havent found a comprehensive example. On the gsp, things are fine, but I do not know how to persist the checked items on the controller-side. Any help will be most appreciated.


gsp:

Code:
          <g:each in="${TBSMS.TBDrug.list(sort: 'id', order: 'asc')}" var="tbdrug" status="i">
							<label for="tbdrugs">${tbdrug.name}</label>
              <g:checkBox name="tbdrugs" value="${tbdrug.id}" checked="${specimenInstance?.patienttbdrug?.tbdrug? specimenInstance?.patienttbdrug?.tbdrug.contains(tbdrug) : 'false'}" />              
          </g:each>


controller:

Code:
??? - how do I persist the selected entries in the selectbox above?
 

rorama

New member
Hi All, I seriously need help from anybody who has ever implemented checkboxes in Grails. Days are turning into weeks but I am unable to make a headway. Kindly post for me some code for checkbox, otherwise, I am having second thoughts about proceeding. Thanks.
 

dave

Administrator
Staff member
In your controller action, start it with a "println params"
You should see all the parameters available to you in your action.

Looking at your code, I think I would expect to see a tbdrugs[] made available.
 

rorama

New member
Yes, my params has the following as part of it:

tbdrugs:[1, 3, 4, 6] - where the numbers are the ids of the selected items
There is also _tbdrugs:[, , , , , , , , , , ] - containing blanks totalling the number of available checkbox options

In my update controller action, I have the following, which does not work. I have basically added the logic in bold below - adapted from online sources - not sure if it makes sense.

Code:
def update() {

def specimenInstance = Specimen.get(params.id)


if (!specimenInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'specimen.label', default: 'Specimen'), specimenInstance.laboratorynumber])
redirect(action: "list")
return
}

if (params.version) {
def version = params.version.toLong()
if (specimenInstance.version > version) {
specimenInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[message(code: 'specimen.label', default: 'Specimen')] as Object[],
"Another user has updated this Specimen while you were editing")
render(view: "edit", model: [specimenInstance: specimenInstance])
return
}
}

specimenInstance.properties = params


[B]params.each {
if (it.key.startsWith("tbdrugs"))
{
specimenInstance.patienttbdrug << TBDrug.get((it.value - "tbdrugs") as Integer)   ////throws the error "Cannot cast object '[Ljava.lang.Object;@b2d4b3' with 
                                                                                                               ////class '[Ljava.lang.Object;' to class 'java.lang.Integer'" on the line
}
}
[/B]


if (!specimenInstance.save(flush: true)) {

render(view: "edit", model: [specimenInstance: specimenInstance])
return
}


...
}
 
Last edited:
Top