jquery implements the method of sorting tables locally

  • 2020-05-12 02:14:25
  • OfStack

This article demonstrates an example of how jquery can implement local sorting of tables. Share with you for your reference. The specific implementation method is as follows:


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>jquery Form the sorting </title>
    <style type="text/css">
        thead
        {
            background-color: Blue;
            color: White;
        }
        tr.odd
        {
            background-color: #ddd;
        }
        tr.even
        {
            background-color: #eee;
        }
        .clickable
        {
            text-decoration: underline;
        }
        .hover
        {
            background-color: #5dd354;
        }
        .sorted
        {
            background-color: #ded070;
        }
        .page-number
        {
            color: Black;
            margin:2px 10px;
            padding:2px 5px;
        }
        .active
        {
            border:solid 1px red;
            background-color:#76a7d2;
            }
        .pager
        {
            margin-bottom:10px;
            margin-left:20px;
            }
    </style>
    <script type="text/javascript" language="javascript" src="js/jquery1.3.2.js"></script>
    <script type="text/javascript" language="javascript">
        $(function() {
            jQuery.fn.alternateRowColors = function() {                      // Make it into a plug-in
                $('tbody tr:odd', this).removeClass('even').addClass('odd'); // Interlaced discoloration An odd number of rows
                $('tbody tr:even', this).removeClass('odd').addClass('even'); // Interlaced discoloration Even lines
                return this;
            };
            $('table.myTable').each(function() {
                var $table = $(this);                       // will table Stored as 1 a jquery object
                $table.alternateRowColors($table);          // Discoloration of rows during sorting
                $('th', $table).each(function(column) {
                    var findSortKey;
                    if ($(this).is('.sort-alpha')) {       // Alphabetical order
                        findSortKey = function($cell) {
                            return $cell.find('sort-key').text().toUpperCase() + '' + $cell.text().toUpperCase();
                        };
                    } else if ($(this).is('.sort-numeric')) {       // Sort by Numbers
                        findSortKey = function($cell) {
                            var key = parseFloat($cell.text().replace(/^[^\d.]*/, ''));
                            return isNaN(key) ? 0 : key;
                        };
                    } else if ($(this).is('.sort-date')) {          // Sort by date
                        findSortKey = function($cell) {
                            return Date.parse('1 ' + $cell.text());
                        };
                    }
                    if (findSortKey) {
                        $(this).addClass('clickable').hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); }).click(function() {
                            // Reverse sort state declaration
                            var newDirection = 1;
                            if ($(this).is('.sorted-asc')) {
                                newDirection = -1;
                            }
                            var rows = $table.find('tbody>tr').get(); // Converts data rows into arrays
                            $.each(rows, function(index, row) {
                                row.sortKey = findSortKey($(row).children('td').eq(column));
                            });
                            rows.sort(function(a, b) {
                                if (a.sortKey < b.sortKey) return -newDirection;
                                if (a.sortKey > b.sortKey) return newDirection;
                                return 0;
                            });
                            $.each(rows, function(index, row) {
                                $table.children('tbody').append(row);
                                row.sortKey = null;
                            });
                            $table.find('th').removeClass('sorted-asc').removeClass('sorted-desc');
                            var $sortHead = $table.find('th').filter(':nth-child(' + (column + 1) + ')');
                            // Reverse sort
                            if (newDirection == 1) {
                                $sortHead.addClass('sorted-asc');
                            } else {
                                $sortHead.addClass('sorted-desc');
                            }
                            // Calls the interlaced color change function
                            $table.alternateRowColors($table);
                            // Removes the style of the sorted column , Add the style to the current column
                            $table.find('td').removeClass('sorted').filter(':nth-child(' + (column + 1) + ')').addClass('sorted');
                            $table.trigger('repaginate');
                        });
                    }
                });
            });
        });
        // paging
        $(function() {
            $('table.paginated').each(function() {
                var currentPage = 0;
                var numPerPage = 10;
                var $table = $(this);
                $table.bind('repaginate', function() {
                    $table.find('tbody tr').hide().slice(currentPage * numPerPage, (currentPage + 1) * numPerPage).show();
                });
                var numRows = $table.find('tbody tr').length;
                var numPages = Math.ceil(numRows / numPerPage);
                var $pager = $('<div class="pager"></div>');
                for (var page = 0; page < numPages; page++) {
                    $('<span class="page-number"></span>').text(page + 1)
                     .bind('click', { newPage: page }, function(event) {
                         currentPage = event.data['newPage'];
                         $table.trigger('repaginate');
                         $(this).addClass('active').siblings().removeClass('active');
                     }).appendTo($pager).addClass('clickable');
                }
                $pager.insertBefore($table);
                $table.trigger('repaginate');
                $pager.find('span.page-number:first').addClass('active');
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="myTable paginated">
            <thead>
                <tr>
                    <th class="sort-alpha">
                        Last Name
                    </th>
                    <th class="sort-alpha">
                        First Name
                    </th>
                    <th>
                        Email
                    </th>
                    <th class="sort-numeric">
                        Due
                    </th>
                    <th class="sort-date">
                        Date
                    </th>
                    <th>
                        Web Site
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        tmith
                    </td>
                    <td>
                        erthn
                    </td>
                    <td>
                        eth@gmail.com
                    </td>
                    <td>
                        $34.00
                    </td>
                    <td>
                       14 2009
                    </td>
                    <td>
                        ftp://www.baidu.com
                    </td>
                </tr>
                <tr>
                    <td>
                        TTmith
                    </td>
                    <td>
                        BJohn
                    </td>
                    <td>
                        jsmith@gmail.com
                    </td>
                    <td>
                        $50.00
                    </td>
                    <td>
                        Mar 2009
                    </td>
                    <td>
                        ftp://www.baidu.com
                    </td>
                </tr>
                <tr>
                    <td>
                        CSmith
                    </td>
                    <td>
                        John
                    </td>
                    <td>
                        DDDD@gmail.com
                    </td>
                    <td>
                        $50.00
                    </td>
                    <td>
                        Mar 2009
                    </td>
                    <td>
                        //www.ofstack.com
                    </td>
                </tr>
                <tr>
                    <td>
                        Smith
                    </td>
                    <td>
                        John
                    </td>
                    <td>
                        sdsf@gmail.com
                    </td>
                    <td>
                        $50.00
                    </td>
                    <td>
                        f32 2009
                    </td>
                    <td>
                        ffttp://www.ofstack.com
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    </form>
</body>
</html>

I hope this article has been helpful to your jquery programming.


Related articles: