如何在使用excel-bootstrap-table-filter jQuery插件时对日期进行排序

She*_*ils 5 jquery

我正在使用excel-bootstrap-table-filter jQuery插件来对我网站上的表进行排序.除了排序日期字段外,它的工作方式与梦想类似.我有DD MMM YYYY格式的日期.

excel-bootstrap-table-filter根据DD值对表进行排序.任何人都知道如何让插件正确排序日期?

Nar*_*hav 3

As you are using this plugin so there no implementation of date filter. you need to replace FilterCollection.prototype.sort function with below code

FilterCollection.prototype.sort = function(column, order, table, options) {
    var flip = 1;
    if (order === options.captions.z_to_a.toLowerCase().split(' ').join('-')) flip = -1;
    var tbody = $(table).find('tbody').get(0);
    var rows = $(tbody).find('tr').get();
    var th = $(table).find('th')[column];
    var isType = th.getAttribute('istype');
    var dateformat = th.getAttribute('dateformat');
    rows.sort(function(a, b) {
        var A = a.children[column].innerText.toUpperCase();
        var B = b.children[column].innerText.toUpperCase();
        if (isType == 'date') {
            A = moment(A, dateformat);
            B = moment(B, dateformat);
            return A.diff(B, 'd') * flip;
        } else if (!isNaN(Number(A)) && !isNaN(Number(B))) {
            if (Number(A) < Number(B)) return -1 * flip;
            if (Number(A) > Number(B)) return 1 * flip;
        } else {
            if (A < B) return -1 * flip;
            if (A > B) return 1 * flip;
        }
        return 0;
    });
    for (var i = 0; i < rows.length; i++) {
        tbody.appendChild(rows[i]);
    }
};
Run Code Online (Sandbox Code Playgroud)

In this FIDDLE, I have created a demo using same plugin and I have modify the sort method with I have mentioned above code. Here I have used moment.js. I hope this will help/guide you to achieve your requirement.

Table Code

<table id="table" class="table table-bordered table-intel">
    <thead>
        <tr>
            <th class="filter">Animal</th>
            <th class="filter">Class</th>
            <th class="filter">Collective Noun</th>
            <th dateformat="DD MM YYYY" isType="date" class="filter">Date</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Bear</td>
            <td>Mammal</td>
            <td>Sleuth</td>
            <td>11 04 2018</td>
        </tr>
        <tr>
            <td>Ant</td>
            <td>Insect</td>
            <td>Army</td>
            <td>11 05 2018</td>
        </tr>
        <tr>
            <td>Salamander</td>
            <td>Amphibian</td>
            <td>Congress</td>
            <td>11 04 2018</td>
        </tr>
        <tr>
            <td>Owl</td>
            <td>Bird</td>
            <td>Parliament</td>
            <td>10 04 2018</td>
        </tr>
        <tr>
            <td>Frog</td>
            <td>Amphibian</td>
            <td>Army</td>
            <td>1 04 2018</td>
        </tr>
        <tr>
            <td>Shark</td>
            <td>Fish</td>
            <td>Gam</td>
            <td>11 04 2018</td>
        </tr>
        <tr>
            <td>Kookaburra</td>
            <td>Bird</td>
            <td>Cackle</td>
            <td>21 04 2018</td>
        </tr>
        <tr>
            <td>Crow</td>
            <td>Bird</td>
            <td>Murder</td>
            <td>23 04 2018</td>
        </tr>
        <tr>
            <td>Elephant</td>
            <td>Mammal</td>
            <td>Herd</td>
            <td>11 03 2018</td>
        </tr>
        <tr>
            <td>Barracude</td>
            <td>Fish</td>
            <td>Grist</td>
            <td>30 04 2018</td>
        </tr>
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)