I have created HTML TABLE from JSON data of PHP using JQuery.
After creating table, I try to attach table sorter (JQuery)
but, it doesn’t work. How to solve? Code below:
<script type="text/javascript">
$(document).ready(function() {
$.getJSON('listnotice.php', function(data) {
var table = "";
$.each(data, function(index,entry) {
table += '<tr>';
table += '<td>' +entry["title"] + '</td>';
table += '<td>' +entry["content"] + '</td>';
table += '<td>' +entry["date"] + '</td>';
table += '</tr>';
});
table += "</tbody>";
$("#noticeList").append(table);
});
$("#noticeList").tablesorter({debug: false, widgets: ['zebra'], sortList: [[0,0]]}).tablesorterFilter({filterContainer: $("#filter-box"),
filterClearContainer: $("#filter-clear-button"),
filterColumns: [0],
filterCaseSensitive: false});
});
</script>
</head>
<body>
<table id="noticeList">
<thead><tr><th>1</th><th>2</th><th>3</th></tr></thead><tbody>
</tablev>
</body>
</html>
You need to apply the tablesorter in the same callback where you are building the table. The getJSON call is asynchronous and the way you have it now, you’re applying tablesorter before the call returns and the table is built.