javascript 表格排序方法

來源:互聯網
上載者:User
提示:您可以先修改部分代碼再運行

<html> <head> <title>二維排序表格</title> <style type="text/css"> * { font-family: Tahoma, Arial, Serif; font-size: 14; } body { text-align: center; min-width: 760px; } #main { width: 720px; margin: 0 auto; text-align: left; } p { border-bottom: dotted 1px #e63; font-size: 24px; font-family: Arial; font-weight: bold; } /* 設定表格樣式 */table { text-align: center; border: solid 1px #4682b4; width: 80%; background-color: #4682b4; } th { background-color: #6495ed; cursor: pointer; color: White; height: 30px; } /* 配上儲存格樣式以後,表格呈現出細線的邊框 */td { background-color: #fff; padding: 4px; } /* 深藍色(滑鼠經過) */.activecol { background-color: #005eae; } </style> <script type="text/javascript"> //------------以下是對二維數組的初始化與顯示操作---------- // 記錄二維數組 var twoArr = new Array(5); // 初始化二維數組 function initTwoArr() { for (var i = 0; i < twoArr.length; i++) { twoArr[i] = new Array(6); for (var j = 0; j < 6; j++) { twoArr[i][j] = getRandom(); } } showTwoArrOnTable(); } // 得到0-100內的隨機數 function getRandom() { var result = Math.round(Math.random() * 100 - 1); return result; } // 把二維表顯示在table中 function showTwoArrOnTable() { if (twoArr == null || twoArr.length < 1) { alert("數組初始化失敗!"); return; } // 設定td的數值 var allTd = document.getElementById("tb").getElementsByTagName("td"); var tdIndex = 0; for (var i = 0; i < twoArr.length; i++) { // 為每個儲存格設定id,格式 1-2 與數組的下標對應的 var idValue; for (var j = 0; j < twoArr[i].length; j++) { idValue = i.toString() + "-" + j.toString(); allTd[tdIndex].id = idValue; allTd[tdIndex].innerHTML = twoArr[i][j]; tdIndex++; } } } //------------以下是對table的操作---------- // 記錄當前列(數字型)與當前的排序次序(布爾型) var currentCol; var currentReverse; // 根據(col-列索引值)排序 function sortColumn(col) { // 判斷是否要對數組倒序 var doReverse; // 如果再次單擊當前列,則設定逆序的標誌,並記錄排序的方式(正序/逆序) if (col == currentCol) { doReverse = true; currentReverse = !currentReverse; } else { currentReverse = false; } // 記錄當前排序的欄位 currentCol = col; // 把table的所有行放入數組中 var tb = document.getElementById("tb"); var tbody = tb.rows[1].parentNode; var allTr = new Array(); for (var i = 1; i < tb.rows.length; i++) { allTr.push(tb.rows[i]); } if (doReverse) { allTr.reverse(); } else { allTr.sort(createComp(col)); } // 把排好序的行集合加入到table中 var frag = document.createDocumentFragment(); for (var i = 0; i < allTr.length; i++) { frag.appendChild(allTr[i]); } tbody.appendChild(frag); } // 根據(col-列索引值)返回一個定序 function createComp(col) { // 比較函數(正序) var compFunc = function(trA, trB) { // 取出要比較的值 var valA = trA.cells[col].firstChild.nodeValue.trim(); var valB = trB.cells[col].firstChild.nodeValue.trim() return comp(valA, valB); }; return compFunc; } // 對string對象進行擴充,去除字串兩端的空格 String.prototype.trim = function() { var reg = /^s+|s+$/g; return this.replace(reg, ""); } // 預設比較方式(升序) function comp(a, b) { a = parseInt(a); b = parseInt(b); return (a < b) ? -1 : ((a > b) ? 1 : 0); } var desc = false; // 預設為升序 // 根據傳入的l列排序table function sortRow(rowTh) { // 找出rowIndex; var rowIndex; var tbody = document.getElementById("tb").childNodes[1]; var allTr = tbody.childNodes; for (var i = 0; i < allTr.length; i++) { if (rowTh == allTr[i].firstChild) { rowIndex = i; break; } } // 對數組進行排序,再填充到table arrRowSortFunc(rowIndex, desc); desc = !desc; showTwoArrOnTable(); } // 對twoArr進行分組冒泡排序 function arrRowSortFunc(rowIndex, isDesc) { var row = twoArr[rowIndex]; for (var i = 0; i < row.length; i++) { for (var j = 0; j < row.length - i - 1; j++) { if (isDesc) { // 如果是逆序 if (row[j] < row[j + 1]) { // 交換當前數組的同時要交換對應列的其他資料 var tmp = row[j]; row[j] = row[j + 1]; row[j + 1] = tmp; for (var m = 0; m < twoArr.length; m++) { if (m != rowIndex) { var tmp2 = twoArr[m][j]; twoArr[m][j] = twoArr[m][j + 1]; twoArr[m][j + 1] = tmp2; } } } } else { // 升序 if (row[j] > row[j + 1]) { // 交換當前數組的同時要交換對應列的其他資料 var tmp = row[j]; row[j] = row[j + 1]; row[j + 1] = tmp; for (var m = 0; m < twoArr.length; m++) { if (m != rowIndex) { var tmp2 = twoArr[m][j]; twoArr[m][j] = twoArr[m][j + 1]; twoArr[m][j + 1] = tmp2; } } } } } } } // 設定滑鼠經過表頭樣式 function setActiveCol(activeCol) { activeCol.className = "activecol"; } function setLoseFocusCol(col) { col.className = ""; } // 當按一下滑鼠cell儲存格時,把目前的儲存格設定為input輸入框 function inputValue(cell) { var oldValue = cell.innerHTML.trim(); // 如果已經雙擊過了,則不做任何動作 var reg = /<input/gi; if (reg.test(oldValue)) return; // 否則的話,則改為輸入框 var input = "<input type='text' value='" + oldValue + "' onblur='saveChange(this," + oldValue + ")' " + " />"; cell.innerHTML = input; // 把游標移到input內 cell.firstChild.focus(); } // 儲存改變的值 function saveChange(input, voldValue) { // 記錄輸入框的值 var newValue = input.value; if (newValue == "" || isNaN(newValue)) { // 如果輸入的值不是數字或者為空白,則還原為原始值 newValue = voldValue; } // 處理儲存格id,則可以對應數組下標 var arrIndex = input.parentNode.id; var reg = /-/; var arrIndexAB = arrIndex.split(reg); var arrIndexA = arrIndexAB[0]; var arrIndexB = arrIndexAB[1]; // 更改對應數組的值 twoArr[arrIndexA][arrIndexB] = newValue; // 更改儲存格的顯示 input.parentNode.innerHTML = newValue; } function showMe() { alert("作者:葉志雄"); } </script> </head> <body onload="initTwoArr()"> <div id="main"> <p> 二維排序表格</p> <table id="tb" border="0" cellpadding="1" cellspacing="1"> <thead> <tr> <th> </th> <th onclick="sortColumn(1)" onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)"> 列1 </th> <th onclick="sortColumn(2)" onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)"> 列2 </th> <th onclick="sortColumn(3)" onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)"> 列3 </th> <th onclick="sortColumn(4)" onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)"> 列4 </th> <th onclick="sortColumn(5)" onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)"> 列5 </th> <th onclick="sortColumn(6)" onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)"> 列6 </th> </tr> </thead> <tbody> <tr> <th onclick="sortRow(this)" onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)"> 行1 </th> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> </tr> <tr> <th onclick="sortRow(this)" onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)"> 行2 </th> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> </tr> <tr> <th onclick="sortRow(this)" onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)"> 行3 </th> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> </tr> <tr> <th onclick="sortRow(this)" onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)"> 行4 </th> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> </tr> <tr> <th onclick="sortRow(this)" onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)"> 行5 </th> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> <td onmousemove="setActiveCol(this)" onmouseout="setLoseFocusCol(this)" ondblclick="inputValue(this)"> </td> </tr> </tbody> </table> <div > 說明:<br> 1.排序功能:單擊行表頭或列表頭則進行正序排序;若再次單擊,則進行逆序;<br> 2.修改功能:雙擊某個儲存格,則可進行輸入操作,當輸入框失去焦點時,則新資料被儲存;<br> 3.隨機功能:每次重新整理頁面,表格中的資料都不一樣;<br> </div> </div> </body> </html>
提示:您可以先修改部分代碼再運行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.