使用 jQuery 按列值对正文元素进行排序

Sorting tbody elements by column value with jQuery

本文关键字:元素 排序 正文 jQuery 使用      更新时间:2023-09-26

我正在尝试对表格进行排序 - 因此当用户单击表格标题时,它将按升序/降序排序。我已经到了可以根据列值对表进行排序的程度。但是,我有表格行的分组(每个表正文两行),我想根据每个表体第一行的列中的值对列进行排序,但是当它对表重新排序时,它希望它重新排序表正文,而不是表行。

<table width="100%" id="my-tasks" class="gen-table">
<thead>
    <tr>
        <th class="sortable"><p>Name</p></th>
        <th class="sortable"><p>Project</p></th>
        <th class="sortable"><p>Priority</p></th>
        <th class="sortable"><p>%</p></th>
    </tr>
</thead>
<tbody>
<tr class="sortable-row" id="44">
    <td><p>dfgdf</p></td><td><p>Test</p></td>
    <td><p>1</p></td><td><p>0</p></td>
</tr>
<tr>
    <td></td>
    <td colspan="3"><p>asdfds</p></td>
</tr>
</tbody>
<tbody>
<tr class="sortable-row" id="43">
    <td><p>a</p></td>
    <td><p>Test</p></td>
    <td><p>1</p></td>
    <td><p>11</p></td>
</tr>
<tr>
    <td></td>
    <td colspan="3"><p>asdf</p></td>
</tr>
</tbody>
<tbody>
<tr class="sortable-row" id="40">
    <td><p>Filter Tasks</p></td>
    <td><p>Propel</p></td>
    <td><p>10</p></td>
    <td><p>10</p></td>
</tr>
<tr>
    <td></td>
    <td colspan="3"><p>Add a button to filter tasks.</p></td>
</tr>
</tbody>
</table>

使用以下 JavaScript:

jQuery(document).ready(function () {
    jQuery('thead th').each(function(column) {
        jQuery(this).addClass('sortable').click(function() {
            var findSortKey = function($cell) {
                return $cell.find('.sort-key').text().toUpperCase() + ' ' + $cell.text().toUpperCase();
            };
            var sortDirection = jQuery(this).is('.sorted-asc') ? -1 : 1;
            var $rows = jQuery(this).parent().parent().parent().find('.sortable-row').get();
            jQuery.each($rows, function(index, row) {
                row.sortKey = findSortKey(jQuery(row).children('td').eq(column));
            });
            $rows.sort(function(a, b) {
                if (a.sortKey < b.sortKey) return -sortDirection;
                if (a.sortKey > b.sortKey) return sortDirection;
                return 0;
            });
            jQuery.each($rows, function(index, row) {
                jQuery('#propel-my-tasks').append(row);
                row.sortKey = null;
            });
            jQuery('th').removeClass('sorted-asc sorted-desc');
            var $sortHead = jQuery('th').filter(':nth-child(' + (column + 1) + ')');
            sortDirection == 1 ? $sortHead.addClass('sorted-asc') : $sortHead.addClass('sorted-desc');
            jQuery('td').removeClass('sorted').filter(':nth-child(' + (column + 1) + ')').addClass('sorted');
        });
    });
});
您需要

tbody元素进行排序,而不是对row元素进行排序。 你在问题的描述中说了你自己,但你的代码实际上是对行进行排序,而不是对行进行排序。

次要问题是您的排序将所有内容视为字符串,当将 1 位数字字符串 ("2") 与两位字符串 ("10") 排序时会中断。

要修复,请替换以下内容:

        var $rows = jQuery(this).parent().parent().parent()
            .find('.sortable-row').get();
        jQuery.each($rows, function(index, row) {
            row.sortKey = findSortKey(jQuery(row).children('td').eq(column));
        });

有了这个:

        var $tbodies = jQuery(this).parent().parent().parent()
            .find('.sortable-row').parent().get();
        jQuery.each($tbodies, function(index, tbody) {
            var x = findSortKey(jQuery(tbody).find('tr > td').eq(column));
            var z = ~~(x); // if integer, z == x
            tbody.sortKey = (z == x) ? z : x;
        });

然后在整个脚本中将$rows替换为$tbodies,并使用 tbody 进行行。

例:
http://jsbin.com/oxuva5

我强烈推荐jQuery插件 http://tablesorter.com/而不是自己推出。

它功能齐全,支持良好。