如下图,请实现表格信息的排序功能,当点击表头的属性区域,将表格信息进行排序切换功能,即第一次点击为降序排序,再一次点击进行升序排序。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>表格排序</title> </head> <style type="text/css"> table{ width: 500px; height: 200px; position: relative; border-width: 0px 0px 1px 1px; border-style: solid; } tr{ height: 40px; } td,th{ width: 25%; border-width: 1px 1px 0 0; border-style: solid; padding: 0; margin: 0; } </style> <body> <table id="table" cellpadding="1" cellspacing="1"> <thead> <tr bgcolor="grey"> <th>姓名</th> <th onclick="sortTable(1)">力量</th> <th onclick="sortTable(2)">敏捷</th> <th onclick="sortTable(3)">智力</th> </tr> </thead> <tbody> <tr> <th>德鲁伊</th> <td>17</td> <td>24</td> <td>13</td> </tr> <tr> <th>月之骑士</th> <td>15</td> <td>22</td> <td>16</td> </tr> <tr> <th>众神归王</th> <td>19</td> <td>35</td> <td>20</td> </tr> <tr> <th>流浪剑客</th> <td>23</td> <td>15</td> <td>14</td> </tr> </tbody> </table> <script type="text/javascript"> var isSort = [false,false,false];//标记是否排过序 function sortTable (colNo) { var rowsArray = [];//表格中所有行的集合 var colsArray = [];//表格中所有列的集合 var tbody = document.getElementsByTagName('tbody')[0]; //初始化行和列 for (var i = 0; i < tbody.rows.length; i++) { rowsArray[i] = tbody.rows[i]; colsArray[i] = rowsArray[i].cells[colNo]; } //排序 console.log(isSort[colNo]) if (!isSort[colNo]) {//isSort为false时,降序排列 isSort[colNo] = true; colsArray.sort(function (a,b) { return b.innerHTML - a.innerHTML;//将被点的那一列降序排序 }); }else{ //此时已经降序排序过了,对数组逆序即可 colsArray.reverse(); isSort[colNo] = false; } //当某一列排序后将表格所有元素的值放到新的数组中 var rowsTempArray = []; for (var i = 0; i < rowsArray.length; i++) { var colsTempArray = []; for (var j = 0; j < colsArray.length; j++) { //将i行的所有列的内容保存在colsTempArray[j]中 colsTempArray[j] = colsArray[i].parentNode.cells[j].innerHTML; } //将一行内容保存到rowsTempArray。 rowsTempArray[i] = colsTempArray; } //重绘页面 for (var i = 0; i < rowsArray.length; i++) { for (var j = 0; j < colsArray.length; j++) { rowsArray[i].cells[j].innerHTML = rowsTempArray[i][j]; } } } </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <table border="1" cellpadding="10" id="sortTable"> <thead align="left"> <tr> <th>姓名</th> <th>力量</th> <th>敏捷</th> <th>智力</th> </tr> </thead> <tbody></tbody> </table> </body> <script> </script> </html>function SortTable( dom ) { this.dom = dom; this.init(); } var propertys = { init : function() { this.setting(); this.render(); this.bindEvent(); }, setting : function() { //数据没有那么重要吧 this.data = [ {name : "德鲁伊", power : 17, speed : 24, intel : 13}, {name : "napoleon", power : 23, speed : 14, intel : 33}, {name : "覃小夫", power : 122, speed : 122, intel : 156}, {name : "敏感词", power : 10, speed : 10, intel : 120} ]; this.isClick = { power : null, speed : null, intel : null }; }, render : function() { var str = ''; for ( var i = 0; i < this.data.length; i++ ) { str += '<tr>'; for ( var pro in this.data[i] ) { str += '<td>' + this.data[i][pro] + '</td>'; } str += '</tr>'; } this.dom.innerHTML = str; }, bindEvent : function() { this.clickEvent = this.handleClick.bind( this ); //让table元素来响应click事件 this.dom.parentNode.addEventListener( 'click', this.clickEvent, false ); }, handleClick : function(event) { var tag = event.target.innerHTML.toLowerCase(); switch( tag ) { case '力量': this.sort( 'power' ); break; case '敏捷': this.sort( 'speed' ); break; case '智力': this.sort( 'intel' ); break; } this.render(); }, sort : function(property) { this.data.sort( function(a,b) { if ( this.isClick[property] ) return a[property] > b[property]; else return a[property] < b[property]; }.bind(this) ); this.isClick[property] = !this.isClick[property]; }, destroy : function() { this.data = null; this.isClick = null; this.dom.parentNode.removeEventListener( 'click', this.clickEvent ); this.dom = null; } }; SortTable.prototype = Object.create( propertys );var tableSorting = new SortTable( document.querySelector( '#sortTable tbody'));
<!DOCTYPE html> <html> <head> <title>Test</title> </head> <body> <style type="text/css"> table,th,td{ border: 1px solid #ccc; border-collapse: collapse; } th,td{ width: 70px; text-align: center; } th:hover{ background-color: #09f; } </style> <table> <tr> <th>姓名</th> <th>力量</th> <th>敏捷</th> <th>智力</th> </tr> <tr> <td>德鲁伊</td> <td>17</td> <td>24</td> <td>13</td> </tr> <tr> <td>月之骑士</td> <td>15</td> <td>22</td> <td>16</td> </tr> <tr> <td>众神之王</td> <td>19</td> <td>15</td> <td>20</td> </tr> <tr> <td>流浪剑客</td> <td>23</td> <td>15</td> <td>14</td> </tr> </table> <script type="text/javascript"> var thArr = document.querySelectorAll("th"); var tdArr = document.querySelectorAll("td"); var tdContentArr = []; var ascend = false; //升序排序标志位 function convertType(index,str){ if(index !== 0){ return parseInt(str); } else{ return str.toString(); } } for(let i=0;i<(tdArr.length/thArr.length);i++){ tdContentArr[i] = []; //二维数组 for(let j=0;j<thArr.lengt敏感词dContentArr[i].push(tdArr[i*thArr.length+j].innerHTML); } } console.log(tdContentArr); for(let i=0;i<thArr.length;i++){ thArr[i].onclick = function(){ let index = i; //点击的第几列 //冒泡排序 升序排序 for(let j=0;j<(tdArr.length/thArr.length);j++){ for(let k=1;k<(tdArr.length/thArr.length);k++){ if(convertType(index,tdContentArr[k][index])<convertType(index,tdContentArr[k-1][index])){ //升序排序 let temp; for(let x=0;x<thArr.length;x++){ //交换两行内容 temp = tdContentArr[k][x]; tdContentArr[k][x] = tdContentArr[k-1][x]; tdContentArr[k-1][x] = temp; } } } } console.log(tdContentArr); var rowItems = thArr.length; //每行的数目 var colItems = tdArr.length/thArr.length; //升序排序 if(ascend){ for(let i=0;i<tdArr.length;i++){ tdArr[i].innerHTML = tdContentArr[parseInt(i/rowItems)][parseInt(i%rowItems)]; } } else{ //降序序排序 for(let i=0;i<tdArr.length;i++){ tdArr[i].innerHTML = tdContentArr[colItems-1-parseInt(i/rowItems)][parseInt(i%rowItems)]; } } ascend = !ascend; } } </script> </body> </html>
<!DOCTYPE html> <html> <head> <title>sort table</title> <meta charset="utf-8"> <style type="text/css"> *{ padding:0; margin:0; } table tr td{ width:100px; height:30px; line-height:30px; text-align:center; } </style> </head> <body> <table border="1" cellspacing="0" > <tr style="background:#eee;"> <td>姓名</td> <td>力量</td> <td>敏捷</td> <td>智力</td> </tr> <tr> <td>德鲁伊</td> <td>17</td> <td>24</td> <td>13</td> </tr> <tr> <td>月之骑士</td> <td>15</td> <td>22</td> <td>16</td> </tr> <tr> <td>众神之王</td> <td>19</td> <td>15</td> <td>20</td> </tr> <tr> <td>流浪剑客</td> <td>23</td> <td>15</td> <td>14</td> </tr> </table> </body> <script type="text/javascript"> var thead=document.getElementsByTagName("tr")[0]; var td=thead.getElementsByTagName("td"); for(var i=0;i<td.length;i++){ td[i].index=i; td[i].onclick=function(){ sortTable(this.index); }; } var trlist=document.getElementsByTagName("tr"); var table=document.getElementsByTagName("table")[0]; function sortTable(index){ console.log(index); var tr=Array.prototype.slice.call(trlist,1); tr.sort(function(a,b){ var td1=a.getElementsByTagName("td")[index].innerText; var td2=b.getElementsByTagName("td")[index].innerText; if(td1*1){ return td1-td2; }else{ return td1>td2?-1:1; } }); //console.log(tr); table.innerHTML=""; table.appendChild(thead); for(var i=0;i<tr.length;i++){ table.appendChild(tr[i]); } } </script> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>对列表进行排序</title> </head> <body> <script type="text/javascript"> var obj1 = { name:'德鲁伊', strength:'17', veh:'24', intelligence:'13' }; var obj2 = { name:'月之骑士', strength:'15', veh:'22', intelligence:'16' }; var obj3 = { name:'众神之王', strength:'19', veh:'15', intelligence:'20' }; var obj4 = { name:'流浪剑客', strength:'23', veh:'15', intelligence:'14' }; var arr = [obj1,obj2,obj3,obj4]; window.onload = function(){ var tr = document.getElementsByTagName('tr'); var th = document.getElementsByTagName('th'); function insert(){ for (var i = 1; i < tr.length; i++) { tr[i].innerHTML = '<td>'+arr[i-1].name+'</td>'+'<td>'+arr[i-1].strength+'</td>'+'<td>'+arr[i-1].veh+'</td>'+'<td>'+arr[i-1].intelligence+'</td>'; } } insert(); function sortByAttr1(attr){ return function(obj1,obj2){ var a = obj1[attr]; var b = obj2[attr]; return a-b; } } function sortByAttr2(attr){ return function(obj1,obj2){ var a = obj1[attr]; var b = obj2[attr]; return b-a; } } var flag; for (var i = 1; i < th.length; i++) { switch(th[i].innerHTML){ case '力量': th[i].attr='strength';break; case '敏捷': th[i].attr = 'veh';break; case '智力': th[i].attr = 'intelligence';break; default: alert('error'); } th[i].onclick = function(){ if(flag == false){ arr.sort(sortByAttr1(this.attr)); flag = true; }else{ arr.sort(sortByAttr2(this.attr)); flag = false; } insert(); }; } } </script> <table cellspacing="0px" cellpadding="0px" border="1px"> <tr><th>姓名</th><th>力量</th><th>敏捷</th><th>智力</th></tr> <tr></tr> <tr></tr> <tr></tr> <tr></tr> </table> </body> </html>
<table > <thead id="jsThead"> <tr> <th>姓名</th> <th>力量</th> <th>敏捷</th> <th>智力</th> </tr> </thead> <tbody id="jsTable"> <tr> <td>德鲁伊</td> <td>17</td> <td>24</td> <td>13</td> </tr> <tr> <td>月之骑士</td> <td>15</td> <td>22</td> <td>16</td> </tr> <tr> <td>众神之王</td> <td>19</td> <td>15</td> <td>20</td> </tr> <tr> <td>流浪剑客</td> <td>23</td> <td>15</td> <td>14</td> </tr> </tbody> </table> <style> body,html{ padding: 0; margin: 0; font-size: 14px; color: #000000; } table{ border-collapse: collapse; width: 100%; table-layout: fixed; } thead{ background: #3d444c; color: #ffffff; } td,th{ border: 1px solid #e1e1e1; padding: 0; height: 30px; line-height: 30px; text-align: center; } </style> <script> function sorts(type,order){ var oJsList = document.getElementById("jsTable"); var aTr = oJsList.getElementsByTagName("tr"); var len = aTr.length; var col; switch(type) { case 0: col = 0; break; case 1: col = 1; break; case 2: col = 2; break; case 3: col = 3; break; } aTr = Array.prototype.slice.call(aTr); console.log(col) if(col == 0){ aTr.sort(function (val1,val2){ var v1 = val1.getElementsByTagName("td")[col].innerText; var v2 = val2.getElementsByTagName("td")[col].innerText; if (order == "asc") { return v2.localeCompare(v1); } else { return v1.localeCompare(v2); } }) }else{ aTr.sort(function (val1,val2) { var v1 = parseFloat(val1.getElementsByTagName("td")[col].innerText); var v2 = parseFloat(val2.getElementsByTagName("td")[col].innerText); if (order == "asc") { return v1 - v2; } else { return v2 - v1; } }); } for (var i = 0; i < len; i++) { oJsList.appendChild(aTr[i]); } } var oJsTable = document.getElementById("jsThead"); var thList = oJsTable.getElementsByTagName('tr')[0].children; for(var s=0;s<thList.length;s++){ thList[s].index=s; thList[s].num = 1; thList[s].onclick=function(){ if(this.num%2 != 0){ console.log(this.index) sorts(this.index,'asc'); this.num = this.num+1; }else{ sorts(this.index,'desc'); this.num = this.num-1; } } }</script>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>sort</title> </head> <body> <table> <Thead> <th id='name'>姓名</td> <th id='power'>力量</td> <th id='speed'>敏捷</td> <th id='smart'>智力</td> </Thead> <tr> <td>德鲁伊</td> <td>17</td> <td>24</td> <td>13</td> </tr> <tr> <td>月之骑士</td> <td>15</td> <td>22</td> <td>16</td> </tr> <tr> <td>众神之王</td> <td>19</td> <td>15</td> <td>14</td> </tr> <tr> <td>流浪剑客</td> <td>23</td> <td>15</td> <td>14</td> </tr> </table> <script type="text/javascript"> var flag=1; var objList=[]; var trs=document.getElementsByTagName('tr'); // console.log(trs.length); for(var i=1;i<trs.length;i++){ var tds=trs[i].children; // var obj={}; var obj=[]; obj.push(tds[0].innerHTML); for(var j=1;j<tds.length;j++){ obj.push(parseInt(tds[j].innerHTML)); } objList.push(obj); } // console.log(objList); var theads=document.getElementsByTagName('Thead')[0]; theads.onclick=function(){ var event=event||window.event; var target=event.target||event.srcElement; switch(target.id){ case 'name': mySort(objList,0);break; case 'power': mySort(objList,1);break; case 'speed': mySort(objList,2);break; case 'smart': mySort(objList,3);break; default: break; } flag=-1*flag; render(); } function render(){ for(var i=1;i<trs.length;i++){ var tds_=trs[i].children; for(var j=0;j<tds.length;j++){ console.log(i+':'+objList[i-1][j]); tds_[j].innerHTML=objList[i-1][j]; } } } function mySort(arr,index){ // var index_=index; arr.sort(function (v1,v2){ // console.log('i:'+index); return flag*(v1[index]-v2[index]); }); console.log(arr); return arr; } </script> </body> </html>
排序算法
Js的Dom和事件相关操作。