0

使用时区创建客户时, timeZone 具有价值。当我显示 timeZone 时,我想显示选定的选项。

例如,我想显示SST, Samoa Standard Time -11:0.0(Selected Option)而不是Pacific/Midway(Value)

我必须在显示页面中更改此功能的哪些内容?

类客户{

    static constraints = {
    }
    String name
    String timeZone
}

在 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>

在 show.gsp 中:

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

1 回答 1

0

我找到了解决方案

 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]
    }

在 Show.gsp 中:

<span class="property-value" aria-labelledby="timeZone-label">${timeZone}</span>
于 2013-08-01T08:44:35.607 回答