`
MafiaDada
  • 浏览: 25086 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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

import java.text.DateFormatSymbols
import java.text.DateFormat
import org.springframework.web.servlet.support.RequestContextUtils as RCU


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\" />"

        // create year select
        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>'
        }

        // create month select
        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"' : ''}>${monthIndex}月</option>"
                }
            }
            out.println '</select>'
        }

        // create day select
        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 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>"
    }
}

注意加上红色部分代码,否则会报错。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics