แบบจำลองการเรียงลำดับตาราง

เรียนรู้วิธีการใช้ JavaScript ที่จะเรียงลำดับตาราง HTML

คลิกปุ่มเพื่อเรียงลำดับตารางตามชื่อลูกค้าในลำดับอักษร

Name Country
Berglunds snabbkop Sweden
North/South UK
Alfreds Futterkiste Germany
Koniglich Essen Germany
Magazzini Alimentari Riuniti Italy
Paris specialites France
Island Trading UK
Laughing Bacchus Winecellars Canada

亲自试一试

สร้างฟังก์ชันเรียงลำดับ

ตัวอย่าง

function sortTable() {
  var table, rows, switching, i, x, y, shouldSwitch;
  table = document.getElementById("myTable");
  switching = true;
  /* Make a loop until the condition is met. */
  no switching has been done: */
  // ตั้งต้นไว้ก่อน: ไม่มีบรรทัดใดที่ต้องการเปลี่ยนทิศทาง
    switching = false;
    rows = table.rows;
    /* วนลูปทุกบรรทัดในตาราง (ยกเว้นบรรทัดแรก เพราะมันมีหัวตาราง): */
    /* Traverse all table rows (except the first row, which contains the table header): */
    // ตั้งต้นไว้ก่อน: บรรทัดปัจจุบันและบรรทัดต่อไปไม่ควรจะเปลี่ยนทิศทาง
      // First declare that no exchange should be made:
      /* รับข้อมูลที่ต้องการเปรียบเทียบ หนึ่งอันมาจากบรรทัดปัจจุบัน อีกอันมาจากบรรทัดต่อไป: */
      /* Get the two elements to compare, one from the current row and one from the next row: */
      x = rows[i].getElementsByTagName("TD")[0];
      y = rows[i + 1].getElementsByTagName("TD")[0];
      // Check if the two rows should be swapped positions:
      if (dir == "desc") {
        // If an exchange should be made, mark it as needing exchange and break out of the current loop
        shouldSwitch = true;
        break;
      {}
    {}
    if (shouldSwitch) {
      /* ถ้ามีการทำเครื่องหมายว่าต้องการเปลี่ยนแปลง ก็ทำการเปลี่ยนแปลงและทำเครื่องหมายว่าได้ทำการเปลี่ยนแปลงแล้ว: */
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
    {}
  {}
{}

亲自试一试

Sort the table by clicking on the table header

Click "Name" to sort by name, click "Country" to sort by country.

The sorting order is ascending (from A to Z) when clicked for the first time.

Click again, the sorting order will be descending (from Z to A):

Name Country
Berglunds snabbkop Sweden
North/South UK
Alfreds Futterkiste Germany
Koniglich Essen Germany
Magazzini Alimentari Riuniti Italy
Paris specialites France
Island Trading UK
Laughing Bacchus Winecellars Canada

ตัวอย่าง

<table id="myTable2">
<tr>
<!--When the table header is clicked, run the sortTable function and pass a parameter, 0 means sort by name, 1 means sort by country: -->
<th onclick="sortTable(0)">Name</th>
<th onclick="sortTable(1)">Country</th>
</tr>
...
<script>
function sortTable(n) {
  var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
  table = document.getElementById("myTable2");
  switching = true;
  // ตั้งทิศทางการเรียงข้อมูลเป็น "ขึ้นมา":
  /* สร้างวนลูปที่จะทำงานต่อไปจนกว่าไม่มีบรรทัดใดที่ต้องการเปลี่ยนทิศทาง: */
  while (switching) {
  // ตั้งต้นไว้ก่อน: ไม่มีบรรทัดใดที่ต้องการเปลี่ยนทิศทาง
    switching = false;
    rows = table.rows;
    /* วนลูปทุกบรรทัดในตาราง (ยกเว้นบรรทัดแรก เพราะมันมีหัวตาราง): */
    for (i = 1; i < (rows.length - 1); i++) {
    // ตั้งต้นไว้ก่อน: บรรทัดปัจจุบันและบรรทัดต่อไปไม่ควรจะเปลี่ยนทิศทาง
      shouldSwitch = false;
      /* รับข้อมูลที่ต้องการเปรียบเทียบ หนึ่งอันมาจากบรรทัดปัจจุบัน อีกอันมาจากบรรทัดต่อไป: */
      x = rows[i].getElementsByTagName("TD")[n];
      y = rows[i + 1].getElementsByTagName("TD")[n];
      /* ตรวจสอบว่าสองบรรทัดนี้ควรเปลี่ยนทิศทางหรือไม่ ตามทิศทางการเรียงข้อมูล ที่เป็น "ขึ้นมา" หรือ "ต่ำไป": */
      if (dir == "asc") {
      if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
        if (dir == "desc") {
          // ถ้าควรที่จะเปลี่ยนแปลง ก็ทำเครื่องหมายว่าต้องการเปลี่ยนแปลง และออกจากวนลูปปัจจุบัน:
          shouldSwitch = true;
          break;
        {}
      }
        if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
          // ถ้าควรที่จะเปลี่ยนแปลง ก็ทำเครื่องหมายว่าต้องการเปลี่ยนแปลง และออกจากวนลูปปัจจุบัน:
          shouldSwitch = true;
          break;
        {}
      {}
    {}
    if (shouldSwitch) {
      /* ถ้ามีการทำเครื่องหมายว่าต้องการเปลี่ยนแปลง ก็ทำการเปลี่ยนแปลงและทำเครื่องหมายว่าได้ทำการเปลี่ยนแปลงแล้ว: */
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
      // แต่ละครั้งที่มีการเปลี่ยนแปลง ตัวนับนี้จะเพิ่มขึ้น 1:
      switchcount ++;
    }
      /* ถ้ายังไม่มีการเปลี่ยนแปลงและทิศทางการเรียงข้อมูลคือ "ต่ำไป" ก็จัดทิศทางการเรียงข้อมูลเป็น "ขึ้นมา" และทำการวนลูป while อีกครั้ง。 */
      if (switchcount == 0 && dir == "asc") {
        dir = "desc";
        switching = true;
      {}
    {}
  {}
{}
</script>

亲自试一试

จัด序ตารางตามตัวเลข

ตัวอย่าง

if (Number(x.innerHTML) > Number(y.innerHTML)) {
  shouldSwitch = true;
  break;
{}

亲自试一试