1

I am working in a website in which page is displaying data in table when some option is selected. The page is creating and displaying data in a table dynamically by converting the json output from AJAX call to html table output. I need to sort individual columns of the table according to alphabetically/date wise/open-close when subsequent table header is clicked.

my code is something like this:-

if (json.d[0].length > 0){

    htmlTable += "<table class='table table-striped' id='myTable'>"+
    "<tr>"+
    "<th  style='background-color: #df820a;'>Customer</button></th>"+
    "<th style='background-color: #df820a;'>Survey date</th>"+
    "<th style='background-color: #df820a;'>Contact by</th>"+
    "<th style='background-color: #df820a;'>Assigned to</th>"+
    "<th style='background-color: #df820a;'>Status</th>"+
    "<th style='background-color: #df820a;'>Resolution</th>"+
    "<th style='background-color: #df820a;'>Coupon offered</th>"+
    "</tr>";

    for (var i = 0; i < json.d[0].length; i++) {
        htmlTable += "<tr>"+
        "<td>" + json.d[0][i].customerName1 + "</td>"+
        "<td>" + json.d[0][i].createdDt1 + "</td>"+
        "<td>" + json.d[0][i].customerContactBy1 + "</td>"+
        "<td>" + json.d[0][i].AssignedTo1 + "</td>"+
        "<td>" + json.d[0][i].alertStatus1 + "</td>"+
        "<td>" + json.d[0][i].resolution1 + "</td>"+
        "<td>" + json.d[0][i].couponOffered1 + "</td>"+;
        "</tr>";
    }
    htmlTable += "</table>";
    $('#AlertManagement').html(htmlTable);
}

Is there any way by which I can sort these dynamically generated columns???

1
  • A table in HTML sense is not like a DataTable in programming sense, all it does is provides a layout, you will have to do any sorting by storing your data in arrays and repopulating the table after each 'sort'. Commented Aug 4, 2014 at 11:51

1 Answer 1

2

I use Christian Bach's tablesorter plugin. It works really well with ajax calls. It's as easy as this:

$.post('script.php', formData, function(data){
    $("#myTable").html(data).tablesorter(); 
});

Of course I understand you are using json so it will be a little different than this, but I think you see where this is going.

http://tablesorter.com/docs/#Demo

Hope this helps!

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

1 Comment

Did this help with your issue? Did you have any further questions? Just a follow up as I see there is no selected answer. :)

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.