如何使用javascript对html中的表格进行排序?

时间:2017-01-10 08:38:24

标签: javascript html sorting

我在工作场所遇到这个问题,我觉得这张桌子不适合我。我想对这个表进行排序,就像当我单击其中一个标题时,它从该标题类别中排序。在示例中,我有这个HTML代码:

<html>
<head>
</head>
<body>
    <table id="AirflightDetail">
        <thead>
            <tr>
                <th class="col-lg-2 text-center">Airline</th>
                <th class="col-lg-2 text-center">Depart</th>
                <th class="col-lg-2 text-center">Arrival</th>
                <th class="col-lg-2 text-center">Duration</th>
                <th class="col-lg-2 text-center">Price</th>
            </tr>
        </thead>

        <tbody class="table-result-list">
            <tr>
                <td class="text-center" min-width="87px">
                    <span>Airline X</span>
                    <br>
                    <p>AX-006</p>
                </td>
                <td class="text-center" min-width="100px">
                        <strong>13:10</strong>
                        <br>
                        <span>Singapore</span>
                </td>
                <td class="text-center" min-width="100px">
                        <strong>14:25</strong>
                        <br>
                        <span>Bangkok</span>
                </td>
                <td class="text-center" min-width="65px">
                        <strong>1hour 15m</strong>
                        <br>
                        <span>Direct</span>
                </td>
                <td class="text-right amount" min-width="77px">
                        <strong>$327</strong>
                </td>
            </tr>
        </tbody>

        <tbody class="table-result-list">
            <tr>
                <td class="text-center" min-width="87px">
                    <span>Airline H</span>
                    <br>
                    <p>AH-999</p>
                </td>
                <td class="text-center" min-width="100px">
                        <strong>18:30</strong>
                        <br>
                        <span>Singapore</span>
                </td>
                <td class="text-center" min-width="100px">
                        <strong>19:45</strong>
                        <br>
                        <span>Bangkok</span>
                </td>
                <td class="text-center" min-width="65px">
                        <strong>1hour 15m</strong>
                        <br>
                        <span>Direct </span>
                </td>
                <td class="text-right amount" min-width="77px">
                        <strong>$273</strong>
                </td>
            </tr>
        </tbody>

        <tbody class="table-result-list">
            <tr>
                <td class="text-center" min-width="87px">
                    <span>Airline K</span>
                    <br>
                    <p>AK-100</p>
                </td>
                <td class="text-center" min-width="100px">
                        <strong>12:05</strong>
                        <br>
                        <span>Singapore</span>
                </td>
                <td class="text-center" min-width="100px">
                        <strong>14:20</strong>
                        <br>
                        <span>Bangkok</span>
                </td>
                <td class="text-center" min-width="65px">
                        <strong>2hour</strong>
                        <br>
                        <span>Direct </span>
                </td>
                <td class="text-right amount" min-width="77px">
                        <strong>$273</strong>
                </td>
            </tr>
        </tbody>
    </table>
</body>
</html>

当我点击示例航空公司中的标题标题时,该表格从航空公司中排序如下:

<html>
<head>
</head>
<body>
    <table id="AirflightDetail">
        <thead>
            <tr>
                <th class="col-lg-2 text-center">Airline</th>
                <th class="col-lg-2 text-center">Depart</th>
                <th class="col-lg-2 text-center">Arrival</th>
                <th class="col-lg-2 text-center">Duration</th>
                <th class="col-lg-2 text-center">Price</th>
            </tr>
        </thead>

        <tbody class="table-result-list">
            <tr>
                <td class="text-center" min-width="87px">
                    <span>Airline H</span>
                    <br>
                    <p>AH-999</p>
                </td>
                <td class="text-center" min-width="100px">
                        <strong>18:30</strong>
                        <br>
                        <span>Singapore</span>
                </td>
                <td class="text-center" min-width="100px">
                        <strong>19:45</strong>
                        <br>
                        <span>Bangkok</span>
                </td>
                <td class="text-center" min-width="65px">
                        <strong>1hour 15m</strong>
                        <br>
                        <span>Direct </span>
                </td>
                <td class="text-right amount" min-width="77px">
                        <strong>$273</strong>
                </td>
            </tr>
        </tbody>

        <tbody class="table-result-list">
            <tr>
                <td class="text-center" min-width="87px">
                    <span>Airline K</span>
                    <br>
                    <p>AK-100</p>
                </td>
                <td class="text-center" min-width="100px">
                        <strong>12:05</strong>
                        <br>
                        <span>Singapore</span>
                </td>
                <td class="text-center" min-width="100px">
                        <strong>14:20</strong>
                        <br>
                        <span>Bangkok</span>
                </td>
                <td class="text-center" min-width="65px">
                        <strong>2hour</strong>
                        <br>
                        <span>Direct </span>
                </td>
                <td class="text-right amount" min-width="77px">
                        <strong>$273</strong>
                </td>
            </tr>
        </tbody>

         <tbody class="table-result-list">
            <tr>
                <td class="text-center" min-width="87px">
                    <span>Airline X</span>
                    <br>
                    <p>AX-006</p>
                </td>
                <td class="text-center" min-width="100px">
                        <strong>13:10</strong>
                        <br>
                        <span>Singapore</span>
                </td>
                <td class="text-center" min-width="100px">
                        <strong>14:25</strong>
                        <br>
                        <span>Bangkok</span>
                </td>
                <td class="text-center" min-width="65px">
                        <strong>1hour 15m</strong>
                        <br>
                        <span>Direct</span>
                </td>
                <td class="text-right amount" min-width="77px">
                        <strong>$327</strong>
                </td>
            </tr>
        </tbody>
    </table>
</body>
</html>

我已经看过很多javascript排序表,但它只排序tbody的元素,但我从未见过任何可以排序tbody的javascript。我怎么能用javascript和没有jquery

来做到这一点

1 个答案:

答案 0 :(得分:0)

这个涉及很多东西。

首先,您必须将事件侦听器附加到您的所有项目中,以便在单击它们时它们将执行排序操作。

var airFlightTable = document.querySelector("#AirflightDetail");

var tableHeaders = airFlightTable.querySelectorAll("thead tr th");

tableHeaders.forEach(function(tableHeader){

  tableHeader.addEventListener("click", onTableHeaderClick);
});

当点击发生时,您可以通过收集所有tbody标记并操纵其DOM来开始排序过程。

function onTableHeaderClick(event){

  var sortBy = event.currentTarget.innerHTML;

  if(sortBy === 'Airline'){

    airFlightTable.querySelectorAll(".table-result-list").forEach(function(tableResultsLIst){

      var items = [].slice.call(tableResultsLIst.querySelectorAll("td"));

      var sortedItems = items.sort(); //you can place your sort logic here and after you're done you have to replace items of current tbody with sorted ones
    });
  }
}

还有一些工作要做,但从这一点开始你应该没问题。这是一个带有此实现的JSBin