jQuery/Tablesorter:维护二级字母顺序排序

发布于 2024-10-10 03:45:49 字数 1862 浏览 3 评论 0原文

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

当用户实际单击年龄 ,但 sortForce 使字母排序成为主要排序。还有其他选择吗?

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

或者我误解了sortForce此处的文档。

更新: 我不能'找不到一个插件可以为我执行此操作,因此我编写了一些代码来对构建表的多维数组进行排序。如果您有一个名为 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;
}

I have a table of names and ages that I want the user to be able to sort. When the page initally loads, sortList lists the rows in order from oldest to youngest, and then secondarily from A to Z.

I want the same thing (a SECONDARY alphabetical sort) when the user actually clicks on the age <th>, but sortForce is making the alphabetical sort primary. Is there an alternative?

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

Or am I misunderstanding sortForce? Documentation here.

Update: I couldn't find a plugin to do this for me, so I wrote some code that sorts the multidimensional array that builds the table. If you have an array called tableContents, and tableContents[0] is a subarray of names and tableContents[1] is a subarray of ages, then calling tableContents.sort(numSort) will sort the array first from oldest to youngest, and then from A to Z. (num2Sort sorts youngest to oldest first.) Then you can recreate the table using createHtml(data), and use the result to replace the old table.

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲欢浪云 2024-10-17 03:45:50

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

jQuery tablesorter secondary排序问题

I was having the same problem, and stumbled upon this:

jQuery tablesorter secondary sorting problem

蔚蓝源自深海 2024-10-17 03:45:50

我不熟悉 tablesorter,但通过查看文档,似乎 sortForce 属性将列锁定为特定顺序 - 然后允许对其他列进行二次排序。我不认为这正是你想要的。

I'm not familiar with tablesorter, but by looking at the documentation, it appears the sortForce property locks down a column to a specific order -- which then allows for sorting of other columns secondarily. I don't think that's exactly what you want.

灯角 2024-10-17 03:45:50

第一个答案中的链接已损坏。这是此页面的网络存档的链接。

jQuery tablesorter 二次排序问题

我稍微修改了此代码以在 v.2.22.3 中工作(也许会在其他版本中工作)

从 tablesorter 的代码中剪切这些行:

if (c.sortForce !== null) {
    arry = c.sortForce;
    for (col = 0; col < arry.length; col++) {
        if (arry[col][0] !== indx) {
            c.sortList.push(arry[col]);
        }
    }
}

并将其粘贴到 c.sortList 之后。 Push([ indx, order ]); - 先前代码所在位置下方的一些行。
Tablesorter v.2.0 对变量有其他名称(例如 c => config、indx => i 等),但您可以找到搜索 sortForce 的正确位置 - 它在Tablesorter的代码

The link in first answer is broken. Here's a link to webarchive of this page.

jQuery tablesorter secondary sorting problem

I had slightly modified this code to work in v.2.22.3 (maybe will work in other versions)

Cut these lines from the tablesorter's code:

if (c.sortForce !== null) {
    arry = c.sortForce;
    for (col = 0; col < arry.length; col++) {
        if (arry[col][0] !== indx) {
            c.sortList.push(arry[col]);
        }
    }
}

And paste it after c.sortList.push([ indx, order ]); - some lines below the place with previous code.
Tablesorter v.2.0 had another names for variables (for c => config, indx => i, etc.) but you can find the right place searching for sortForce - it has several appearances in the code of Tablesorter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文