Skip to content

Commit cffad23

Browse files
committed
10_03_HW9_datetimepicker
1 parent 41726ec commit cffad23

6 files changed

Lines changed: 73 additions & 13 deletions

File tree

‎src/main/java/ru/javawebinar/topjava/model/Meal.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.hibernate.annotations.OnDeleteAction;
66
import org.hibernate.validator.constraints.Range;
77
import org.springframework.format.annotation.DateTimeFormat;
8+
import ru.javawebinar.topjava.util.DateTimeUtil;
89

910
import javax.persistence.*;
1011
import javax.validation.constraints.NotBlank;
@@ -33,7 +34,7 @@ public class Meal extends AbstractBaseEntity {
3334

3435
@Column(name = "date_time", nullable = false)
3536
@NotNull
36-
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
37+
@DateTimeFormat(pattern = DateTimeUtil.DATE_TIME_PATTERN)
3738
private LocalDateTime dateTime;
3839

3940
@Column(name = "description", nullable = false)

‎src/main/java/ru/javawebinar/topjava/util/DateTimeUtil.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import java.time.format.DateTimeFormatter;
1010

1111
public class DateTimeUtil {
12-
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
12+
public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm";
13+
public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
1314

1415
// DB doesn't support LocalDate.MIN/MAX
1516
private static final LocalDateTime MIN_DATE = LocalDateTime.of(1, 1, 1, 0, 0);

‎src/main/webapp/WEB-INF/jsp/fragments/headTag.jsp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<link rel="stylesheet" href="webjars/noty/3.1.4/demo/font-awesome/css/font-awesome.min.css">
1313
<link rel="stylesheet" href="webjars/datatables/1.13.5/css/dataTables.bootstrap4.min.css">
1414
<link rel="stylesheet" href="webjars/noty/3.1.4/lib/noty.css"/>
15+
<link rel="stylesheet" href="webjars/datetimepicker/2.5.20-1/jquery.datetimepicker.css">
1516
<link rel="shortcut icon" href="resources/images/icon-meal.png">
1617

1718
<%--http://stackoverflow.com/a/24070373/548473--%>
@@ -20,4 +21,5 @@
2021
<script src="webjars/datatables/1.13.5/js/jquery.dataTables.min.js" defer></script>
2122
<script src="webjars/datatables/1.13.5/js/dataTables.bootstrap4.min.js" defer></script>
2223
<script src="webjars/noty/3.1.4/lib/noty.min.js" defer></script>
24+
<script src="webjars/datetimepicker/2.5.20-1/build/jquery.datetimepicker.full.min.js" defer></script>
2325
</head>

‎src/main/webapp/WEB-INF/jsp/meals.jsp‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717
<div class="card-body pb-0">
1818
<form id="filter">
1919
<div class="row">
20-
<div class="col-3">
20+
<div class="col-2">
2121
<label for="startDate"><spring:message code="meal.startDate"/></label>
22-
<input class="form-control" type="date" name="startDate" id="startDate">
22+
<input class="form-control" name="startDate" id="startDate" autocomplete="off">
2323
</div>
24-
<div class="col-3">
24+
<div class="col-2">
2525
<label for="endDate"><spring:message code="meal.endDate"/></label>
26-
<input class="form-control" type="date" name="endDate" id="endDate">
26+
<input class="form-control" name="endDate" id="endDate" autocomplete="off">
2727
</div>
28-
<div class="offset-2 col-2">
28+
<div class="offset-2 col-3">
2929
<label for="startTime"><spring:message code="meal.startTime"/></label>
30-
<input class="form-control" type="time" name="startTime" id="startTime">
30+
<input class="form-control" name="startTime" id="startTime" autocomplete="off">
3131
</div>
32-
<div class="col-2">
32+
<div class="col-3">
3333
<label for="endTime"><spring:message code="meal.endTime"/></label>
34-
<input class="form-control" type="time" name="endTime" id="endTime">
34+
<input class="form-control" name="endTime" id="endTime" autocomplete="off">
3535
</div>
3636
</div>
3737
</form>
@@ -79,7 +79,7 @@
7979

8080
<div class="form-group">
8181
<label for="dateTime" class="col-form-label"><spring:message code="meal.dateTime"/></label>
82-
<input type="datetime-local" class="form-control" id="dateTime" name="dateTime"
82+
<input class="form-control" id="dateTime" name="dateTime" autocomplete="off"
8383
placeholder="<spring:message code="meal.dateTime"/>">
8484
</div>
8585

‎src/main/webapp/resources/js/topjava.common.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,18 @@ function updateRow(id) {
3333
$("#modalTitle").html(i18n["editTitle"]);
3434
$.get(ctx.ajaxUrl + id, function (data) {
3535
$.each(data, function (key, value) {
36-
form.find(`input[name='${key}']`).val(value);
36+
form.find("input[name='" + key + "']").val(
37+
key === "dateTime" ? formatDate(value) : value
38+
);
3739
});
3840
$('#editRow').modal();
3941
});
4042
}
4143

44+
function formatDate(date) {
45+
return date.replace('T', ' ').substr(0, 16);
46+
}
47+
4248
function deleteRow(id) {
4349
if (confirm(i18n['common.confirm'])) {
4450
$.ajax({

‎src/main/webapp/resources/js/topjava.meals.js‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ $(function () {
2424
"data": "dateTime",
2525
"render": function (date, type, row) {
2626
if (type === 'display') {
27-
return date.replace('T', ' ').substr(0, 16);
27+
return formatDate(date);
2828
}
2929
return date;
3030
}
@@ -56,4 +56,54 @@ $(function () {
5656
$(row).attr("data-meal-excess", data.excess);
5757
}
5858
});
59+
60+
// http://xdsoft.net/jqplugins/datetimepicker/
61+
var startDate = $('#startDate');
62+
var endDate = $('#endDate');
63+
const dateOptions = {
64+
timepicker: false,
65+
format: 'Y-m-d',
66+
formatDate: 'Y-m-d',
67+
};
68+
startDate.datetimepicker({
69+
...dateOptions,
70+
onShow: function (ct) {
71+
this.setOptions({
72+
maxDate: endDate.val() ? endDate.val() : false
73+
})
74+
}
75+
});
76+
endDate.datetimepicker({
77+
...dateOptions,
78+
onShow: function (ct) {
79+
this.setOptions({
80+
minDate: startDate.val() ? startDate.val() : false
81+
})
82+
}
83+
});
84+
85+
var startTime = $('#startTime');
86+
var endTime = $('#endTime');
87+
startTime.datetimepicker({
88+
datepicker: false,
89+
format: 'H:i',
90+
onShow: function (ct) {
91+
this.setOptions({
92+
maxTime: endTime.val() ? endTime.val() : false
93+
})
94+
}
95+
});
96+
endTime.datetimepicker({
97+
datepicker: false,
98+
format: 'H:i',
99+
onShow: function (ct) {
100+
this.setOptions({
101+
minTime: startTime.val() ? startTime.val() : false
102+
})
103+
}
104+
});
105+
106+
$('#dateTime').datetimepicker({
107+
format: 'Y-m-d H:i'
108+
});
59109
});

0 commit comments

Comments
 (0)