开发者

Grails: <g:select

How can I achieve the following:

I have a boolean stored in my Domain, by default开发者_运维问答 Grails creates a checkbox as a control. I want a select control with values : Active/Inactive. On selecting Active the value True should be passed and On selecting InActive the value False should be passed.

How can I achieve this using

<g:select name="status" from="" optionKey="" value=""  />

Much appreciated.


I don't know if this is the best approach, but you could have an enum to do the work:

public enum SelectOptions{
    ACTIVE(true, 'Active'),
    INACTIVE(false, 'InActive')

    Boolean optionValue
    String name

    SelectOptions(boolean optionValue, String name){
        this.optionValue = optionValue
        this.name = name
    }

    static getByName(String name){
        for(SelectOptions so : SelectOptions.values()){
            if(so.name.equals(name)){
                return so;
            }
        }
        return null;
    }

    static list(){
        [ACTIVE, INACTIVE]
    }

    public String toString(){
        return name
    }
}

Add an instance of the SelectOptions enum to your domain:

class MyDomain {
    SelectOptions selectOptions = SelectOptions.ACTIVE
    //Other properties go here

    static constraints = {
        selectOptions(inList:SelectOptions.list())
        //other constraints
    }
}

Then in your GSP view:

<g:select
    name="status"
    from="${myDomainInstance.constraints.selectOptions.inList}"
    value="${myDomainInstance.selectOptions}" />

In your controller's save method, you need to get the correct enum from the String value submitted by the view:

def save = {
    SelectOptions selectOption = SelectOptions.getByName(params.status)
    def myDomainInstance = new MyDomain(params)
    myDomainInstance.selectOptions = selectOption
    // proceed to save your domain instance
}


Ok After a lot of brainstorming I could figure it out

My Code is simpler and goes like this:

package exproj.masters

 public enum ExamDurationTypes{
    FULL_EXAM(1, 'For Whole Exam'),
    PER_QUESTION(2, 'Per Question')

    Integer optionValue
    String name

    ExamDurationTypes(Integer optionValue, String name){
        this.optionValue = optionValue
        this.name = name
    }

    static getByName(String name){
        for(ExamDurationTypes edt : ExamDurationTypes.values()){
            if(edt.name.equals(name)){
                return edt;
            }
        }
        return null;
    }
    static list(){
        [FULL_EXAM, PER_QUESTION]
    }

    public String toString(){
        return optionValue
    }

}

Then I added it to my Domain class

class Exam {
.
.
.

Integer durationType

    static constraints = {
        durationType(inList: ExamDurationTypes.list())
    }   

}

In my GSP Page I cracked it like this

<g:select
name="durationType"
from="${exproj.masters.ExamDurationTypes.values()*.getName()}"
keys="${exproj.masters.ExamDurationTypes.values()*.getOptionValue()}"
value="${examInstance.durationType}" />

Finally produces this:

<select name="durationType" id="durationType">
<option value="1">For Whole Exam</option>
<option value="2">Per Question</option>
</select>

Enjoy Coding

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜