简体   繁体   中英

Working with the Grails g:timeZoneSelect tag?

I am wanting to use the g:timeZoneSelect tag within my application, problem is im finding the resulting html select to be quite overwhelming.

  1. Over 600 options are being displayed, IMHO this is to much to display to the user. Maybe someone could point me to an example of a much more manageable list of timezones? Maybe you have seen a site that does timezone selection well? Im sure over 600 option is "technically" correct, but this will just look like noise to the user.

  2. The display value of the timezone is to long.

Eg "CST, Central Standard Time (South Australia/New South Wales) 9.5:30.0"

Just "CST, Central Standard Time" or "Australia/Broken_Hill" would be better

Is there a way to address these issues via tag attributes of some sort (cant find any in the docs) or config that I am unaware of?

Or, is my best bet to wrap an html select within a custom tag lib and "roll my own" solution (Id prefer not to).

Thanks

Having a look at the source, there's no way to override the "optionValue" attribute, as it is set in the taglib method itself

So I guess you'd have to roll your own :-(

The source for the original tag is here , which should be a good starting point. You'd probably need something like this:

class MyNewTagLib {
    static namespace = 'my'
    def tzSelect = { attrs ->
        attrs['from'] = TimeZone.getAvailableIDs();
        attrs['value'] = (attrs['value'] ? attrs['value'].ID : TimeZone.getDefault().ID)
        def date = new Date()

        // set the option value as a closure that formats the TimeZone for display
        attrs['optionValue'] = {
            TimeZone tz = TimeZone.getTimeZone(it);
            def shortName = tz.getDisplayName(tz.inDaylightTime(date), TimeZone.SHORT);
            def longName = tz.getDisplayName(tz.inDaylightTime(date), TimeZone.LONG);

            return "${shortName}/${longName}"
        }

        // use generic select
        out << g.select(attrs)
    }
}

Then you could do:

<my:tzSelect/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM