给Grails 3中的datePicker做个“小手术”

Grails自带了时间选择标签:datePicker,但是格式却是日月年,同时日、年中只有数字,却没有中文。见下图:


g:date picker的显现效果

这对于我大中华用户,却是非常不习惯。我们能做的就是大刀阔斧的改代码,定义自己的datePicker标签。

从Github上下载Grails的源代码,在grails-plugin-gsp/src/main/groovy/org/grails/plugins/web/taglib下找到FormTagLib.groovy ,Grails的date picker标签就在这个文件里面。

在自己的工程目录下,执行Grials create-taglib命令创建TestTagLib,将FormTagLib.groovy中的datePicker实现部分拷贝到TestTagLib下,做如下修改:
1.调整年月日的顺序;
2.为日、年添加中文信息。

执行后,效果如下:


my:date picker的显示效果

看起来是不是很简单?其实还有点小麻烦,Grails 3对代码做了很大的改动过,在TagLib中找不到熟悉的代码,怎么变化这么大。所以在修改datePicker的代码时,要大胆心细,才能让“手术”成功。如下,是成功修改后的部分代码,供参考。

package com.prohitbit.taglib
import grails.gsp.TagLib
import groovy.transform.CompileStatic
import org.grails.plugins.web.GrailsTagDateHelper
import java.text.DateFormat
import java.text.DateFormatSymbols
import org.springframework.web.servlet.support.RequestContextUtils as RCU
import org.springframework.web.servlet.support.RequestDataValueProcessor
@TagLib
class MyTagLib  {
    GrailsTagDateHelper grailsTagDateHelper
    RequestDataValueProcessor requestDataValueProcessor
    static namespace = "my" 
    
    Closure datePicker = { attrs ->
        ......

        // Change this hidden to use requestDataValueProcessor
        def dateStructValue = processFormFieldValueIfNecessary("${name}","date.struct","hidden")
        out.println ""

        
        // 创建“年”选择
        if (precision >= PRECISION_RANKINGS["year"]) {
            out.println "'
        }
        // create month select
        if (precision >= PRECISION_RANKINGS["month"]) {
            out.println "'
        }

        // create day select
        if (precision >= PRECISION_RANKINGS["day"]) {
            out.println "'
        }

        // do hour select
        if (precision >= PRECISION_RANKINGS["hour"]) {
            out.println " :'

            // If we're rendering the hour, but not the minutes, then display the minutes as 00 in read-only format
            if (precision < PRECISION_RANKINGS["minute"]) {
                out.println '00'
            }
        }

        // do minute select
        if (precision >= PRECISION_RANKINGS["minute"]) {
            out.println "'
        }
    }

    Closure renderNoSelectionOption = {noSelectionKey, noSelectionValue, value ->
        renderNoSelectionOptionImpl(out, noSelectionKey, noSelectionValue, value)
    }

    def renderNoSelectionOptionImpl(out, noSelectionKey, noSelectionValue, value) {
        // If a label for the '--Please choose--' first item is supplied, write it out
        out << ""
    }
    
    /**
     * Some attributes can be defined as Boolean values, but the html specification
     * mandates the attribute must have the same value as its name. For example,
     * disabled, readonly and checked.
     */
    @CompileStatic
    private void booleanToAttribute(Map attrs, String attrName) {
        def attrValue = attrs.remove(attrName)
        if (attrValue instanceof CharSequence) {
            attrValue = attrValue.toString().trim()
        }
        // If the value is the same as the name or if it is a boolean value,
        // reintroduce the attribute to the map according to the w3c rules, so it is output later
        if ((attrValue instanceof Boolean && attrValue) ||
            (attrValue instanceof String && (((String)attrValue).equalsIgnoreCase("true") || ((String)attrValue).equalsIgnoreCase(attrName)))) {
            attrs.put(attrName, attrName)
        } else if (attrValue instanceof String && !((String)attrValue).equalsIgnoreCase("false")) {
            // If the value is not the string 'false', then we should just pass it on to
            // keep compatibility with existing code
            attrs.put(attrName, attrValue)
        }
    } 
    private processFormFieldValueIfNecessary(name, value, type) {
        if (requestDataValueProcessor != null) {
            return requestDataValueProcessor.processFormFieldValue(request, name, "${value}", type)
        }
        return value
    }

    
}

你可能感兴趣的:(给Grails 3中的datePicker做个“小手术”)