trentjs

Table Row Sorting w3 schools example

Jan 28th, 2021 (edited)
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Sort a HTML Table Alphabetically</title>
  5. <style>
  6. table {
  7.   border-spacing: 0;
  8.   width: 100%;
  9.   border: 1px solid #ddd;
  10. }
  11.  
  12. th {
  13.   cursor: pointer;
  14. }
  15.  
  16. th, td {
  17.   text-align: left;
  18.   padding: 16px;
  19. }
  20.  
  21. tr:nth-child(even) {
  22.   background-color: #f2f2f2
  23. }
  24. </style>
  25. </head>
  26. <body>
  27.  
  28. <p><strong>Click the headers to sort the table.</strong></p>
  29. <p>The first time you click, the sorting direction is ascending (A to Z).</p>
  30. <p>Click again, and the sorting direction will be descending (Z to A):</p>
  31.  
  32. <table id="myTable">
  33.   <tr>
  34.    <!--When a header is clicked, run the sortTable function, with a parameter, 0 for sorting by names, 1 for sorting by country:-->  
  35.     <th onclick="sortTable(0)">Name</th>
  36.     <th onclick="sortTable(1)">Country</th>
  37.   </tr>
  38.   <tr>
  39.     <td>Berglunds snabbkop</td>
  40.     <td>Sweden</td>
  41.   </tr>
  42.   <tr>
  43.     <td>North/South</td>
  44.     <td>UK</td>
  45.   </tr>
  46.   <tr>
  47.     <td>Alfreds Futterkiste</td>
  48.     <td>Germany</td>
  49.   </tr>
  50.   <tr>
  51.     <td>Koniglich Essen</td>
  52.     <td>Germany</td>
  53.   </tr>
  54.   <tr>
  55.     <td>Magazzini Alimentari Riuniti</td>
  56.     <td>Italy</td>
  57.   </tr>
  58.   <tr>
  59.     <td>Paris specialites</td>
  60.     <td>France</td>
  61.   </tr>
  62.   <tr>
  63.     <td>Island Trading</td>
  64.     <td>UK</td>
  65.   </tr>
  66.   <tr>
  67.     <td>Laughing Bacchus Winecellars</td>
  68.     <td>Canada</td>
  69.   </tr>
  70. </table>
  71.  
  72. <script>
  73.  
  74. // https://www.w3schools.com/howto/howto_js_sort_table.asp
  75. // https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_sort_table_desc
  76.  
  77. function sortTable(n) {
  78.   var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
  79.   table = document.getElementById("myTable");
  80.   switching = true;
  81.   //Set the sorting direction to ascending:
  82.   dir = "asc";
  83.   /*Make a loop that will continue until
  84.   no switching has been done:*/
  85.   while (switching) {
  86.     //start by saying: no switching is done:
  87.     switching = false;
  88.     rows = table.rows;
  89.     /*Loop through all table rows (except the
  90.     first, which contains table headers):*/
  91.     for (i = 1; i < (rows.length - 1); i++) {
  92.       //start by saying there should be no switching:
  93.       shouldSwitch = false;
  94.       /*Get the two elements you want to compare,
  95.       one from current row and one from the next:*/
  96.       x = rows[i].getElementsByTagName("TD")[n];
  97.       y = rows[i + 1].getElementsByTagName("TD")[n];
  98.       /*check if the two rows should switch place,
  99.       based on the direction, asc or desc:*/
  100.       if (dir == "asc") {
  101.         if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
  102.           //if so, mark as a switch and break the loop:
  103.           shouldSwitch= true;
  104.           break;
  105.         }
  106.       } else if (dir == "desc") {
  107.         if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
  108.           //if so, mark as a switch and break the loop:
  109.           shouldSwitch = true;
  110.           break;
  111.         }
  112.       }
  113.     }
  114.     if (shouldSwitch) {
  115.       /*If a switch has been marked, make the switch
  116.       and mark that a switch has been done:*/
  117.       rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
  118.       switching = true;
  119.       //Each time a switch is done, increase this count by 1:
  120.       switchcount ++;      
  121.     } else {
  122.       /*If no switching has been done AND the direction is "asc",
  123.       set the direction to "desc" and run the while loop again.*/
  124.       if (switchcount == 0 && dir == "asc") {
  125.         dir = "desc";
  126.         switching = true;
  127.       }
  128.     }
  129.   }
  130. }
  131. </script>
  132.  
  133. </body>
  134. </html>
Add Comment
Please, Sign In to add comment