I have a table that I want to make sortable. The problem is that this table has been loaded from the external file (inc.php) via some JS (filter-data.js) function. To be more precisely, I have a main.php page with the Submit button. When I click on it, that triggers some JS code which calls inc.php file to populate my table with its data on demand from MySQL base and then puts them both (table + data) back to the main page:
This is the table placeholder on the main page.
<div id="tableData"></div>
This is the submit button on the main page:
<input type="submit" onclick="genTable()" value="Populate users data">
This is what I am geting form the table.inc.php page:
<table id="my-table">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>45</td>
<td>34</td>
</tr>
<tr>
<td>23</td>
<td>17</td>
</tr>
</tbody>
</table>
I am not sure how and where to call TS function - should it be on the main.php page or table.inc.php page? I tried almost everything but with no success.
$("#my-table").tablesorter();
If I skip JS and just require my table.inc.php file from main.php page, it works properly.
Thank you!
It is better practice to separate the HTML from scripting, so to do this, add this to your page (or even better, move the stuff inside the script tag into an external file):
Then modify your submit button code by removing the
onclick
attribute:If that doesn't work, could you share the
genTable();
code with us?Sadly, I'm about to go out of town for a week... hopefully I can help you before then.
What I would recommend doing is download my fork of tablesorter because it has an option to disable client-side sorting:
serverSideSorting
(reference).Then you can bind to tablesorter's
sortEnd
function: