codesnooker
codesnooker

Reputation: 1181

Custom g.select taglib with default selected value in grails

We are trying to create a custom g.select using taglib. We are successful in creating that but now we want g.select should have some default selected value. How can we do that?

def timePicker = { attrs ->
    def hours = 0..21
    def stringHours = hours.collect{ String.format('%02d', it) }

    def minutes = 0..59
    def stringMinutes = minutes.collect{ String.format('%02d', it) }

    out << "${select(from: stringHours, name: attrs.name + '.hour')}"
    out << "${select(from: stringMinutes, name: attrs.name + '.minute')}"
}

For example, the default selected value in hour could be 12 and in minutes will be 30. Also, we want to pass this values from the GSP file.

i.e. in GSP

<me:timePicker h="12" m="30" />

Upvotes: 1

Views: 681

Answers (1)

Arturo
Arturo

Reputation: 1089

You can pass the value that is to be selected by default in the value attribute. Something like this:

out << "${select(from: stringMinutes, name: attrs.name + '.minute', value: attrs.h)}"
out << "${select(from: stringMinutes, name: attrs.name + '.minute', value: attrs.m )}"

Upvotes: 2

Related Questions