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 "<input type=\"hidden\" name=\"${name}\" value=\"${dateStructValue}\" />"
// 创建“年”选择
if (precision >= PRECISION_RANKINGS["year"]) {
out.println "<select name=\"${name}_year\" id=\"${id}_year\" aria-labelledby=\"${name}\""
if (attrs.disabled) {
out << ' disabled="disabled"'
}
if (attrs.readonly) {
out << ' readonly="readonly"'
}
out << '>'
if (noSelection) {
renderNoSelectionOptionImpl(out, noSelection.key, noSelection.value, '')
out.println()
}
for (i in years) {
// Change this year option to use requestDataValueProcessor
def yearIndex = processFormFieldValueIfNecessary("${name}_year","${i}","option")
out.println "<option value=\"${yearIndex}\"${i == year ? ' selected="selected"' : ''}>${i}年</option>"
}
out.println '</select>'
}
// create month select
if (precision >= PRECISION_RANKINGS["month"]) {
out.println "<select name=\"${name}_month\" id=\"${id}_month\" aria-labelledby=\"${name}\""
if (attrs.disabled) {
out << ' disabled="disabled"'
}
if (attrs.readonly) {
out << ' readonly="readonly"'
}
out << '>'
if (noSelection) {
renderNoSelectionOptionImpl(out, noSelection.key, noSelection.value, '')
out.println()
}
dfs.months.eachWithIndex {m, i ->
if (m) {
def monthIndex = i + 1
monthIndex = processFormFieldValueIfNecessary("${name}_month","${monthIndex}","option")
out.println "<option value=\"${monthIndex}\"${i == month ? ' selected="selected"' : ''}>${monthIndex}月</option>"
}
}
out.println '</select>'
}
// create day select
if (precision >= PRECISION_RANKINGS["day"]) {
out.println "<select name=\"${name}_day\" id=\"${id}_day\" aria-labelledby=\"${name}\""
if (attrs.disabled) {
out << ' disabled="disabled"'
}
if (attrs.readonly) {
out << ' readonly="readonly"'
}
out << '>'
if (noSelection) {
renderNoSelectionOptionImpl(out, noSelection.key, noSelection.value, '')
out.println()
}
for (i in 1..31) {
// Change this option to use requestDataValueProcessor
def dayIndex = processFormFieldValueIfNecessary("${name}_day","${i}","option")
out.println "<option value=\"${dayIndex}\"${i == day ? ' selected="selected"' : ''}>${i}日</option>"
}
out.println '</select>'
}
// do hour select
if (precision >= PRECISION_RANKINGS["hour"]) {
out.println "<select name=\"${name}_hour\" id=\"${id}_hour\" aria-labelledby=\"${name}\""
if (attrs.disabled) {
out << ' disabled="disabled"'
}
if (attrs.readonly) {
out << ' readonly="readonly"'
}
out << '>'
if (noSelection) {
renderNoSelectionOptionImpl(out, noSelection.key, noSelection.value, '')
out.println()
}
for (i in 0..23) {
def h = '' + i
if (i < 10) h = '0' + h
// This option add hour to requestDataValueProcessor
h = processFormFieldValueIfNecessary("${name}_hour","${h}","option")
out.println "<option value=\"${h}\"${i == hour ? ' selected="selected"' : ''}>$h</option>"
}
out.println '</select> :'
// 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 "<select name=\"${name}_minute\" id=\"${id}_minute\" aria-labelledby=\"${name}\""
if (attrs.disabled) {
out << 'disabled="disabled"'
}
if (attrs.readonly) {
out << 'readonly="readonly"'
}
out << '>'
if (noSelection) {
renderNoSelectionOptionImpl(out, noSelection.key, noSelection.value, '')
out.println()
}
for (i in 0..59) {
def m = '' + i
if (i < 10) m = '0' + m
m = processFormFieldValueIfNecessary("${name}_minute","${m}","option")
out.println "<option value=\"${m}\"${i == minute ? ' selected="selected"' : ''}>$m</option>"
}
out.println '</select>'
}
}
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 << "<option value=\"${(noSelectionKey == null ? '' : noSelectionKey)}\"${noSelectionKey == value ? ' selected="selected"' : ''}>${noSelectionValue.encodeAsHTML()}</option>"
}
/**
* 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
}
}
网友评论