我在Grails项目中使用joda-time-plugin
。在视图中,我通过timepicker设置时间(LocalTime)。不幸的是,始终会显示秒数。精确选项只是使秒不可编辑,因此它们被显示为"硬编码"。
我也尝试在config.groovy中更改Timeformat,但我不允许删除格式模式中的秒数。
这是我认为的代码:
<div class="fieldcontain ${hasErrors(bean: dailyBookingInstance, field: 'start', 'error')} required">
<label for="start">
<g:message code="dailyBooking.start.label" default="Start" />
<span class="required-indicator">*</span>
</label>
<joda:timePicker name="start" value="${dailyBookingInstance?.start}" ></joda:timePicker>
</div>
这是我的config.groovy中的部分,我试图改变:
jodatime {
format.org.joda.time.DateTime = "dd.MM.yyyy HH:mm:ss"
format.org.joda.time.LocalDate = "dd.MM.yyyy"
format.org.joda.time.LocalTime = "HH:mm:ss"
}
答案 0 :(得分:0)
我查看了插件代码,这是不可能的,因为它在插件标记库中是硬编码的。
要实现这一点,您需要从插件标记库创建自己的标记库范围,并删除该硬编码的第二部分。
我试过这个,以下是我的代码,看看 -
某种观点
<joda:timePickerNoSec name="end" value="" default="none" noSelection="['': '']"/>
<强> TestTagLib.groovy 强>
class TestTagLib extends DateTimeTagLib {
def timePickerNoSec = { attrs ->
log.debug '***** joda:timePickerNoSec *****'
def fields = [DateTimeFieldType.hourOfDay(), DateTimeFieldType.minuteOfHour()]
myRenderPicker(fields, attrs)
}
def myRenderPicker = { List fields, attrs ->
def precision = attrs.precision ?: (grailsApplication.config.grails.tags.datePicker.default.precision ?: 'minute')
log.debug "precision = $precision"
log.debug "fields = $fields"
switch (precision) {
case 'year': fields.remove(DateTimeFieldType.monthOfYear())
case 'month': fields.remove(DateTimeFieldType.dayOfMonth())
case 'day': fields.remove(DateTimeFieldType.hourOfDay())
case 'hour': fields.remove(DateTimeFieldType.minuteOfHour())
case 'minute': fields.remove(DateTimeFieldType.secondOfMinute())
}
log.debug "fields = $fields"
def defaultValue = attrs.'default'
if (!defaultValue) {
defaultValue = new DateTime()
} else if (defaultValue == 'none') {
defaultValue = null
} else if (defaultValue instanceof String) {
defaultValue = getParser(fields).parseDateTime(defaultValue)
} else if (!(defaultValue instanceof ReadableInstant) && !(defaultValue instanceof ReadablePartial)) {
throwTagError("Tag [datePicker] requires the default date to be a parseable String or instanceof ReadableInstant or ReadablePartial")
}
log.debug "default = $defaultValue"
def value = attrs.value
if (value == 'none') {
value = null
} else if (!value) {
value = defaultValue
}
log.debug "value = $value"
def name = attrs.name
def id = attrs.id ?: name
def noSelection = attrs.noSelection
if (noSelection) {
noSelection = noSelection.entrySet().iterator().next()
}
def years = attrs.years
def dfs = new DateFormatSymbols(RequestContextUtils.getLocale(request))
if (!years) {
def tempyear = null
if (value && value?.isSupported(DateTimeFieldType.year())) tempyear = value.year
else tempyear = new LocalDate().year
years = (tempyear - (grailsApplication.config.grails.tags.datePicker.default.yearsBelow ?: 100))..(tempyear + (grailsApplication.config.grails.tags.datePicker.default.yearsAbove ?: 100))
}
log.debug "starting rendering"
out << "<input type=\"hidden\" name=\"$name\" value=\"struct\" />"
// create day select
if (fields.contains(DateTimeFieldType.dayOfMonth())) {
log.debug "rendering day"
out.println "<select name=\"${name}_day\" id=\"${id}_day\">"
if (noSelection) {
renderNoSelectionOption(noSelection.key, noSelection.value, value?.dayOfMonth)
out.println()
}
for (i in 1..31) {
out << "<option value=\"${i}\""
if (i == value?.dayOfMonth) {
out << " selected=\"selected\""
}
out.println ">${i}</option>"
}
out.println '</select>'
}
// create month select
if (fields.contains(DateTimeFieldType.monthOfYear())) {
log.debug "rendering month"
out.println "<select name=\"${name}_month\" id=\"${id}_month\">"
if (noSelection) {
renderNoSelectionOption(noSelection.key, noSelection.value, value?.monthOfYear)
out.println()
}
dfs.months.eachWithIndex { m, i ->
if (m) {
def monthIndex = i + 1
out << "<option value=\"${monthIndex}\""
if (monthIndex == value?.monthOfYear) out << " selected=\"selected\""
out << '>'
out << m
out.println '</option>'
}
}
out.println '</select>'
}
// create year select
if (fields.contains(DateTimeFieldType.year())) {
log.debug "rendering year"
out.println "<select name=\"${name}_year\" id=\"${id}_year\">"
if (noSelection) {
renderNoSelectionOption(noSelection.key, noSelection.value, value?.year)
out.println()
}
for (i in years) {
out << "<option value=\"${i}\""
if (i == value?.year) {
out << " selected=\"selected\""
}
out.println ">${i}</option>"
}
out.println '</select>'
}
// do hour select
if (fields.contains(DateTimeFieldType.hourOfDay())) {
log.debug "rendering hour"
out.println "<select name=\"${name}_hour\" id=\"${id}_hour\">"
if (noSelection) {
renderNoSelectionOption(noSelection.key, noSelection.value, value?.hourOfDay)
out.println()
}
for (i in 0..23) {
def h = i.toString().padLeft(2, '0')
out << "<option value=\"${h}\""
if (value?.hourOfDay == i) 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 and seconds as 00 in read-only format
if (!fields.contains(DateTimeFieldType.minuteOfHour())) {
out.println '00:00'
}
}
// do minute select
if (fields.contains(DateTimeFieldType.minuteOfHour())) {
log.debug "rendering minute"
out.println "<select name=\"${name}_minute\" id=\"${id}_minute\">"
if (noSelection) {
renderNoSelectionOption(noSelection.key, noSelection.value, value?.minuteOfHour)
out.println()
}
for (i in 0..59) {
def m = i.toString().padLeft(2, '0')
out << "<option value=\"${m}\""
if (value?.minuteOfHour == i) out << " selected=\"selected\""
out << '>' << m << '</option>'
out.println()
}
// out.println '</select> :'
out.println '</select>'
// If we're rendering the minutes, but not the seconds, then display the seconds as 00 in read-only format
// if (!fields.contains(DateTimeFieldType.secondOfMinute())) {
// out.println '00'
// }
}
// do second select
if (fields.contains(DateTimeFieldType.secondOfMinute())) {
log.debug "rendering second"
out.println "<select name=\"${name}_second\" id=\"${id}_second\">"
if (noSelection) {
renderNoSelectionOption(noSelection.key, noSelection.value, value?.secondOfMinute)
out.println()
}
for (i in 0..59) {
def s = i.toString().padLeft(2, '0')
out << "<option value=\"${s}\""
if (value?.secondOfMinute == i) out << " selected=\"selected\""
out << '>' << s << '</option>'
out.println()
}
out.println '</select>'
}
// do zone select
if (attrs.useZone == "true") {
out << dateTimeZoneSelect(name: "${name}_zone")
}
log.debug "done"
}
}
希望这有助于...... ,.