【问题标题】:I'm having problems creating a Dynamic Table?我在创建动态表时遇到问题?
【发布时间】:2020-05-06 12:49:23
【问题描述】:

对于一个研究项目,我必须使用 JavaScript 创建一个动态表。我向一些朋友寻求帮助,但由于他们不是计算机科学家(或任何其他方向的人),我只知道使用 AJAX 和 DOM 很好。并使用以下 javascipt(src=w3schools 和堆栈)

问题 它正在排序,但不是字母或数字。它可能是表格一列中包含的图片中的 url,但对我来说它看起来是随机的。

如何创建动态工作排序表??

<table id="phones">
    <thead>
        <th><h2>Brand<br><p>Click to sort</p>
          <p><button onclick="sortTable()"></button></p></h2></th>
        <th><h2>IMG</h2></th>
        <th><h2>Model<br><p>Click to sort</p>
          <p><button onclick="sortTable()"></button></p></h2></th>
        <th><h2>Os<br><p>Click to sort</p>
          <p><button onclick="sortTable()"></button></p></h2></th>
        <th><h2>Screensize<br><p>Click to sort</p>
          <p><button onclick="sortTable()"></button></p></h2></th>
        <th><h2>Price<br><p>Click to sort</p>
          <p><button onclick="sortTable()"></button></p></h2></th> 
    </thead>  ``` 

function sortTable() {
  var table, rows, switching, i, x, y, shouldSwitch;
  table = document.getElementById('phones');
  switching = true;

  while (switching) { 
    switching = false;
    rows = table.rows;
    for (i = 1; i < (rows.length - 1); i++) {
      shouldSwitch = false;
      x = rows[i].getElementsByTagName("td")[0];
      y = rows[i + 1].getElementsByTagName("td")[0];
      if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
        shouldSwitch = true;
        break;
       }
    }
    if (shouldSwitch) {      
      rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
      switching = true;
          }
  }
}








【问题讨论】:

    标签: javascript html dynamic tablesorter tablesort


    【解决方案1】:

    getElementsByTagName("TD")[0] 表示访问 first TD 元素,但您并不总是想要第一个。相反,您需要添加一个colNumparameter,它可用于在调用函数时定义的方括号中放置不同的数字。您还需要以不同于字母排序的方式处理数字排序。当您将数字视为字符串时,它们的排序方式不同,如下所示:

    console.log( ["19", "20", "3", "35", "1", "25", "300"].sort() );

    所以添加第二个参数,告诉函数它应该使用字母排序还是数字排序。然后,在函数中,检测参数并进行相应的排序,如下所示:

    function sortTable(colNum, sortType) {
      var table, rows, switching, i, x, y, shouldSwitch;
      table = document.getElementById("phones");
      switching = true;
    
      while (switching) {
        switching = false;
        rows = table.rows;
        for (i = 1; i < (rows.length - 1); i++) {
          shouldSwitch = false;
          x = rows[i].getElementsByTagName("TD")[colNum];
          y = rows[i + 1].getElementsByTagName("TD")[colNum];
          if (sortType == "alphabetical") {
            if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
              shouldSwitch = true;
              break;
            }
          } else {
            if (Number(x.innerHTML) > Number(y.innerHTML)) {
              shouldSwitch = true;
              break;
            }
          }
        }
        if (shouldSwitch) {
          rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
          switching = true;
        }
      }
    }
    <table id="phones">
      <tr>
        <th><button onclick="sortTable(0, 'alphabetical');">Sort this column</button></th>
        <th><button onclick="sortTable(1, 'alphabetical');">Sort this column</button></th>
        <th><button onclick="sortTable(2, 'numerical');">Sort this column</button></th>
      </tr>
      <tr>
        <td>C</td>
        <td>A</td>
        <td>100</td>
      </tr>
      <tr>
        <td>D</td>
        <td>Z</td>
        <td>1</td>
      </tr>
      <tr>
        <td>E</td>
        <td>D</td>
        <td>19</td>
      </tr>
      <tr>
        <td>A</td>
        <td>C</td>
        <td>55</td>
      </tr>
    </table>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多