كيفية ترتيب الجدول

تعلم كيفية استخدام 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

تجربة بنفسك

إنشاء وظيفة ترتيب

مثال

وظيفة ترتيب الجدول() {
  تعريف جدول، سطر، التبديل، i، x، y، يجب التبديل;
  table = document.getElementById("myTable");
  switching = true;
  /* Make a loop until the condition is met. */
  no switching has been done: */
  while (switching) {
    // أولاً، تقرير: لا يوجد سطر يجب تبديله:
    switching = false;
    rows = table.rows;
    /* Traverse all table rows (except the first row, which contains the header): */
    للحصول على (i = 1; i < (rows.length - 1); i++) {
      // First declare that no exchange should be made:
      shouldSwitch = false;
      /* Get the two elements to be compared, 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:
      إذا (dir == "desc") {
        // If an exchange should be made, mark it as needing an exchange and exit the current loop
        shouldSwitch = true;
        break;
      }
    }
    إذا (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.

When clicked 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;
  // قم بتعيين اتجاه الترتيب إلى تصاعدي:}
  dir = "asc";
  /* أنشئ دورة ستستمر حتى لا يوجد سطر يجب تبديله: */
  while (switching) {
    // أولاً، تقرير: لا يوجد سطر يجب تبديله:
    switching = false;
    rows = table.rows;
    /* استعرض جميع سطور الجدول (استثنًا السطر الأول، لأنه يحتوي على عناوين الجدول): */
    للحصول على (i = 1; i < (rows.length - 1); i++) {
      // أولاً، تقرير: لا يجب تبادل السطر الحالي والسطر التالي:
      shouldSwitch = false;
      x = rows[i].getElementsByTagName("TD")[n];
      y = rows[i + 1].getElementsByTagName("TD")[n];
      /* تحقق مما إذا كان يجب تبادل هذه السطور أو لا بناءً على اتجاه الترتيب، تصاعدي أو تنازلي: */
      إذا (dir == "asc") {
      إذا (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
        إذا (dir == "desc") {
          // إذا كان يجب التبادل، فقم بتعيينه بأنه ضروري والخروج من الدورة الحالية:
          shouldSwitch = true;
          break;
        }
      }
        إذا (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
          // إذا كان يجب التبادل، فقم بتعيينه بأنه ضروري والخروج من الدورة الحالية:
          shouldSwitch = true;
          break;
        }
      }
    }
    إذا (shouldSwitch) {
      /* إذا تم تسمية هذا التبادل بأنه ضروري، فقم بإجراء التبادل وتسميته بأنه تم إجراء التبادل: */
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
      // يتم زيادة هذا المعادلة بـ 1 عند كل تبادل:
      switchcount ++;
    }
      /* إذا لم يتم إجراء تبادل وأيضًا كان اتجاه الترتيب "تصاعدي"، فإن قم بتعيين اتجاه الترتيب إلى "تنازلي" وإعادة تشغيل الدورة while مرة أخرى. */
      إذا (switchcount == 0 && dir == "asc") {
        dir = "desc";
        switching = true;
      }
    }
  }
}
</script>

تجربة بنفسك

ترتيب الجدول حسب الرقم

مثال

إذا (Number(x.innerHTML) > Number(y.innerHTML)) {
  shouldSwitch = true;
  break;
}

تجربة بنفسك