porno porno rokettube
Results 1 to 4 of 4

Thread: Checkbox implementation

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    9

    Checkbox implementation

    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?

  2. #2
    Junior Member
    Join Date
    Dec 2012
    Posts
    9
    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.

  3. #3
    Administrator dave's Avatar
    Join Date
    Mar 2010
    Location
    West Yorkshire, UK - Valencia Spain
    Posts
    110
    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.
    Connect with me on twitter : @davebrown1975 http://twitter.com/davebrown1975

    Web & Mobile Development by Tucanoo Solutions Ltd

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    9
    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
    
    
    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
    }
    }
    
    
    
    if (!specimenInstance.save(flush: true)) {
    
    render(view: "edit", model: [specimenInstance: specimenInstance])
    return
    }
    
    
    ...
    }
    Last edited by rorama; 20-12-2012 at 06:51 PM. Reason: changing tbdrug_ to tbdrugs

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Forum Design by Tucanoo Solutions Ltd Grails Development Specialists.