jquery实现表格本地排序的方法


Posted in Javascript onMarch 11, 2015

本文实例讲述了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').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>

                        https://3water.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://3water.com

                    </td>

                </tr>

            </tbody>

        </table>

    </div>

    </form>

</body>

</html>

希望本文所述对大家的jquery程序设计有所帮助。

Javascript 相关文章推荐
js判断两个日期是否相等的方法
Sep 10 Javascript
测试IE浏览器对JavaScript的AngularJS的兼容性
Jun 19 Javascript
基于jQuery Bar Indicator 插件实现进度条展示效果
Sep 30 Javascript
JavaScript正则表达式的分组匹配详解
Feb 13 Javascript
使用jquery获取url及url参数的简单实例
Jun 14 Javascript
canvas的神奇用法
Feb 03 Javascript
微信小程序 template模板详解及实例
Feb 21 Javascript
React Native中导航组件react-navigation跨tab路由处理详解
Oct 31 Javascript
Angular4 组件通讯方法大全(推荐)
Jul 12 Javascript
Vue中 key keep-alive的实现原理
Sep 18 Javascript
vue3修改link标签默认icon无效问题详解
Oct 09 Javascript
微信小程序实现注册登录功能(表单校验、错误提示)
Dec 10 Javascript
jQuery实现渐变下拉菜单的简单方法
Mar 11 #Javascript
jQuery实现新消息闪烁标题提示的方法
Mar 11 #Javascript
javascript实现详细时间提醒信息效果的方法
Mar 11 #Javascript
JavaScript实现SHA-1加密算法的方法
Mar 11 #Javascript
JavaScript前补零操作实例
Mar 11 #Javascript
JavaScript限定图片显示大小的方法
Mar 11 #Javascript
iScroll中事件点击触发两次解决方案
Mar 11 #Javascript
You might like
php 格式化数字的时候注意数字的范围
2010/04/13 PHP
php通过strpos查找字符串出现位置的方法
2015/03/17 PHP
基于JQuery+PHP编写砸金蛋中奖程序
2015/09/08 PHP
thinkphp的dump函数无输出实例代码
2016/11/15 PHP
php解析非标准json、非规范json的方式实例
2020/12/10 PHP
通过代码实例解析PHP session工作原理
2020/12/11 PHP
使用js获取QueryString的方法小结
2010/02/28 Javascript
基于datagrid框架的查询
2013/04/08 Javascript
JS替换字符串中字符即替换全部而不是第一个
2014/06/04 Javascript
js与C#进行时间戳转换
2014/11/14 Javascript
node.js中的console.assert方法使用说明
2014/12/10 Javascript
jQuery实现菜单式图片滑动切换
2015/03/14 Javascript
js给selected添加options的方法
2015/05/06 Javascript
JavaScript中setMonth()方法的使用详解
2015/06/11 Javascript
js图片上传前预览功能(兼容所有浏览器)
2016/08/24 Javascript
JS基于面向对象实现的多个倒计时器功能示例
2017/02/28 Javascript
angularjs实现猜大小功能
2017/10/23 Javascript
深入研究React中setState源码
2017/11/17 Javascript
浅谈webpack devtool里的7种SourceMap模式
2019/01/14 Javascript
Node.js 进程平滑离场剖析小结
2019/01/24 Javascript
vue draggable resizable 实现可拖拽缩放的组件功能
2019/07/15 Javascript
使用Node.js在深度学习中做图片预处理的方法
2019/09/18 Javascript
基于JS判断对象是否是数组
2020/01/10 Javascript
vuex刷新后数据丢失的解决方法
2020/10/18 Javascript
[01:08:57]2014 DOTA2国际邀请赛中国区预选赛 5 23 CIS VS LGD第二场
2014/05/24 DOTA
使用Python生成随机密码的示例分享
2016/02/18 Python
Python处理XML格式数据的方法详解
2017/03/21 Python
python实现识别手写数字 python图像识别算法
2020/03/23 Python
通过python的matplotlib包将Tensorflow数据进行可视化的方法
2019/01/09 Python
dpn网络的pytorch实现方式
2020/01/14 Python
利用keras加载训练好的.H5文件,并实现预测图片
2020/01/24 Python
css3实现3d旋转动画特效
2015/03/10 HTML / CSS
html5使用canvas实现跟随光标跳动的火焰效果
2014/01/07 HTML / CSS
2013年入党人员的自我鉴定
2013/10/25 职场文书
电子信息专业学生自荐信
2013/11/09 职场文书
部门年终奖分配方案
2014/05/07 职场文书