简体   繁体   English

使用 Javascript 对 HTML 表进行排序

[英]Sort HTML table with Javascript

Here is an example code for table sorting taken from javascript.info: https://plnkr.co/edit/5xHStBMfV5AVtoxR?p=preview&preview=这是从 javascript.info 获取的表格排序示例代码: https://plnkr.co/edit/5xHStBMfV5AVtoxR?p=preview&preview=

let sortedRows = Array.from(table.tBodies[0].rows).sort((rowA, rowB) => rowA.cells[0].innerHTML.localeCompare(rowB.cells[0].innerHTML));

table.tBodies[0].append(...sortedRows);

My question is: Array.from(table.tBodies[0].rows) produces an array.我的问题是: Array.from(table.tBodies[0].rows)产生一个数组。 But cells[0] is not defined for arrays.但是没有为 arrays 定义cells[0] How does the code work?代码是如何工作的?

The .rows property of a table gives you an HTMLCollection of the rows in the table.表的.rows属性为您提供表中行的 HTMLCollection。 Array.from turns the HTMLCollection of rows into an array of rows. Array.from将行的 HTMLCollection 转换为行数组。

With

.sort((rowA, rowB) => rowA.cells[0] ...

Each parameter is one of the array items being iterated over - a row - neither rowA nor rowB are the array that was created with Array.from .每个参数都是被迭代的数组项之一 - 一行 - rowArowB都不是使用Array.from创建的数组。 They're the array elements, which are rows.它们是数组元素,它们是行。

The array elements (which were originally the HTMLCollection elements) are still rows - and rows have a .cells property.数组元素(最初是 HTMLCollection 元素)仍然是行 - 并且行具有.cells属性。

Your rowA is a TR, so you will have to use fetch cells from it.您的 rowA 是一个 TR,因此您必须使用从中获取单元格。 Array.from just converts an HTML collection/ NodeList to an Array of elements so that you can use Array methods Array.from 只是将 HTML 集合/ NodeList 转换为元素数组,以便您可以使用数组方法

 let sortedRows = Array.from(table.tBodies[0].rows).sort((rowA, rowB) => { const cellsA = rowA.querySelectorAll('td') const cellsB = rowB.querySelectorAll('td') return cellsA[0].innerHTML.localeCompare(cellsB[0].innerHTML) }); table.tBodies[0].append(...sortedRows);
 <table id="table"> <thead> <tr><th>Name</th><th>Surname</th><th>Age</th></tr> </thead> <tbody> <tr><td>John</td><td>Smith</td><td>10</td></tr> <tr><td>Pete</td><td>Brown</td><td>15</td></tr> <tr><td>Ann</td><td>Lee</td><td>5</td></tr> <tr><td>...</td><td>...</td><td>...</td></tr> </tbody> </table>

Data based approach:基于数据的方法:

 const data = [ { name: "John", surname: "Smith", age: 10}, { name: "Pete", surname: "Brown", age: 15}, { name: "Ann", surname: "Lee", age: 5}, ] const sortData = (key) => { return data.slice().sort((a,b) => { return typeof a[key] === "string"? a[key].localeCompare(b[key]): a[key] - b[key] }); } const createTable = (data) => { return data.map((item) => { return `<tr>${ Object.values(item).map((value) => `<td>${value}</td>`).join("") }</tr>` }).join("").concat("<tr><td>...</td><td>...</td><td>...</td></tr>") } const renderTable = (data) => { document.getElementById('table').querySelector('tbody').innerHTML = createTable(data) } document.querySelector('.actionPanel').addEventListener('click', (e) => { const text = e.target.textContent.toLowerCase(); renderTable(sortData(text)) }) renderTable(data)
 <div class='actionPanel'> <button>Name</button> <button>Surname</button> <button>Age</button> </div> <table id="table"> <thead> <tr><th>Name</th><th>Surname</th><th>Age</th></tr> </thead> <tbody></tbody> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM