对 HTML 表格进行排序

问题描述 投票:0回答:1

我有一个包含多个列的 HTML 表格。我找到了一个 JSscript w3school 来对该表进行排序。我稍微修改了这个脚本,以便能够对仅包含数字的列进行排序。 除了 1 列包含这样的值 ' 548m / 1797ft' 之外,它运行良好。这种价值追求更高?!我真的不明白这个特定的专栏出了什么问题。

<TABLE id="table" class="resp">
            <thead>
                <TR>
                    <th onclick="sortTable(0)" class="sortable" scope="col"> Position </th>
                    <th onclick="sortTable(1)" class="sortable" scope="col"> Sommet </th>
                    <th onclick="sortTable(2)" class="sortable" scope="col"> Altitude </th>
                    
                </TR>
            </thead>
                <tr>
                        
                        <td data-label="Position"> 1 </td>
                        <td data-label="Sommet"> Mont Marcy </td>
                        <td data-label="Altitude"> 1629m / 5343ft </td>
                        
                </tr>
                <tr>
                        
                        <td data-label="Position"> 2 </td>
                        <td data-label="Sommet"> Mont Algonquin </td>
                        <td data-label="Altitude"> 1559m / 5114ft </td>                      
                </tr>
                <tr>
                        
                        <td data-label="Position"> 3 </td>
                        <td data-label="Sommet"> Mont Haystack </td>
                        <td data-label="Altitude"> 1510m / 4953ft </td>                         
                </tr>
                <tr>
                        
                        <td data-label="Position"> 4 </td>
                        <td data-label="Sommet"> Mont Skylight </td>
                        <td data-label="Altitude"> 1501m / 4923ft </td>
                        
                </tr>
                <tr>
                        
                        <td data-label="Position"> 5 </td>
                        <td data-label="Sommet"> Mont Whiteface</td>
                        <td data-label="Altitude">1483m / 4864ft </td>
 
                </tr>
                <tr>
 
                        <td data-label="Position"> 6 </td>
                        <td data-label="Sommet"> Mont Dix </td>
                        <td data-label="Altitude"> 1481m / 4858ft </td> 
                        
                </tr>
                <tr>
                        
                        <td data-label="Position"> 7  </td>
                        <td data-label="Sommet"> Mont Gray </td>
                        <td data-label="Altitude"> 1475m / 4838ft </td> 
                        
                </tr>
                <tr>
                        <td data-label="Position"> 8 </td>
                        <td data-label="Sommet"> Mont Iroquois </td>
                        <td data-label="Altitude"> 1475m / 4838ft </td> 
                        
                </tr>
                <tr>
                        
                        <td data-label="Position"> 9 </td>
                        <td data-label="Sommet"> Mont Iroquois </td>
                        <td data-label="Altitude"> 1001m / 3283ft </td> 
                        
                </tr>
                <tr>
                        
                        <td data-label="Position"> 10 </td>
                        <td data-label="Sommet"> Mont St-Bruno </td>
                        <td data-label="Altitude"> 548m / 1797ft </td>
                        
                </tr>
                <tr>
                        
                        <td data-label="Position"> 11 </td>
                        <td data-label="Sommet"> Mont Royal </td>
                        <td data-label="Altitude"> 472m / 1548ft </td>
                        
                </tr>
                    
    </table>

我删除了几列,因为它们不涉及这个问题

还有脚本部分

function sortTable(n) {
    var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
    table = document.getElementById("table");
    switching = true;
    // Set the sorting direction to ascending:
    dir = "asc";
    /* Make a loop that will continue until
    no switching has been done: */
    while (switching) {
      // Start by saying: no switching is done:
      switching = false;
      rows = table.rows;
      /* Loop through all table rows (except the
      first, which contains table headers): */
      for (i = 1; i < (rows.length - 1); i++) {
        // Start by saying there should be no switching:
        shouldSwitch = false;
        /* Get the two elements you want to compare,
        one from current row and one from the next: */
        x = rows[i].getElementsByTagName("TD")[n];
        y = rows[i + 1].getElementsByTagName("TD")[n];
        /* Check if the two rows should switch place,
        based on the direction, asc or desc: */
        if (!isNaN(x.innerHTML)) {
            // NUMERIC
            if (dir == "asc") {
                if (Number(x.innerHTML) > Number(y.innerHTML)) {
                    shouldSwitch = true;
                    break;
                  }
              } else if (dir == "desc") {
                if (Number(x.innerHTML) < Number(y.innerHTML)) {
                    shouldSwitch = true;
                    break;
                  }
              }

        } else {

            // ALPHABETIC
        if (dir == "asc") {
            if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
              // If so, mark as a switch and break the loop:
              shouldSwitch = true;
              break;
            }
          } else if (dir == "desc") {
            if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
              // If so, mark as a switch and break the loop:
              shouldSwitch = true;
              break;
            }
          }
        }

        }  // FOR LOOP
            
        
      if (shouldSwitch) {
        /* If a switch has been marked, make the switch
        and mark that a switch has been done: */
        rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
        switching = true;
        // Each time a switch is done, increase this count by 1:
        switchcount ++;
      } else {
        /* If no switching has been done AND the direction is "asc",
        set the direction to "desc" and run the while loop again. */
        if (switchcount == 0 && dir == "asc") {
          dir = "desc";
          switching = true;
        }
      }
    }
  }

我认为这个特定的列会被视为字符串,但它似乎不是......所以我真的不知道该怎么做。

P.S.:抱歉我的英语不好,这不是我的语言

javascript html sorting
1个回答
0
投票

在将字符串传递给排序函数之前,您需要将其转换为数字。 这是一个实现:

function sortTable(n) {
    var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
    table = document.getElementById("table");
    switching = true;
    // Set the sorting direction to ascending:
    dir = "asc";
    /* Make a loop that will continue until
    no switching has been done: */
    while (switching) {
      // Start by saying: no switching is done:
      switching = false;
      rows = table.rows;
      console.dir(rows)
      /* Loop through all table rows (except the
      first, which contains table headers): */
      for (i = 1; i < (rows.length - 1); i++) {
        // Start by saying there should be no switching:
        shouldSwitch = false;
        /* Get the two elements you want to compare,
        one from current row and one from the next: */
        x = rows[i].getElementsByTagName("TD")[n];
        y = rows[i + 1].getElementsByTagName("TD")[n];
        /* Check if the two rows should switch place,
        based on the direction, asc or desc: */
        if (!isNaN(x.innerHTML)) {
            // NUMERIC
            if (dir == "asc") {
                if (Number(x.innerHTML) > Number(y.innerHTML)) {
                    shouldSwitch = true;
                    break;
                  }
              } else if (dir == "desc") {
                if (Number(x.innerHTML) < Number(y.innerHTML)) {
                    shouldSwitch = true;
                    break;
                  }
              }

        } else {
        if (/m \/ /.test(x.innerHTML)) { // Altitude
            if (dir == "asc") {
                if (Number(x.innerHTML.split('m')[0]) > Number(y.innerHTML.split('m')[0])) {
                    // If so, mark as a switch and break the loop:
                    shouldSwitch = true;
                    break;
                }
            } else if (dir == "desc") {
                if (Number(x.innerHTML.split('m')[0]) < Number(y.innerHTML.split('m')[0])) {
                    // If so, mark as a switch and break the loop:
                    shouldSwitch = true;
                    break;
                }
            }
        } else if (dir == "asc") { // ALPHABETIC
            if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
              // If so, mark as a switch and break the loop:
              shouldSwitch = true;
              break;
            }
          } else if (dir == "desc") {
            if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
              // If so, mark as a switch and break the loop:
              shouldSwitch = true;
              break;
            }
          }
        }

        }  // FOR LOOP
            
        
      if (shouldSwitch) {
        /* If a switch has been marked, make the switch
        and mark that a switch has been done: */
        rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
        switching = true;
        // Each time a switch is done, increase this count by 1:
        switchcount ++;
      } else {
        /* If no switching has been done AND the direction is "asc",
        set the direction to "desc" and run the while loop again. */
        if (switchcount == 0 && dir == "asc") {
          dir = "desc";
          switching = true;
        }
      }
    }
}```
© www.soinside.com 2019 - 2024. All rights reserved.