Skip to content

Commit 62459f9

Browse files
committed
Allow clear/reset date and datetime pickers in form
1 parent 4e1bb08 commit 62459f9

File tree

2 files changed

+72
-34
lines changed

2 files changed

+72
-34
lines changed

main/inc/lib/formvalidator/Element/DatePicker.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,20 @@ public function toHtml()
4444
$value = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
4545
}
4646

47-
return $this->getElementJS() . '
47+
return '
4848
<div class="input-group">
4949
<span class="input-group-addon">
5050
<input ' . $this->_getAttrString($this->_attributes) . '>
5151
</span>
5252
<input class="form-control" type="text" readonly id="' . $id . '_alt" value="' . $value . '">
53+
<span class="input-group-btn">
54+
<button class="btn btn-default" type="button">
55+
<span class="fa fa-times text-danger" aria-hidden="true"></span>
56+
<span class="sr-only">' . get_lang('Reset') . '</span>
57+
</button>
58+
</span>
5359
</div>
54-
';
60+
' . $this->getElementJS();
5561
}
5662

5763
/**
@@ -76,21 +82,34 @@ private function getElementJS()
7682
$js = null;
7783
$id = $this->getAttribute('id');
7884

79-
$js .= "<script>
85+
$js .= "<script>
8086
$(function() {
81-
$('#$id').hide().datepicker({
82-
defaultDate: '" . $this->getValue() . "',
83-
dateFormat: 'yy-mm-dd',
84-
altField: '#{$id}_alt',
85-
altFormat: \"" . get_lang('DateFormatLongNoDayJS') . "\",
86-
showOn: 'both',
87-
buttonImage: '" . Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true) . "',
88-
buttonImageOnly: true,
89-
buttonText: '" . get_lang('SelectDate') . "',
90-
changeMonth: true,
91-
changeYear: true,
92-
yearRange: 'c-60y:c+5y'
93-
});
87+
var txtDate = $('#$id'),
88+
inputGroup = txtDate.parents('.input-group');
89+
90+
txtDate
91+
.hide()
92+
.datepicker({
93+
defaultDate: '" . $this->getValue() . "',
94+
dateFormat: 'yy-mm-dd',
95+
altField: '#{$id}_alt',
96+
altFormat: \"" . get_lang('DateFormatLongNoDayJS') . "\",
97+
showOn: 'both',
98+
buttonImage: '" . Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true) . "',
99+
buttonImageOnly: true,
100+
buttonText: '" . get_lang('SelectDate') . "',
101+
changeMonth: true,
102+
changeYear: true,
103+
yearRange: 'c-60y:c+5y'
104+
});
105+
106+
inputGroup
107+
.find('button')
108+
.on('click', function (e) {
109+
e.preventDefault();
110+
111+
$('#$id, #{$id}_alt').val('');
112+
});
94113
});
95114
</script>";
96115

main/inc/lib/formvalidator/Element/DateTimePicker.php

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,20 @@ public function toHtml()
3737
$value = api_format_date($value, DATE_TIME_FORMAT_LONG_24H);
3838
}
3939

40-
return $this->getElementJS() . '
40+
return '
4141
<div class="input-group">
4242
<span class="input-group-addon">
4343
<input ' . $this->_getAttrString($this->_attributes) . '>
4444
</span>
4545
<input class="form-control" type="text" readonly id="' . $id . '_alt" value="' . $value . '">
46+
<span class="input-group-btn">
47+
<button class="btn btn-default" type="button">
48+
<span class="fa fa-times text-danger" aria-hidden="true"></span>
49+
<span class="sr-only">' . get_lang('Reset') . '</span>
50+
</button>
51+
</span>
4652
</div>
47-
';
53+
' . $this->getElementJS();
4854
}
4955

5056
/**
@@ -71,22 +77,35 @@ private function getElementJS()
7177
//timeFormat: 'hh:mm'
7278
$js .= "<script>
7379
$(function() {
74-
$('#$id').hide().datetimepicker({
75-
defaultDate: '" . $this->getValue() . "',
76-
dateFormat: 'yy-mm-dd',
77-
timeFormat: 'HH:mm',
78-
altField: '#{$id}_alt',
79-
altFormat: \"" . get_lang('DateFormatLongNoDayJS') . "\",
80-
altTimeFormat: \"" . get_lang('TimeFormatNoSecJS') . "\",
81-
altSeparator: \" " . get_lang('AtTime') . " \",
82-
altFieldTimeOnly: false,
83-
showOn: 'both',
84-
buttonImage: '" . Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true) . "',
85-
buttonImageOnly: true,
86-
buttonText: '" . get_lang('SelectDate') . "',
87-
changeMonth: true,
88-
changeYear: true
89-
});
80+
var txtDateTime = $('#$id'),
81+
inputGroup = txtDateTime.parents('.input-group');
82+
83+
txtDateTime
84+
.hide()
85+
.datetimepicker({
86+
defaultDate: '" . $this->getValue() . "',
87+
dateFormat: 'yy-mm-dd',
88+
timeFormat: 'HH:mm',
89+
altField: '#{$id}_alt',
90+
altFormat: \"" . get_lang('DateFormatLongNoDayJS') . "\",
91+
altTimeFormat: \"" . get_lang('TimeFormatNoSecJS') . "\",
92+
altSeparator: \" " . get_lang('AtTime') . " \",
93+
altFieldTimeOnly: false,
94+
showOn: 'both',
95+
buttonImage: '" . Display::return_icon('attendance.png', null, [], ICON_SIZE_TINY, true, true) . "',
96+
buttonImageOnly: true,
97+
buttonText: '" . get_lang('SelectDate') . "',
98+
changeMonth: true,
99+
changeYear: true
100+
});
101+
102+
inputGroup
103+
.find('button')
104+
.on('click', function (e) {
105+
e.preventDefault();
106+
107+
$('#$id, #{$id}_alt').val('');
108+
});
90109
});
91110
</script>";
92111

0 commit comments

Comments
 (0)