jQuery tablesorter - loss of functionality after AJAX call

后端 未结 8 985
庸人自扰
庸人自扰 2021-02-14 06:00

I have recently been experimenting with the tablesorter plugin for jQuery. I have successfully got it up and running in once instance, and am very impressed. However, I have tri

相关标签:
8条回答
  • 2021-02-14 06:35

    It may be that as your second table is created with ajax that you need to rebind the events. You may want to use the LiveQuery plugin

    http://docs.jquery.com/Plugins/livequery

    which might "auto-magically" help with your problem.

    edit: sorry, just reread your post and seen that you've tried that already.


    Update. I've rigged up a quick test harness which hopefully will help. There are 3 LIs at the top each one has a different way of updating the table contents. The last one updates the contents and keeps the ordering

        <script src="jquery-1.3.js"  type="text/javascript" ></script>
        <script src="jquery.livequery.js"  type="text/javascript" ></script>
        <script src="jquery.tablesorter.min.js" type="text/javascript" ></script>
    
    <script>
    
    var newTableContents = "<thead><tr><th>Last Name</th><th>First Name</th>
    <th>Email</th><th>Due</th><th>Web Site</th></tr></thead>
    <tbody><tr><td>Smith</td><td>John</td><td>jsmith@gmail.com</td><td>$50.00</td>
        <td>http://www.jsmith.com</td></tr><tr><td>Bach</td><td>Frank</td><td>fbach@yahoo.com</td>
    <td>$50.00</td><td>http://www.frank.com</td></tr></tbody>";
    
    
     $(document).ready(function() 
        { 
            $("#addData").click(function(event){
            $("#sortableTable").html(newTableContents);
        });
    
        $("#addLivequery").livequery("click", function(event){
            $("#sortableTable").html(newTableContents);
        });
    
        $("#addLiveTable").livequery("click", function(event){
            $("#sortableTable").html(newTableContents);
            $("#sortableTable").tablesorter( { } ); 
        });
    
        $("#sortableTable").tablesorter( { } ); 
      });
    </script>
    
            <ul>
                <li id="addData" style="background-color:#ffcc99;display:inline;">Update Table</li>
                <li id="addLivequery" style="background-color:#99ccff;display:inline;">Update Table with livequery</li>
                <li id="addLiveTable" style="background-color:#99cc99;display:inline;">Update Table with livequery & tablesorter</li> 
            </ul>
    
            <hr />
            <table id="sortableTable"> 
            <thead> 
            <tr> 
                <th>Last Name</th> 
                <th>First Name</th> 
                <th>Email</th> 
                <th>Due</th> 
                <th>Web Site</th> 
            </tr> 
            </thead> 
            <tbody> 
            <tr> 
                <td>Jones</td> 
                <td>Joe</td> 
                <td>jjones@gmail.com</td> 
                <td>$100.00</td> 
                <td>http://www.jjones.com</td> 
            </tr> 
            <tr> 
                <td>French</td> 
                <td>Guy</td> 
                <td>gf@yahoo.com</td> 
                <td>$50.00</td> 
                <td>http://www.french.com</td> 
            </tr> 
            </tbody> 
            </table> 
    
    0 讨论(0)
  • 2021-02-14 06:37

    Have you tried calling

    $("#myTable").tablesorter();
    

    after the code where you handle the click on tab and repopulate the table??? If not, just give it a try.

    0 讨论(0)
提交回复
热议问题