简体   繁体   中英

How to display Selected option Of timeZoneSelect Tag in grails

When creating Customer with timezone, timeZone have the value. When i show the timeZone ,i want to display the selected Option.

For Example, i want to display SST, Samoa Standard Time -11:0.0(Selected Option) rather than Pacific/Midway(Value) .

What i have to change in show page for this functionality?

class Customer {

    static constraints = {
    }
    String name
    String timeZone
}

In create.gsp:

<div class="fieldcontain ${hasErrors(bean: customerInstance, field: 'timeZone', 'error')} ">
    <label for="timeZone">
        <g:message code="customer.timeZone.label" default="timeZone" />

    </label>
    <g:if test="${customerInstance?.timeZone}">
         <g:timeZoneSelect name="timeZone" value="${TimeZone.getTimeZone(customerInstance?.timeZone)}" />
    </g:if>
    <g:else>
         <g:timeZoneSelect name="timeZone" value="${customerInstance?.timeZone}"  />
    </g:else>
</div>

in show.gsp:

<span class="property-value" aria-labelledby="timeZone-label"><g:fieldValue bean="${customerInstance}" field="timeZone"/></span>

I have found the solution

 def show(Long id) {
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "list")
            return
        }

        def date = new Date()
        TimeZone tz = TimeZone.getTimeZone(customerInstance.timeZone);
        def shortName = tz.getDisplayName(tz.inDaylightTime(date), TimeZone.SHORT);
        def longName = tz.getDisplayName(tz.inDaylightTime(date), TimeZone.LONG);
        def offset = tz.rawOffset;
        def hour = offset / (60 * 60 * 1000);
        def min = Math.abs(offset / (60 * 1000)) % 60;
        def timeZone= shortName+", "+longName+" "+hour+": "+min
        [customerInstance: customerInstance,timeZone:timeZone]
    }

In Show.gsp:

<span class="property-value" aria-labelledby="timeZone-label">${timeZone}</span>

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