laradb

script pour établir une bdd Ãà partir d'un tsv avec différent format - retour accueil

git clone git://bebou.netlib.re/laradb
Log | Files | Refs |

script.js (2002B)


      1 // https://www.w3schools.com/howto/howto_js_sort_table.asp
      2 function sortTable(n) {
      3 	n = n-1
      4   var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
      5   table = document.querySelector("table");
      6   switching = true;
      7   // Set the sorting direction to ascending:
      8   dir = "asc";
      9   /* Make a loop that will continue until
     10   no switching has been done: */
     11   while (switching) {
     12     // Start by saying: no switching is done:
     13     switching = false;
     14     rows = table.rows;
     15     /* Loop through all table rows (except the
     16     first, which contains table headers): */
     17     for (i = 1; i < (rows.length - 1); i++) {
     18       // Start by saying there should be no switching:
     19       shouldSwitch = false;
     20       /* Get the two elements you want to compare,
     21       one from current row and one from the next: */
     22       x = rows[i].getElementsByTagName("TD")[n];
     23       y = rows[i + 1].getElementsByTagName("TD")[n];
     24       /* Check if the two rows should switch place,
     25       based on the direction, asc or desc: */
     26       if (dir == "asc") {
     27         if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
     28           // If so, mark as a switch and break the loop:
     29           shouldSwitch = true;
     30           break;
     31         }
     32       } else if (dir == "desc") {
     33         if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
     34           // If so, mark as a switch and break the loop:
     35           shouldSwitch = true;
     36           break;
     37         }
     38       }
     39     }
     40     if (shouldSwitch) {
     41       /* If a switch has been marked, make the switch
     42       and mark that a switch has been done: */
     43       rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
     44       switching = true;
     45       // Each time a switch is done, increase this count by 1:
     46       switchcount ++;
     47     } else {
     48       /* If no switching has been done AND the direction is "asc",
     49       set the direction to "desc" and run the while loop again. */
     50       if (switchcount == 0 && dir == "asc") {
     51         dir = "desc";
     52         switching = true;
     53       }
     54     }
     55   }
     56 }