2019年3月31日 星期日

Javascript 做簡單的Table Sorting

用 Javascript 作 Table Sorting

這裡寫 2 種自己的做法

第一種 w3schools

function sortTable() {
  var table, rows, switching, i, x, y, shouldSwitch;
  table = document.getElementById("myTable");
  switching = true;
  /* Make a loop that will continue until
  no switching has been done: */
  while (switching) {
    // Start by saying: no switching is done:
    switching = false;
    rows = table.rows;
    /* Loop through all table rows (except the
    first, which contains table headers): */
    for (i = 1; i < (rows.length - 1); i++) {
      // Start by saying there should be no switching:
      shouldSwitch = false;
      /* Get the two elements you want to compare,
      one from current row and one from the next: */
      x = rows[i].getElementsByTagName("TD")[0];
      y = rows[i + 1].getElementsByTagName("TD")[0];
      // Check if the two rows should switch place:
      if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
        // If so, mark as a switch and break the loop:
        shouldSwitch = true;
        break;
      }
    }
    if (shouldSwitch) {
      /* If a switch has been marked, make the switch
      and mark that a switch has been done: */
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
    }
  }
}
第二種 自己亂寫的
function sort_table(col){
     var i, j, arr = new Array(), arr2 = new Array();
     var table = document.getElementById("mytable");
     var rows  = table.rows;
     // 從 1 開始,因為 0 是欄位的標題 (th)) 
     for (i = 1; i < rows.length; i++) {
          // 得 TD 內 innerHTML,加 i 是因應 innerHTML 完全一樣的 CASE
          arr[i-1] = rows[i].getElementsByTagName("TD")[col].innerHTML.toLowerCase() + i;
          // 用 MAP 紀錄 
          arr2[arr[i-1]] = i;
     }
     arr.sort();
     // 利用 insertBefore 來重新排序
     // 缺點有可能會影響 map 裡存的 index
     // 所以要記錄以移動的 cell
     var index = new Array();
     for(i = 0; i < arr.length; i++){
          var offset = 0;
          // 看過往 cell 的移動是否會影響現在的 index
          for(j = 0; j < i; j++){
               if(arr2[arr[i]] < index[j])offset++;
          }
          rows[1].parentNode.insertBefore(rows[arr2[arr[i]]+offset], rows[1]);
          // 紀錄 cell
          index[i] = arr2[arr[i]];
     }
}


參考資料 :
https://www.w3schools.com/howto/howto_js_sort_table.asp

Related Posts:

  • 重新踏入網頁開發 (6) - Express - 5  Express - sendFile()     用 sendFile 把寫好的 HTML 傳出去,然後準備開始前端的開發。 import express from 'express' import path from 'path'; // 建立 express 這 module var app = express() const port = 8888 cons… Read More
  • 重新踏入網頁開發 (6) - Express - 4  Express - App v.s. Router     Express 通常都是用 app.Method 來做 request 的 routing。而 Express 4.0 加入的 router 則可視為可以掛載的迷你 app。 import express from 'express' var app = express() var router = express.Router… Read More
  • 重新踏入網頁開發 (1) - Nodejs 前言     網頁開發是一個對我而言還蠻陌生的東西, 因為我都是自學所以總是得過且過。自學通常就會拿手邊資源也就是公司內部系統的 Code。公司的 Code 較為古早, 不外乎 3 個基本的元素, http server(apache), PHP, 前端(html, css, javascript)。 此時的我靠著 w3schools 就能完成所有事情。既然重新踏入開發網頁, 就去學最新的東西 (目標是 react), 即使… Read More
  • 重新踏入網頁開發 (7) - React  React - Introduction     A JavaScript library for building user interfaces. React 在 MVC 分類上屬於 View,也就是主要用來開發前端。比起直接使用 npx create-react-app,我這裡想紀錄一些較簡單且原始的 React Sample Code。 原本既有的程式碼 <!-- Some HT… Read More
  • 重新踏入網頁開發 (5) - Blocking & Non-Blocking Blocking & Non-Blocking     Nodejs 為事件觸發的單一執行緒,不像 Apache + PHP 會自動開一個 Thread 去接每一個 Request。     Blocking Code 示範 (程式碼另有 index.js 請參照重新踏入網頁開發 (4) - Dependency injection) route.js function start_… Read More

0 意見:

張貼留言

Popular Posts