我是带有Spring Boot和thymeleaf的蒲公英数据表。
这是我想要显示所有日志的表的代码。
<table class="table table-bordered" id="expiredUsersTable" dt:table="true">
<thead>
<tr>
<th dt:sortInitDirection="desc">TIME</th>
<th dt:filterable="true" dt:filterType="select">Log Level</th>
<th>Message</th>
</tr>
</thead>
<tbody>
<tr th:each="log : ${logs}">
<td th:text="${log?.getFormattedDate()}"></td>
<td th:text="${log?.level}"></td>
<td th:text="${log?.message}"></td>
</tr>
</tbody>
</table>
我想在此表的日期范围之间添加过滤器,但我无法使用蒲公英数据表实现此目的。有什么方法可以做到这一点?
答案 0 :(得分:0)
在百里香中,这看起来像这样:
控制器:
// Create these dates however you want, these example dates are filtering between 1950 and 1960.
GregorianCalendar gc = new GregorianCalendar();
gc.set(Calendar.YEAR, 1950);
model.put("start", gc.getTime());
gc.set(Calendar.YEAR, 1960);
model.put("end", gc.getTime());
Thymeleaf:
<table class="table table-bordered" id="expiredUsersTable" dt:table="true">
<thead>
<tr>
<th dt:sortInitDirection="desc">TIME</th>
<th dt:filterable="true" dt:filterType="select">Log Level</th>
<th>Message</th>
</tr>
</thead>
<tbody>
<tr th:each="log : ${logs}" th:unless="${log.date.before(start) OR log.date.after(end)}">
<td th:text="${log?.formattedDate}"></td>
<td th:text="${log?.level}"></td>
<td th:text="${log?.message}"></td>
</tr>
</tbody>
</table>
答案 1 :(得分:0)
数据表站点包含范围过滤的示例:https://datatables.net/examples/plug-ins/range_filtering.html
我不得不猜测你正在使用的日期的格式(这个例子是使用dd-mm-yyyy),这样的事情对我有用:
HTML:
<body onload="initFilter();">
From <input type="text" id="start" /> to <input type="text" id="end" />
JavaScript的:
<script>
// <![CDATA[
function parseDate(date) {
if (date.length < 10)
return false;
var parts = date.split("-");
var d = parseInt(parts[0]);
var m = parseInt(parts[1]) - 1;
var y = parseInt(parts[2]);
return new Date(y, m, d);
}
function initFilter() {
$.fn.dataTable.ext.search.push(
function(settings, data, dataIndex) {
var start = parseDate($('#start').val());
var end = parseDate($('#end').val());
var data = parseDate(data[0]);
var valid = true;
valid = valid && (!start || (start.getTime() =< data.getTime()));
valid = valid && (!end || (end.getTime() > data.getTime()));
return valid;
}
);
$('#start, #end').keyup( function() {
oTable_expiredUsersTable.draw();
});
}
// ]]>
</script>