3

テーブル内のテーブル行を並べ替えるタスクがあります。表のデータは、日付、数値、文字列などのすべてが混在しています。

私は多くのリンクをたどりましたが、いくつかは準備ができたライブラリに向けられていることがわかりました。これは私には役に立たない。最後に、私はすべてのおっぱいとビットを使用して自分で作った多くのことを経験しました。これは数のためだけに働いています

これはスクリプトです:

  $(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>
4

1 に答える 1

3

宿題のように聞こえますが、自分で努力を示しているので問題ありません。テーブルソーターが許可されていない場合は、ダウンロードしてください。デバッグバージョンがあると思いますので、コードをご覧ください。それはあなたの探求に役立つはずです。

これが宿題である場合、プログラミングの黄金のルールは車輪の再発明ではないことを現実の世界で覚えておいてください。ニーズに合ったプラグインがある場合は、それを使用してください。

あなたが持っているものを修正する

現状のコードの問題は次のとおりです。

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
 }

これが動作中のフィドルです。

これは非常に大まかな計算チェックであり、他のデータ型については他のチェックとバランスが必要になる可能性があることに注意してください。

于 2012-08-30T05:29:05.933 に答える