作家
登录

html中table数据排序的js代码

作者: 来源:www.28hudong.com 2013-03-30 00:46:43 阅读 我要评论

对了,注意那个innerText和innerHTML 复制代码 代码如下: function sortCells(event) { var obj = event.target; var count = 0; count是记录点击次数的,根据奇偶进行升序或降序 if(!obj.getAttribute("clickCount")){ obj.setAttribute("clickCount", 0); } else { count = parseInt(obj.getAttribute("clickCount")); count++; obj.setAttribute("clickCount", count); } var table = event.target.parentNode.parentNode; if(table.nodeName.localeCompare("THEAD") == 0){ if(table.parentNode.nodeName.localeCompare("TABLE") == 0){ table = table.parentNode; } else { return; } } else if(table.nodeName.localeCompare("TBODY") == 0){ if(table.parentNode.nodeName.localeCompare("TABLE") == 0){ table = table.parentNode; } else { return; } } else if(table.nodeName.localeCompare("TABLE") == 0){ } else { return; } var colNum; for(x = 0; x < table.rows(1).cells.length; x++){ if(event.target.innerText.localeCompare(table.rows(0).cells[x].innerText) == 0){ colNum = x; break; } } var column = table.rows(1).cells.length; var row = table.rows.length; var Ar = new Array(row - 1); for (x = 0; x < row - 1; x++) { Ar[x] = new Array(column); } for (x = 1; x < row; x++) { for (y = 0; y < column; y++) { Ar[x - 1][y] = table.rows(x).cells(y).innerHTML; } }      //这个可以对字符串进行本地化排序 /* if((count%2) == 0){ Ar.sort(function(a, b) { return b[colNum].localeCompare(a[colNum]) }); } else { Ar.sort(function(a, b) { return a[colNum].localeCompare(b[colNum]) }); } */ var temp; for (x = 0; x < row - 1; x++) { for (y = 1; y < row - 1; y++) { temp = Ar[y - 1]; if((count % 2) == 0){ if (parseInt(Ar[y - 1][colNum]) >= parseInt(Ar[y][colNum])) { Ar[y - 1] = Ar[y]; Ar[y] = temp; } } else { if (parseInt(Ar[y - 1][colNum]) <= parseInt(Ar[y][colNum])) { Ar[y - 1] = Ar[y]; Ar[y] = temp; } } } } for (x = 1; x < row; x++) { for (y = 0; y < column; y++) { table.rows(x).cells(y).innerHTML = Ar[x - 1][y]; } } count++; }

  推荐阅读

  JQuery动态创建DOM、表单元素的实现代码

复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta >>>详细阅读


本文标题:html中table数据排序的js代码

地址:http://www.17bianji.com/kaifa2/JS/24294.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)