为什么Ajax响应不排序的方式方式、Ajax

2023-09-10 22:18:46 作者:誰萆嶶ㄋ承諾

我使用 sorttable.js 为表排序和我的表在每3秒通过Ajax响应更新,但反应不是按排序的方式,因为我希望它可以。

I am using sorttable.jsfor table sorting and my table is updated in every 3 sec by ajax response but the response is not in sorted manner as i expect it to be.

索引页

 <div id="resDiv">
            <table id="myTable1" class="sortable">
            <thead>
                <tr><th id="person">Person</th><th id="monpay">Monthly pay</th></tr>
            </thead>
            <tbody>
                <tr><td>Jan Molby</td><td>£12,000</td></tr>
                <tr><td>Steve Nicol</td><td>£8,500</td></tr>
                <tr><td>Steve McMahon</td><td>£9,200</td></tr>
                <tr><td>John Barnes</td><td>£15,300</td></tr>
            </tbody>
            <tfoot>
                <tr><td>TOTAL</td><td>£45,000</td></tr>
            </tfoot>
        </table>
        </div>
         <a href="#" id="ajax-append">Append new table data</a>

Ajax响应是:

<table id="myTable" class="sortable">
    <thead>
        <tr><th>Person</th><th>Monthly pay</th></tr>
    </thead>
    <tbody>
        <tr><td>prabha Molby</td><td>£12,000</td></tr>
        <tr><td>abcd Nicol</td><td>£8,500</td></tr>
        <tr><td>steev McMahon</td><td>£9,200</td></tr>
        <tr><td>John Barnes</td><td>£15,300</td></tr>
    </tbody>
    <tfoot>
        <tr><td>TOTAL</td><td>£55,000</td></tr>
    </tfoot>
</table>

的JavaScript

 $(function() {

                $("#ajax-append").click(function() {
                    setInterval(function() {
                    var request = $.get("assets/replacecontent.jsp", function(html) {
                        alert(html);
                        $('#resDiv').html(html);
                        var newTableObject = document.getElementById("myTable");
                        alert(newTableObject);
                        sorttable.makeSortable(newTableObject);

//                        alert($("#myTable").length);

                    });
                }, 3000);

                });
            });

现在如果有时间我有点Ajax响应它得到排序却接连响应再次改变它的订单,但我希望它分类为previous之一。

Now if any time i sort the ajax response it get sorted but after another response it again change it's order but i want it sorted as previous one.

推荐答案

我觉得你应该读什么sorttable.js常见问题解答说:

I think you should have read what the sorttable.js faq says:

当页面加载排序表

很多人问,如何让我的SortTable排序表中的第一   时间在页面加载答案是:你不的SortTable左右。   改变供应时间是从您的服务器没有一个网页的HTML   刷新。当页面首先从服务器提供服务,你必须   承担为它无论如何送达期待。所以,如果你想表   排序时,首先显示的页面,服务表中排序   订购。表经常会从数据库中;获得从数据   数据库中的排序顺序与ORDER BY子句在您的SQL。任何   溶液这涉及在运行的SortTable只要页   负载(即没有用户输入)是一个错误的解决方案。

Lots of people ask, "how do I make sorttable sort the table the first time the page is loaded?" The answer is: you don't. Sorttable is about changing the HTML that is served from your server without a page refresh. When the page is first served from the server, you have to incur the wait for it to be served anyway. So, if you want the table sorted when a page is first displayed, serve the table in sorted order. Tables often come out of a database; get the data from the database in a sorted order with an ORDER BY clause in your SQL. Any solution which involves you running sorttable as soon as the page loads (i.e., without user input) is a wrong solution.

但是,他们还规定对于一个解决方案:

But they also state a solution for that:

//Find the TH you want to use, maybe you can store that using an event handler before
var myTH = document.getElementsByTagName("th")[0];
//Then sort it
sorttable.innerSortFunction.apply(myTH, []);

不过来,你必须找到你的用户之前单击该列,并说实话我还没有找到直接使用API​​的SortTable任何方式。也许使用某种类型的单击事件处理程序和存储被点击最后日。

But to that you will have to find the column your user clicked on before, and to be honest I have not found any way using the sorttable api directly. Maybe use some kind of click event handler and store the th that was clicked last.