0

I am having problems with sorting an JSON/Ajax generated table. The tablesorter i use is this: https://github.com/Mottie/tablesorter and it works fine when I create tables and content directly in HTML.

This is the code on my index.php:

<html>

<head>
<link rel="stylesheet" href="styles.css">
<script type="text/javascript" src="jquery-2.1.3.min.js"></script> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="tablesorter master/js/jquery.tablesorter.js"></script> 
</head>

<body>
include 'purchases.php';
<div id='id01'></div>

<script type="text/javascript" language="javascript">
//$(document).ready(function() { 
    //call the tablesorter plugin 
    $("#myTable").tablesorter();
    $("#myTable").trigger("update");
    } 
); 
</script> 


</body>
</html>

This is my purchases.php:

<script>

var xmlhttp = new XMLHttpRequest();
var url = "purchasesDB.php";

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
    document.getElementById("id01").innerHTML = "";

    var arr = JSON.parse(response);
    var i;
    var out = "<table cellspacing='1' class='sortable' id='myTable'><thead><tr><th>Company</th><th>Amount</th><th>Margin</th><th>Date</th><th>Date 2</th><th>Customer</th><th>Transaction phase</th></tr></thead><tbody>";

    for(i = 0; i < arr.length; i++) {   
    var aTT = arr[i].Id;

  out += "<tr><td><a href='index.php?purch=" + aTT + "&p=Purchased'>" +     

       arr[i].Company + 
        "</a></td><td>" +
         arr[i].Amount +
        "</td><td>" +
         arr[i].Margin +
        "</td><td>" +
         arr[i].Date +
        "</td><td>" +
         arr[i].Date2 +
        "</td><td>" +
         arr[i].Customer +
        "</td><td>" +
        arr[i].TransactionPhase +
        "</td></tr>";

    }
    out += "</tbody></table>";
        //out += "</tbody></table>";



    document.getElementById("id01").innerHTML = out;

}
</script>

The purchasesDB.php is a JSON response and everything works fine except the sorting, which doesn't work at all. What I can see, the potential problems are:

  1. The table isn't assigned the class 'tablesorter'.
  2. The data is not really in the html, only the script. This shows when i watch the page source for index.php where all i can see is the index.php code itself and the included script (but only as the script, not generated html).

I have tried to put the -tags directly in the HTML code on the index page, but this makes no difference.

Anyone got an idea? As you understand I'm pretty new to this.

Thanks a lot!

Peter

5
  • Call tablesorter after the table has been added to the DOM in your myfunction Commented Apr 7, 2015 at 12:00
  • Thanks Rory! Can you please give me a code example of how I should do this? Sorry for my lack of knowlegde. Commented Apr 7, 2015 at 12:05
  • You simply need to more $("#myTable").tablesorter(); to after the innerHTML = out line. Commented Apr 7, 2015 at 12:09
  • @RoryMcCrossan worked perfectly! A million thanks to you! Commented Apr 7, 2015 at 12:45
  • No problem, glad to help. I've added it as an answer for you. Commented Apr 7, 2015 at 13:00

1 Answer 1

1

You need to initialise the tablesorter plugin after the table has been added to the DOM. To do this, move $("#myTable").tablesorter(); to after the innerHTML = out line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.