我使用TableSorter插件允许用户轻松地对数据进行排序。它通常很棒。我的问题是第一列包含一个自动递增的值。当用户对第一列以外的列进行排序时,我希望使用这些数字。该表如下所示:
$y = 0;
foreach ($data as $row) {
$y++;
$table = "<tr><td>$y</td></tr>
<td>$row->name</td>
</tr>";
}
print "<table id='myTable' class='tablesorter' cellpadding='1'>
<th>No</th>
<th>Name</th>
$table
</table>";
排序后,我希望$y
重新排序,以便顶行始终显示1.如何在不将请求发送回服务器的情况下执行此操作?的Javascript?
答案 0 :(得分:0)
在tablesorter主页wiki页面上(在Widgets&gt; other&gt;“Automatic Row numbering”下),有三个例子:
Automatic row numbering without pager
// add custom numbering widget
$.tablesorter.addWidget({
id: "numbering",
format: function(table) {
var c = table.config;
$("tr", table.tBodies[0]).each(function(i) {
$(this).find('td').eq(0).text(i + 1);
});
}
});
Automatic row numbering with values instead of an index
// add custom numbering widget
var columnText = [ 'a', 'b', 'c', 'd', 'e' ];
$.tablesorter.addWidget({
id: "numbering",
format: function(table) {
$("tr:visible", table.tBodies[0]).each(function(i) {
$(this).find('td').eq(0).text( columnText[i] );
});
}
});