grails taglib修改datePicker格式

grails的datePicker标签很好,但是并不能设置成yyyy-MM-dd的格式,另外,在月份选择列表中,显示的是中文的:一月、二月等等,对于追求简捷的中国人来说,这不符合我们的习惯。怎么办?

在grails英文指南页面中,显示了它的源代码位置在:/grails/src/groovy/org/codehaus/groovy/grails/plugins/web/taglib/FormTagLib.groovy
因此,在www.grails.org把源代码下载下来,按照路径找到这个FormTagLib.groovy文件,把里面关于datePicker的一节复制下来,在自己所做项目的taglib目录下建立一个tag文件MyTagLig.Groovy,把代码拷贝进去,稍作修改。

1、把年、月、日的代码按顺序替换一下;
2、在月份的实现部分替换掉。

package com.mycompany

[b][color=red]import java.text.DateFormatSymbols
import java.text.DateFormat
import org.springframework.web.servlet.support.RequestContextUtils as RCU[/color][/b]

class MyTagLib {

static namespace = "my"

def datePicker = { attrs ->
def out = out // let x = x ?
def xdefault = attrs['default']
if (xdefault == null) {
xdefault = new Date()
}

...........................................
...........................................

out.println "<input type=\"hidden\" name=\"${name}\" value=\"date.struct\" />"

[b][color=green]// create year select[/color][/b]
if (precision >= PRECISION_RANKINGS["year"]) {
out.println "<select name=\"${name}_year\" id=\"${id}_year\">"

if (noSelection) {
renderNoSelectionOptionImpl(out, noSelection.key, noSelection.value, '')
out.println()
}

for (i in years) {
out.println "<option value=\"${i}\"${i == year ? ' selected="selected"' : ''}>${i}</option>"
}
out.println '</select>'
}

[b][color=green]// create month select[/color][/b]
if (precision >= PRECISION_RANKINGS["month"]) {
out.println "<select name=\"${name}_month\" id=\"${id}_month\">"

if (noSelection) {
renderNoSelectionOptionImpl(out, noSelection.key, noSelection.value, '')
out.println()
}

dfs.months.eachWithIndex {m, i ->
if (m) {
def monthIndex = i + 1
out.println "<option value=\"${monthIndex}\"${i == month ? ' selected="selected"' : ''}>[b][color=green]${monthIndex}月[/color][/b]</option>"
}
}
out.println '</select>'
}

[b][color=green]// create day select[/color][/b]
if (precision >= PRECISION_RANKINGS["day"]) {
out.println "<select name=\"${name}_day\" id=\"${id}_day\">"

if (noSelection) {
renderNoSelectionOptionImpl(out, noSelection.key, noSelection.value, '')
out.println()
}

for (i in 1..31) {
out.println "<option value=\"${i}\"${i == day ? ' selected="selected"' : ''}>${i}</option>"
}
out.println '</select>'
}

...........................................
...........................................

}
}

def [b][color=red]renderNoSelectionOption [/color][/b]= {noSelectionKey, noSelectionValue, value ->
renderNoSelectionOptionImpl(out, noSelectionKey, noSelectionValue, value)
}

def [b][color=red]renderNoSelectionOptionImpl[/color][/b](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>"
}
}

注意加上红色部分代码,否则会报错。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值