سؤال

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" />
هل كانت مفيدة؟

المحلول

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 )}"

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top