首页 > 代码库 > 表格排序
表格排序
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <div> <table id="tab"> <thead> <tr> <th class="cursor">名字</th> <th class="cursor">年龄</th> <th class="cursor">武力</th> </tr> </thead> <tbody > </tbody> </table> </div> <script type="text/javascript"> var oTab = document.getElementById("tab"); var tBody = oTab.tBodies[0]; var xmlHttp = new XMLHttpRequest(); var data = http://www.mamicode.com/null;"tr"); for(var key in cur){ // console.log("key "+key,data[i][key]); var td = document.createElement("td"); //td.setAttribute("value",data[i][key]); td.innerHTML =data[i][key]; tr.appendChild(td); } farg.appendChild(tr); } tBody.appendChild(farg); } rendData(); var oRows = tBody.rows; Array.apply(null,oRows); function sort(n){ var that = this; this.flag*=-1; var newArray = [].slice.call(oRows); newArray.sort(function(a,b){ var itemA = a.cells[n].innerHTML; var itemB = b.cells[n].innerHTML; if(isNaN(itemA)||isNaN(itemB)){ return itemA.localeCompare(itemB)*(that.flag); }else{ return (parseFloat(itemA)-parseFloat(itemB))*(that.flag); } return ; }); var frag = document.createDocumentFragment(); for(var x =0;x<newArray.length;x++){ frag.appendChild(newArray[x]); } tBody.appendChild(frag); console.log(newArray); } var curs = document.getElementsByClassName("cursor"); for(var v = 0;v <curs.length;v++){ curs[v].flag = -1; curs[v].index = v; curs[v].onclick =function (){ var that = this; sort.call(that,that.index) } } </script> </body> </html>
表格排序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。