简体   繁体   English

在TypeScript中获取表行索引和行数据

[英]Get table row index and row data in TypeScript

I have a table in HTML, which is created as follows: 我有一个HTML表格,其创建如下:

<table id="tableID" onclick="getRowData()" class="table table-hover"></table>

At first it gets populated with some initial data, which could make it look something like this: 首先,它会填充一些初始数据,这可能使它看起来像这样:

From  Action  To
 a      1     b
 a      0     a

I want to be able to retrieve the data from an arbitrary row, just by clicking on that row on the webpage. 我希望能够从任意行检索数据,只需单击网页上的该行即可。 I also want to be able to retrieve the row index from that row as well. 我也希望能够从该行检索行索引。 For example, if I would want to get the data from the first row, then I would get a 1 b 例如,如果我想从第一行获取数据,那么我会得到a 1 b

How would such a function look like? 这样的功能怎么样?

You have to place your click handler on the rows and not on the table. 您必须将单击处理程序放在行而不是表上。

Since your table is dynamically generated, attaching the click handlers from Typescript/JavaScript might be easier, here is a way to do it. 由于您的表是动态生成的,因此从Typescript / JavaScript附加单击处理程序可能会更容易,这是一种方法。

Use document.querySelector('#tableID') to get a reference to your table. 使用document.querySelector('#tableID')获取对表的引用。

Then, there are two ways to get a reference to the table rows and cells: 然后,有两种方法可以获得对表行和单元格的引用:

  • Use table.querySelectorAll('tbody td') to query the rows in the table DOM. 使用table.querySelectorAll('tbody td')查询表DOM中的行。 Then use row.querySelectorAll('td') to get the cells. 然后使用row.querySelectorAll('td')来获取单元格。

  • Use the table DOM API (see @HB comment below) to avoid querying the DOM for each row and each cell. 使用表DOM API(请参阅下面的@HB注释)以避免查询每行和每个单元格的DOM。 With this technique you can get the rows with table.tBodies[0].rows and the cells with row.cells . 使用此技术,您可以使用table.tBodies[0].rows和包含row.cells的单元格获取行。

Then use element.addEventListener('click', handler) to attach the click handlers to every row. 然后使用element.addEventListener('click', handler)将单击处理程序附加到每一行。

Here is a JavaScript demo with detailed comments: 这是一个包含详细注释的JavaScript演示:

 // get a reference to your table by id // cast this to HTMLTableElement in TypeScript const table = document.querySelector('#tableID'); // get all rows in the first table body const rows = table.tBodies[0].rows; // convert the rows to an array with the spread operator (...) // then iterate over each row using forEach Array.from(rows).forEach((row, idx) => { // attach a click handler on each row row.addEventListener('click', event => { // get all cells in the row, convert them to an array with the spread operator (...) // then for each cell, return its textContent by mapping on the array const tds = Array.from(row.cells).map(td => td.textContent); console.clear(); // Log the row index console.log('row index:', idx); // Log the tds content array console.log('tds content:', ...tds); // join the contents of the tds with a space and display the string console.log('tds string:', tds.join(' ')); }); }); 
 <table id="tableID"> <thead> <tr><th>From</th><th>Action</th><th>To</th></tr> </thead> <tbody> <tr><td>a</td><td>1</td><td>b</td></tr> <tr><td>a</td><td>0</td><td>a</td></tr> </tbody> </table> 

Also, in your TypeScript code, do not forget to cast the result of document.querySelector('#tableID') to HTMLTableElement to get the correct typing: 另外,在TypeScript代码中,不要忘记将document.querySelector('#tableID')的结果document.querySelector('#tableID')HTMLTableElement以获得正确的输入:

const table: HTMLTableElement = document.querySelector('#tableID');

See the TypeScript demo 请参阅TypeScript演示

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

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