Spring Boot Dandelion Datatables Thymeleaf搜索日期范围之间

4

我是Dandelion,正在使用Spring Boot和thymeleaf实现datatables。

以下是我用于显示所有日志的表格代码:

<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>

我想在这个表格中添加日期范围过滤器,但使用蒲公英数据表无法实现。有什么方法可以做到这一点?

2个回答

0
在Thymeleaf中,这将看起来像这样:
控制器:
// 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>

我正在寻找解决这个问题的JavaScript方法。抱歉,之前忘记说明了。 - user5672248

0

datatable网站有范例展示范围过滤: 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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接