Matt Westlake
Matt Westlake

Reputation: 3651

how do I write a set for g:select tag

enter image description hereHow do I write options so I can generate it into an HTML select? The problem with this is "options" needs a set, not an array

Here is everything I have. I know the naming convention is bad, and I will fix that, but for right now I've been on this issue for days now.

Controller Class

import org.springframework.dao.DataIntegrityViolationException
import grails.plugin.mail.*

class EmailServiceController {

    static defaultAction = "contactService"

def contactService() {
    def options = new ArrayList()
    options.push("Qestions about service")
    options.push("Feedback on performed service")
    options.push("Other")
    options.push("Why am I doing this")
    options
}

    def send() {
        sendMail(){
            to "[email protected]"
            from params.email
            subject params.subject
            body params.information
        }
    }
}

Domain class

class EmailService {

    static constraints = {
    }
}

g:select call from the gsp

<g:select name = "subject" from = "${options}" noSelection="Topic"/>

also tried the following with "${selectOptions}" instead of "${options}" with no luck

def selectOptions() {
    def options = new ArrayList()
    options.push("Qestions about service": "QAS")
    options.push("Feedback on performed service":"FoPS")
    options.push("Other":"Other")
    options.push("Why am I doing this":"WHY")
    return options
}

Upvotes: 2

Views: 2406

Answers (3)

Chris
Chris

Reputation: 8109

Your options is neither an array nor map. There is a syntax error. That's why you have only one option in your select. You need to enter either a real list or a map, like that:

def selectOptions() {
    def options = [:]
    options["Qestions about service"] = "QAS"
    options["Feedback on performed service"] = "FoPS"
    [options:options]
}

Using a map you can use it in the view like this:

<g:select name="subject" from="${options.entrySet()}" 
    optionValue="key" optionKey="value"
    noSelection="['Topic': 'Topic']"/>

Upvotes: 3

Gregg
Gregg

Reputation: 35864

Ok, I think I might know what is going on here. The missing piece to the question is what gsp is being called. Here is the appropriate way:

class EmailServiceController {

  def contactService() {
    def options = ["Qestions about service", "Feedback on performed service", "Other"]
    // assumes you are going to render contactService.gsp
    // you have to push the options to the view in the request
    [options:options]
  }

}

And then in contactService.gsp you would have:

<g:select name="subject" from="${options}" noSelection="['Topic': 'Topic']"/>

Upvotes: 6

doelleri
doelleri

Reputation: 19682

You need to use double quotes in your tags, not single quotes. With single quotes, you're just passing a String that looks like '${options}' instead of passing a GString with the value of options.

<g:select name="subject" from="${options}" noSelection="Topic"/>

In addition, assuming you're calling the contactService action, you need to return options instead of returning options.push("Other"). push() returns a boolean, which means the implicit return of contactService is the boolean result of push() instead of options.

Upvotes: 0

Related Questions