微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-当我们单击表格标题时,对表格中的行进行排序

我的任务是对表格中的表格行进行排序.表格中的数据是日期,数字,字符串等所有内容的混合.

我浏览了很多链接,在其中找到了一些指向就绪库的链接.这对我没有用.最终,我经历了很多事情,我用所有的山雀和小东西制作了自己的东西.这仅适用于数字

这是脚本:

  $(document).ready(function () {

        //grab all header rows
        $('th').each(function (column) {
            $(this).addClass('sortable').click(function () {
                    var findSortKey = function ($cell) {
                        return $cell.find('.sort-key').text().toupperCase()+ ' ' + $cell.text().toupperCase();

                    };
                    var sortDirection = $(this).is('.sorted-asc') ? -1 : 1;
                    var $rows = $(this).parent().parent().parent().find('tbody tr').get();

                    //loop through all the rows and find
                    $.each($rows, function (index, row) {
                        row.sortKey = findSortKey($(row).children('td').eq(column));
                    });

                    //compare and sort the rows alphabetically or numerically
                    $rows.sort(function (a, b) {
                        if (a.sortKey.indexOf('-') == -1) {
                            if (parseInt(a.sortKey) < parseInt(b.sortKey)) {
                                return -sortDirection;
                            }
                            if (parseInt(a.sortKey) > parseInt(b.sortKey)) {                                
                                return sortDirection;
                            }
                        } else {
                            if (a.sortKey < b.sortKey) {
                                return -sortDirection;
                            }
                            if (a.sortKey > b.sortKey) {
                                return sortDirection;
                            }
                        }
                        return 0;
                    });

                    //add the rows in the correct order to the bottom of the table
                    $.each($rows, function (index, row) {
                        $('tbody').append(row);
                        row.sortKey = null;
                    });

                    //identify the column sort order
                    $('th').removeClass('sorted-asc sorted-desc');
                    var $sortHead = $('th').filter(':nth-child(' + (column + 1) + ')');
                    sortDirection == 1 ? $sortHead.addClass('sorted-asc') : $sortHead.addClass('sorted-desc');

                    //identify the column to be sorted by
                    $('td').removeClass('sorted').filter(':nth-child(' + (column + 1) + ')').addClass('sorted');
                });
            });
        });

这是文档的样式:

    <style>
    root
    {
        display: block;
    }

    th.sortable
    {
        color: #666;
        cursor: pointer;
        text-decoration: underline;
    }

        th.sortable:hover
        {
            color: black;
        }

    th.sorted-asc, th.sorted-desc
    {
        color: black;
        background-color: cadetblue;
    }
</style>

因此,下面是HTML部分.

<table>
    <tbody>
        <tr>
            <th class="sortable">Name</th>
            <th class="sortable">Salary</th>
            <th>Extension</th>
            <th>Start date</th>
            <th>Start date (American)</th>
        </tr>
        <tr>
            <td>Bloggs, Fred</td>
            <td>$12000.00</td>
            <td>1353</td>
            <td>18/08/2003</td>
            <td>08/18/2003</td>
        </tr>
        <tr>
            <td>Turvey, Kevin</td>
            <td>$191200.00</td>
            <td>2342</td>
            <td>02/05/1979</td>
            <td>05/02/1979</td>
        </tr>
        <tr>
            <td>Mbogo, Arnold</td>
            <td>$32010.12</td>
            <td>2755</td>
            <td>09/08/1998</td>
            <td>08/09/1998</td>
        </tr>
        <tr>
            <td>Shakespeare, Bill</td>
            <td>$122000.00</td>
            <td>3211</td>
            <td>12/11/1961</td>
            <td>11/12/1961</td>
        </tr>
        <tr>
            <td>Shakespeare, Hamnet</td>
            <td>$9000</td>
            <td>9005</td>
            <td>01/01/2002</td>
            <td>01/01/2002</td>
        </tr>
        <tr>
            <td>Fitz, Marvin</td>
            <td>$3300</td>
            <td>5554</td>
            <td>22/05/1995</td>
            <td>05/22/1995</td>
        </tr>
    </tbody>
</table>

解决方法:

听起来很像功课,这很不错,因为您自己付出了一些努力.如果不允许使用tablesorter,请下载它,我认为这里有一个调试版本,请看一下代码.那应该可以帮助您完成任务.

如果这是家庭作业,请在现实世界中记住,编程的黄金法则是不要重新发明轮子.如果有满足您需求的插件,请使用它.

修理你所拥有的

您的代码目前存在的问题是:

if (a.sortKey.indexOf('-') == -1) {
    if (parseInt(a.sortKey) < parseInt(b.sortKey)) {
        return -sortDirection;
     }

     if (parseInt(a.sortKey) > parseInt(b.sortKey)) {                                
          return sortDirection;
     }
} else {
    //Non numeric sort
}

在这里,如果a不包含-,您总是试图将其排序为整数.一个非常粗略的解决方法是:

if (a.sortKey.indexOf('-') == -1 && (!isNaN(a.sortKey) && !isNaN(a.sortKey))) {
//Rough Numeracy check                          

    if (parseInt(a.sortKey) < parseInt(b.sortKey)) {
        return -sortDirection;
    }

    if (parseInt(a.sortKey) > parseInt(b.sortKey)) {                                
        return sortDirection;
    }

 } else {
    //Non numeric sort
 }

这是工作中的fidle.

请记住,这是一个非常粗糙的计算能力检查,您可能需要其他检查和其他数据类型的余额.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐