`

扩展Grails的datePicker标签

阅读更多

本文所述代码实现基于Grails 1.1 以上版本。

 

---------------------------------------------------------------------------------------------------------------------------

Grails的datePicker标签是一个常用的标签,它有一个属性years,使用这个属性可以指定列表列出的年份,可以是范围,也可以是多选。这个属性在你需要限制选择范围的时候非常有用,但是如果你还要限制月份,日期,小时,分,秒这些时间单位呢? datePicker标签并没有提供相应的属性,那我们只能自己扩展 了。我们希望他有months, days, hours, minuts这些属性,并且和years具有相同的设置方式。

 

修改步骤:

 

首先将代码导入IDE中(省略过程),在目录 src\groovy\org\codehaus\groovy\grails\plugins\web\taglib 下找到FormTagLib.groovy(奇怪为什么是在plugins下的),打开并查找"def datePicker ="下面的内容就是datePicker标签的实现代码,阅读代码可以发现这么一段

 

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

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

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

 

 这段代码就是标签生成年份选择框的,关键的部分是

for (i in years) 

循环生成选择项,它遍历的是years这个变量,往前查找这个变量是这么得到的

def years = attrs['years']

 看到这儿我们就知道了,要实现前面的扩展其实很容易,依葫芦画瓢照样子做就是了。

 

在years后面添加如下代码:

        def months = attrs['months']
        def days = attrs['days']
        def hours = attrs['hours']
        def minutes = attrs['minutes']

 考虑属性不设置为空的情况,需要给变量指定默认值,添加以下代码:

		if (months == null) {
			months = 1..12
		}
		if (days == null) {
			days = 1..31
		}
		if (hours == null) {
			hours = 0..23
		}
		if (minutes == null) {
			minutes = 0..59
		}

 然后修改生成每个时间段选择项的代码,如days:将

for (i in 1..31) {

 修改为

for (i in days) {

 其他都类似,除了月份需要特殊处理,循环时输出时判断,修改如下:

 

            dfs.months.eachWithIndex {m, i ->
                if (m) {
                    def monthIndex = i + 1
                    if(months.contains(monthIndex)){
                        out << "<option value=\"${monthIndex}\""
                        if (month == i) out << " selected=\"selected\""
                        out << '>'
                        out << m
                        out.println '</option>'
                    }
                }
            }
 

 

最后,用ant执行build.xml中的jar,它会编译并打包至dist目录下,其中grails-web-1.1.jar就是扩展后的标签所在的jar,将这个包复制到Grails安装目录的的lib目录下,覆盖原来的jar文件,就可以使用自己扩展的datePicker标签了。

 

--------------------------------------------------------

附件:最后修改的代码如下

 

    /**
    * A simple date picker that renders a date as selects
    * eg. <g:datePicker name="myDate" value="${new Date()}" />
    */
    def datePicker = {attrs ->
        def xdefault = attrs['default']
        if (xdefault == null) {
            xdefault = new Date()
        } else if (xdefault.toString() != 'none') {
            if (xdefault instanceof String) {
                xdefault = DateFormat.getInstance().parse(xdefault)
            }else if(!(xdefault instanceof Date)){
                throwTagError("Tag [datePicker] requires the default date to be a parseable String or a Date")
            }
        } else {
            xdefault = null
        }

        def value = attrs['value']
        if (value.toString() == 'none') {
            value = null
        } else if (!value) {
            value = xdefault
        }
        def name = attrs['name']
        def id = attrs['id'] ? attrs['id'] : name

        def noSelection = attrs['noSelection']
        if (noSelection != null)
        {
            noSelection = noSelection.entrySet().iterator().next()
        }

        def years = attrs['years']
        def months = attrs['months']
        def days = attrs['days']
        def hours = attrs['hours']
        def minutes = attrs['minutes']

        final PRECISION_RANKINGS = ["year": 0, "month": 10, "day": 20, "hour": 30, "minute": 40]
        def precision = (attrs['precision'] ? PRECISION_RANKINGS[attrs['precision']] :
            (grailsApplication.config.grails.tags.datePicker.default.precision ?
                PRECISION_RANKINGS["${grailsApplication.config.grails.tags.datePicker.default.precision}"] :
                PRECISION_RANKINGS["minute"]))

        def day
        def month
        def year
        def hour
        def minute
        def dfs = new java.text.DateFormatSymbols(RCU.getLocale(request))

        def c = null
        if (value instanceof Calendar) {
            c = value
        }
        else if (value != null) {
            c = new GregorianCalendar();
            c.setTime(value)
        }

        if (c != null) {
            day = c.get(GregorianCalendar.DAY_OF_MONTH)
            month = c.get(GregorianCalendar.MONTH)
            year = c.get(GregorianCalendar.YEAR)
            hour = c.get(GregorianCalendar.HOUR_OF_DAY)
            minute = c.get(GregorianCalendar.MINUTE)
        }

        if (years == null) {
            def tempyear
            if (year == null) {
                // If no year, we need to get current year to setup a default range... ugly
                def tempc = new GregorianCalendar()
                tempc.setTime(new Date())
                tempyear = tempc.get(GregorianCalendar.YEAR)
            } else {
                tempyear = year
            }
            years = (tempyear - 100)..(tempyear + 100)
        }
        if (months == null) {
            months = 1..12
        }
        if (days == null) {
            days = 1..31
        }
        if (hours == null) {
            hours = 0..23
        }
        if (minutes == null) {
            minutes = 0..59
        }


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

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

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

            for (i in days) {
                out.println "<option value=\"${i}\""
                if (i == day) {
                    out.println " selected=\"selected\""
                }
                out.println ">${i}</option>"
            }
            out.println '</select>'
        }

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

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

            dfs.months.eachWithIndex {m, i ->
                if (m) {
                    def monthIndex = i + 1
                    if(months.contains(monthIndex)){
                        out << "<option value=\"${monthIndex}\""
                        if (month == i) out << " selected=\"selected\""
                        out << '>'
                        out << m
                        out.println '</option>'
                    }
                }
            }
            out.println '</select>'
        }

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

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

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

        // do hour select
        if (precision >= PRECISION_RANKINGS["hour"]) {
            out.println "<select name=\"${name}_hour\" id=\"${id}_hour\">"

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

            for (i in hours) {
                def h = '' + i
                if (i < 10) h = '0' + h
                out << "<option value=\"${h}\" "
                if (hour == h.toInteger()) out << "selected=\"selected\""
                out << '>' << h << '</option>'
                out.println()
            }
            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\">"

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

            for (i in minutes) {
                def m = '' + i
                if (i < 10) m = '0' + m
                out << "<option value=\"${m}\" "
                if (minute == m.toInteger()) out << "selected=\"selected\""
                out << '>' << m << '</option>'
                out.println()
            }
            out.println '</select>'
        }
    }
 

 

 

 

 

3
0
分享到:
评论

相关推荐

    Grails标签

    Grails标签 主要介绍了grails的标签的一个帮助文档

    Grails Grails Grails

    Grails Grails Grails Grails Grails

    Grails权威指南

     8.3 grails动态标签  8.3.1 链接标签  8.3.2 创建表单和字段  8.3.3 验证和错误处理  8.4 国际化支持  8.4.1 标签  8.5 使用布局和模板  8.5.1 布局演示  8.5.2 按照惯例布局 ...

    Grails权威指南 Grails权威指南

    Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南

    Eclipse下搭建Grails项目

    Grails项目的应用越来越多,而对于初学者来说,在Eclipse下搭建Grails项目是一个难题,这个文档将教会你如何搭建Grails项目,希望对你有所帮助。

    Grails入门指南 -- 针对grails1.0.4更新

    Grails入门指南中文pdf -- 针对grails1.0.4更新,附加idea8 开发grails的流程

    Groovy轻松入门——Grails实战基础篇

    在学习任何东西之前,最重要的是培养兴趣,Groovy世界最耀眼的技术之一--Grails相信大家早已耳闻,我将通过Grails实战系列文章 向您展现Grails的迷人风采,使您感受到Grails的魅力,以至疯狂地爱上Grails,并坠入...

    Grails1.1中文文档

    Grails1.1中文文档

    grails+Xfire webservice

    grails+Xfire webservice

    grails开发环境配置及应用开发

    详细讲解grails开发环境配置。 详细讲解grails连接mysql数据库,crud开发

    grails

    grails-2.1.zip.001

    grails 1.0.4

    Grails专为下一代JavaWeb应用程序而设计的框架,其借助于Groovy动态语言,使Web开发变得简单而方便。Grails尽量为更多现有的Java项目创建一个全面的框架(不仅局限于视图处理),这和当前一些Java框架提供给用户的一...

    Grails中文参考手册

    Grails 中文 参考手册

    Grails1.3.7参考手册

    Grails 1.3.7英文版官方参考手册,学习Grails的权威指南

    grails入门经典

    grails grails入门经典 grails入门 grails例子 grails资料 通过自学一点点积累起来的,相信对你有帮助的。

    grails 中文文档+grails-fckeditor-0.9.5.zip插件

    grails 中文文档+grails-fckeditor-0.9.5.zip插件

    grails框架

    grails的插件系统也是其亮点之一。首先,和rails,django等web框架类似,基于微内核的思想,插件(可重用模块)是框架的一等公民。grails除了核心模块以外的功能几乎都是通过插件方式实现的。实际上,一个grails插件...

    grails3.2.8-01

    grails3.2.8 part1

Global site tag (gtag.js) - Google Analytics