tablesorter倒序排序并顯示固定行數(shù)

設了個定時器,每一秒鐘向后臺請求一次數(shù)據(jù)并刷新tablesorter,同時讓tablesorter以第5列數(shù)字倒序來排列,最多顯示10行。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <style>


        .tablesorter thead tr th{
            text-align: center;
        }

        .tablesorter tbody tr td{
            text-align: center;
        }



    </style>

    <title>tablesorter test</title>

    <!--jquery-->
    <script src="js/jquery-2.1.1.min.js"></script>
    <!--tablesorter-->
    <script src="js/jquery.tablesorter.js"></script>
    <link rel="stylesheet" href="css/theme.blue.css">
    <!--bootstrap-->
    <link rel="stylesheet"  integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    <script>

        $(document).ready(function () {

            $(".table2").tablesorter();

            function tableUpdate(data) {
                var html = "<tr><td>" + data.devSerial + "</td>";
                html += "<td>" + data.createTime + "</td>";
                html += "<td>" + data.lastMessageTime + "</td>";
                html += "<td>" + data.batteryLevel + "</td>";
                html += "<td>" + data.signalStrength + "</td>";
                html += "<td>" + JSON.stringify(data.serviceData) + "</td></tr>";

                var row_len = $(".table2")[0].rows.length;

                if (row_len > 10) {
                    $(".table2")[0].deleteRow(row_len - 1)
                }

                // append new html to table body
                $(".table2 tbody").append(html);
                // let the plugin know that we made a update
                $(".table2").trigger("update");
                // set sorting column and direction, this will sort on the first and third column
                var sorting = [[4, 1]];
                // sort on the first column
                $(".table2").trigger("sorton", [sorting]);
                return false;
            }

            var getDataInternal = {
                url: 'http://xxxxxx',
                dataType: 'json',

                success: function (res) {
                    if (res.code == 0) {
                        tableUpdate(res);
                    }
                },
                error: function (res) {
                    //...
                }
            };

            window.setInterval(
                function () {
                    $.ajax(getDataInternal)
                }
                , 1000);
        })
    </script>

</head>

<body>

<div>

    <div id="table-dev">

        <table id="myTable" class="table2 tablesorter tablesorter-blue" cellpadding="1" width="100%">
            <thead>
            <tr>
                <th width="15%">column1</th>
                <th width="10%">column2</th>
                <th width="10%">column3</th>
                <th width="10%">column4</th>
                <th width="10%">column5</th>
                <th width="45%">column6</th>
            </tr>
            </thead>
            <tbody>
            <tr>

            </tr>
            </tbody>
        </table>

    </div>

</div>

</body>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容