I have a PHP generated table from a postgres query. How can I insert an index column with numbered rows at the beginning of the table using tablesorter plugin? The sorting w
I'm not that great with php, but couldn't you just do this?
echo "<table id=\"tabel\" class=\"tablesorter\">
<thead>
<tr>
<th>#</th>";
.
.
.
//next code fetch cells content
echo "<tbody>";
$i=1;
while ($row=pg_fetch_row($result)){
echo "<tr>";
echo "<td> $i </td>";
$i++;
foreach($row as $_column){
echo "<td> $_column </td>";
}
echo "</tr>";
}
If you want a that column to not sort and stay unchanged, you can use the following widget (demo) with header option to prevent sorting:
// target the number column using a zero-based index
var number_column = 0;
// add custom numbering widget
$.tablesorter.addWidget({
id: "numbering",
format: function(table) {
var c = table.config;
$("tr:visible", table.tBodies[0]).each(function(i) {
$(this).find('td').eq(number_column).text(i + 1);
});
}
});
$("table").tablesorter({
theme: 'blue',
// prevent first column from being sortable
headers: {
0: { sorter: false }
},
// apply custom widget
widgets: ['numbering']
});