jeudi 12 août 2021

Sort table with checkbox

So I have two tables, the default table, and a table with checkboxes. The sorting function works well in the default table, but not in the table with checkboxes (the column that is sorted correctly is precisely the previous column). Here is the sort function that I use, anything wrong?

    sort(id) {
      if(this.isSortable(id)) {
        let table, rows, switching, i, x, y, shouldSwitch;
        const typeSort = this.changeTypeSort(id)
        table = document.getElementById(this.index);
        switching = true;
        while (switching) {
          switching = false;
          rows = table.rows;
          for (i = 1; i < (rows.length - 1); i++) {
            shouldSwitch = false;
            x = rows[i].getElementsByTagName("TD")[id];
            y = rows[i + 1].getElementsByTagName("TD")[id];
            if (typeSort == 'asc') {
              x = x.innerHTML.toLowerCase();
              y = y.innerHTML.toLowerCase();
              if (x.localeCompare(y, 'en', {numeric: true}) > 0) {
                shouldSwitch = true;
                break;
              }
            }
            if (typeSort == 'des') {
              x = x.innerHTML.toLowerCase();
              y = y.innerHTML.toLowerCase();
              if (x.localeCompare(y, 'en', {numeric: true}) < 0) {
                shouldSwitch = true;
                break;
              }
            }
        }
          if (shouldSwitch) {
            rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
            switching = true;
          }
        }
      }
    },



Aucun commentaire:

Enregistrer un commentaire