javascript - jQuery/表格排序器 : maintain secondary alphabetical sort

标签 javascript jquery tablesorter

我有一个姓名和年龄表,我希望用户能够对其进行排序。当页面最初加载时,sortList按从旧到新的顺序列出行,然后从 A 到 Z。

当用户实际点击年龄 <th> 时,我想要同样的东西(二次字母排序) ,但是 sortForce正在使按字母顺序排序成为主要。有替代方案吗?

$('#super_results table').tablesorter({
    sortForce: [[0,0]],
    sortList: [[1,1],[0,0]]
});

还是我误会了sortForceDocumentation 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;
}

最佳答案

我遇到了同样的问题,偶然发现了这个:

jQuery tablesorter secondary sorting problem

关于javascript - jQuery/表格排序器 : maintain secondary alphabetical sort,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4577014/

相关文章:

Javascript 数组变量作用域

javascript - 使用 !obj 简单地检查 undefined 和 null

javascript - 在关闭浏览器选项卡之前执行保存

javascript - HTML 单选按钮 Javascript

javascript - 尝试使用 Tablesorter 完全动态地构建表格

jquery - 带有保存排序小部件的 header 的 CSS 类不正确

javascript - 编写条件以添加属性

javascript - 谷歌地图 : Keep additional info with LatLng while dragging polylines

javascript - 从以不同方式编写的 URL 获取 id 的最佳方法是什么?

javascript - 当我添加更多列时,tablesorter 不起作用