我有一个姓名和年龄表,我希望用户能够对其进行排序。当页面最初加载时,sortList
按从旧到新的顺序列出行,然后从 A 到 Z。
当用户实际点击年龄 <th>
时,我想要同样的东西(二次字母排序) ,但是 sortForce
正在使按字母顺序排序成为主要。有替代方案吗?
$('#super_results table').tablesorter({
sortForce: [[0,0]],
sortList: [[1,1],[0,0]]
});
还是我误会了sortForce
? Documentation here.
更新:我找不到一个插件来为我做这件事,所以我写了一些代码来对构建表的多维数组进行排序。如果你有一个名为 tableContents
的数组, 和 tableContents[0]
是名称的子数组,tableContents[1]
是年龄的子数组,然后调用 tableContents.sort(numSort)
将首先从最老到最年轻,然后从 A 到 Z 对数组进行排序。(num2Sort
首先从最年轻到最老排序。)然后您可以使用 createHtml(data)
重新创建表, 并用结果替换旧表。
function numSort(a, b) {
if (a[1] === b[1]) {
if (a[0] === b[0]) {
return 0;
}
return (a[0] < b[0]) ? -1 : 1;
}
return (a[1] < b[1]) ? 1 : -1;
}
function num2Sort(a, b) {
if (a[1] === b[1]) {
if (a[0] === b[0]) {
return 0;
}
return (a[0] < b[0]) ? -1 : 1;
}
return (a[1] < b[1]) ? -1 : 1;
}
function createHtml(data) {
var completeListLength = MYAPP.completeList.length,
html = '<table>';
html += '<thead>';
html += '<tr>';
html += '<th>names</th>';
html += '<th>ages</th>';
html += '</tr>';
html += '</thead>';
html += '<tbody>';
for (var i = 0; i < completeListLength; i += 1) {
html += '<tr>';
html += '<td>' + data[i][0] + '</td>';
html += '<td>' + data[i][1] + '</td>';
html += '</tr>';
}
html += '</tbody>';
html += '</table>';
return html;
}
最佳答案
我遇到了同样的问题,偶然发现了这个:
关于javascript - jQuery/表格排序器 : maintain secondary alphabetical sort,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4577014/