


Detailed explanation of the steps to implement local sorting of tables with jquery (with code)
这次给大家带来jquery实现表格本地排序步骤详解(附代码),jquery实现表格本地排序的注意事项有哪些,下面就是实战案例,一起来看一下。
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>jquery 表格排序</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() { //做成插件的形式 $('tbody tr:odd', this).removeClass('even').addClass('odd'); //隔行变色 奇数行 $('tbody tr:even', this).removeClass('odd').addClass('even'); //隔行变色 偶数行 return this; }; $('table.myTable').each(function() { var $table = $(this); //将table存储为一个jquery对象 $table.alternateRowColors($table); //在排序时隔行变色 $('th', $table).each(function(column) { var findSortKey; if ($(this).is('.sort-alpha')) { //按字母排序 findSortKey = function($cell) { return $cell.find('sort-key').text().toUpperCase() + '' + $cell.text().toUpperCase(); }; } else if ($(this).is('.sort-numeric')) { //按数字排序 findSortKey = function($cell) { var key = parseFloat($cell.text().replace(/^[^\d.]*/, '')); return isNaN(key) ? 0 : key; }; } else if ($(this).is('.sort-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() { //反向排序状态声明 var newDirection = 1; if ($(this).is('.sorted-asc')) { newDirection = -1; } var rows = $table.find('tbody>tr').get(); //将数据行转换为数组 $.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) + ')'); //实现反向排序 if (newDirection == 1) { $sortHead.addClass('sorted-asc'); } else { $sortHead.addClass('sorted-desc'); } //调用隔行变色的函数 $table.alternateRowColors($table); //移除已排序的列的样式,添加样式到当前列 $table.find('td').removeClass('sorted').filter(':nth-child(' + (column + 1) + ')').addClass('sorted'); $table.trigger('repaginate'); }); } }); }); }); //分页 $(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').leng th; var numPages = Math.ceil(numRows / numPerPage); var $pager = $('<p class="pager"></p>'); 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"> <p> <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> PHP </td> <td> $34.00 </td> <td> 14 2009 </td> <td> PHP </td> </tr> <tr> <td> TTmith </td> <td> BJohn </td> <td> PHP </td> <td> $50.00 </td> <td> Mar 2009 </td> <td> PHP </td> </tr> <tr> <td> CSmith </td> <td> John </td> <td> PHP </td> <td> $50.00 </td> <td> Mar 2009 </td> <td> PHP </td> </tr> <tr> <td> Smith </td> <td> John </td> <td> PHP </td> <td> $50.00 </td> <td> f32 2009 </td> <td> PHP </td> </tr> </tbody> </table> </p> </form> </body> </html>
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Detailed explanation of the steps to implement local sorting of tables with jquery (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In our work, we often use wps software. There are many ways to process data in wps software, and the functions are also very powerful. We often use functions to find averages, summaries, etc. It can be said that as long as The methods that can be used for statistical data have been prepared for everyone in the WPS software library. Below we will introduce the steps of how to sort the scores in WPS. After reading this, you can learn from the experience. 1. First open the table that needs to be ranked. As shown below. 2. Then enter the formula =rank(B2, B2: B5, 0), and be sure to enter 0. As shown below. 3. After entering the formula, press the F4 key on the computer keyboard. This step is to change the relative reference into an absolute reference.

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

WPS is a very complete office software, including text editing, data tables, PPT presentations, PDF formats, flow charts and other functions. Among them, the ones we use most are text, tables, and demonstrations, and they are also the ones we are most familiar with. In our study work, we sometimes use WPS tables to make some data statistics. For example, the school will count the scores of each student. If we have to manually sort the scores of so many students, it will be really a headache. In fact, we don’t have to worry, because our WPS table has a sorting function to solve this problem for us. Next, let’s learn how to sort WPS together. Method steps: Step 1: First we need to open the WPS table that needs to be sorted

Sorting methods in excel: 1. Single column sorting; 2. Multiple column sorting; 3. Custom sorting. Detailed introduction: 1. Single-column sorting is the most common sorting method. It sorts according to a selected column; 2. Multi-column sorting refers to sorting data in multiple columns, usually sorting according to a certain column first. On the basis of, sort by another column; 3. Custom sorting, allowing users to define the sort order according to their own needs.

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

In this article, we will show you how to reorder multiple columns in PowerQuery by dragging and dropping. Often, when importing data from various sources, columns may not be in the desired order. Reordering columns not only allows you to arrange them in a logical order that suits your analysis or reporting needs, it also improves the readability of your data and speeds up tasks such as filtering, sorting, and performing calculations. How to rearrange multiple columns in Excel? There are many ways to rearrange columns in Excel. You can simply select the column header and drag it to the desired location. However, this approach can become cumbersome when dealing with large tables with many columns. To rearrange columns more efficiently, you can use the enhanced query editor. Enhancing the query

PHP is a commonly used server-side scripting language widely used in website development and data processing fields. In PHP, it is a very common requirement to sort the values in an array by size. By using the built-in sort function, you can easily sort arrays. The following will introduce how to use PHP to sort the values in an array by size, with specific code examples: 1. Sort the values in the array in ascending order:
