码迷,mamicode.com
首页 > 编程语言 > 详细

js表格排序

时间:2015-06-08 19:25:55      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

// a:列数  bool:排序升序判断参数 true false    Str:支持多列

function newUnitSort(a, bool, str) {

                            var oTable = document.getElementById(‘ATEST‘);
                            var arr = [];
                            for (var i = 0; i < oTable.tBodies[0].rows.length; i++)
                            {
                                arr[i] = oTable.tBodies[0].rows[i];
                            }

                            if (bool) {
                                if (str === "name") {
                                    nameSortUp = false;
                                }
                                if (str === "type") {
                                    typeSortUp = false;
                                }
                                if (str === "ip") {
                                    ipSortUp = false;
                                }
                                arr.sort(function(tr1, tr2) {
                                    var str1 = tr1.cells[a].innerHTML;
                                    var str2 = tr2.cells[a].innerHTML;
                                    if (str1.localeCompare(str2) === -1) {
                                        return -1;
                                    } else if (str1.toLocaleString().localeCompare(str2) === 1) {
                                        return 1;
                                    } else {
                                        return 0;
                                    }
                                });
                            } else {
                                if (str === "name") {
                                    nameSortUp = true;
                                }
                                if (str === "type") {
                                    typeSortUp = true;
                                }
                                if (str === "ip") {
                                    ipSortUp = true;
                                }
                                arr.sort(function(tr1, tr2) {
                                    var str1 = tr1.cells[a].innerHTML;
                                    var str2 = tr2.cells[a].innerHTML;
                                    if (str1.localeCompare(str2) === -1) {
                                        return 1;
                                    } else if (str1.toLocaleString().localeCompare(str2) === 1) {
                                        return -1;
                                    } else {
                                        return 0;
                                    }
                                });
                            }
                            for (var i = 0; i < arr.length; i++)
                            {
                                oTable.tBodies[0].appendChild(arr[i]);
                            }
                            

                        }

  

js表格排序

标签:

原文地址:http://www.cnblogs.com/huhanbo/p/4561639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!