[英]How to use closest and parents javascript methods to get td value in angular 6
I am trying to get table tr and second td values using parents('tr) and closest('tr') javascript methods and after selected dropdown.But not working i have searched in google nut no use.If anyone know please help to find the solutions.我正在尝试使用 parent('tr) 和最接近的 ('tr') javascript 方法以及在选择下拉列表后获取表 tr 和第二个 td 值。解决方案。
app.component.html: app.component.html:
<table>
<thead>
<tr>
<td>Emp id</td>
<td>Roll id</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<tr>
<td>12345</td>
<td>18956</td>
<td>12356</td>
<td>
<select (change)="getTdValue(e)">
<option value="gettdvalue">Get second td value of this row</option>
</select>
</td>
</tr>
<tr>
<td>12345</td>
<td>18906</td>
<td>12356</td>
<td>
<select (change)="getTdValue(e)">
<option value="gettdvalue">Get second td value of this row</option>
</select>
</td>
</tr>
<tr>
<td>12345</td>
<td>98956</td>
<td>12356</td>
<td>
<select (change)="getTdValue(e)">
<option value="gettdvalue">Get second td value of this row</option>
</select>
</td>
</tr>
<tr>
<td>12345</td>
<td>13956</td>
<td>12356</td>
<td>
<select (change)="getTdValue(e)">
<option value="gettdvalue">Get second td value of this row</option>
</select>
</td>
</tr>
</tbody>
</table>
app.component.ts: app.component.ts:
getTdValue(e){
let target = e.target.closest('tr');
if (target && target.id) {
let td = target.find('td:nth-child(2)');
if (td) {
console.log("Second td value is ="+ td.value )
}
}
}
https://stackblitz.com/edit/angular-peioe6?file=src/app/app.component.html https://stackblitz.com/edit/angular-peioe6?file=src/app/app.component.html
First you need to have a second option
element inside your select
element in order to get any change events triggered.首先,您需要在select
元素中包含第二个option
元素,以便触发任何更改事件。
<select (change)="getTdValue($event)">
<option></option>
<option>Get second td value of this row</option>
</select>
Then you can rewrite your getTdValue
method as follows:然后你可以重写你的getTdValue
方法如下:
getTdValue(mouseEvent: MouseEvent) {
let target = <HTMLSelectElement> mouseEvent.target;
let td = <HTMLTableCellElement> target.closest('tr').childNodes.item(1);
console.log("This row ID is " + td.innerHTML);
}
See following StackBlitz请参阅以下StackBlitz
UPDATE更新
When target.closest
doesn't work (Angular 6), it can be replaced by target.parentElement.parentElement
as follows:当target.closest
不起作用(角6),它可以被替换target.parentElement.parentElement
如下:
getTdValue(mouseEvent: MouseEvent) {
let target = <HTMLSelectElement> mouseEvent.target;
let td = <HTMLTableCellElement> target.parentElement.parentElement.childNodes.item(1);
console.log("This row ID is " + td.innerHTML);
}
You have an if statement that evaluates the following condition: target && target.id
.您有一个评估以下条件的 if 语句: target && target.id
。 Since none of your <tr>
element have an ID, the statement will evaluate to false, and none of the logic contained therein will be executed.由于您的<tr>
元素都没有 ID,因此该语句将评估为 false,并且不会执行其中包含的任何逻辑。
Since you are not using the ID for anything, you can simply remove it:由于您没有将 ID 用于任何用途,您可以简单地将其删除:
getTdValue(e){
let target = e.target.closest('tr');
if (target) {
let td = target.find('td:nth-child(2)');
if (td) {
console.log("Second td value is ="+ td.value )
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.