[英]How to get elements of selected row, and show it , using angular 2+ and typescript
I am using a table and I have to capture the data and show it separately when I click a particular row in a table, I was able to highlight the selected row, and capture the row no. 我正在使用一个表,所以我必须捕获数据并单击表中的特定行时分别显示它们,因此能够突出显示选定的行并捕获行号。 and show it.
并显示出来。 I am not sure how can I capture the items in the selected row and show it.
我不确定如何捕获所选行中的项目并显示它。
The code I have done so far is, 到目前为止,我完成的代码是
HTML Markup: HTML标记:
<mat-tab label="PINS" flex>
<div id="pinsDataSource" *ngFor="let pin of pins; let i = index" (click)="setClickedRow(i)" [class.alertactive]="i == selectedRow">
{{pin.alertTitle}}
{{pin.alertCode}}
{{pin.date | date:'MM/dd/yyyy'}}
</div>
</mat-tab>
Here I want to show, the items that I have selected from that particular row like: 我要在这里显示从该特定行中选择的项目,例如:
<div>
Selected Row :
<strong>{{selectedRow}}</strong>
</div>
component.ts file: component.ts文件:
pins: any[];
selectedRow: Number;
setClickedRow: Function;
constructor(private proService: ProService) {
this.setClickedRow = function (index) {
this.selectedRow = index;
}
}
ngOnInit() {
// here the table items are called from webapi
this.proService.getPinnedAlerts().subscribe(res => {
this.pins = res;
});
}
}
Pass the object as paramter to the function and assign the parameter to selectedRow
将对象作为参数传递给函数,并将参数分配给
selectedRow
(click)="setClickedRow(pin )"
setClickedRow(pin ){
this.selectedRow = pin
}
Just pass an instance of your pins data in your function, which is you are calling on div as below: 只需在您的函数中传递您的pins数据的实例,即您按以下方式调用div:
<mat-tab label="PINS" flex>
<div id="pinsDataSource" *ngFor="let pin of pins; let i = index" (click)="setClickedRow(i, pin)" [class.alertactive]="i == selectedRow">
{{pin.alertTitle}}
{{pin.alertCode}}
{{pin.date | date:'MM/dd/yyyy'}}
</div>
</mat-tab>
I have passed pin instance of pins data and capture it on your ts file. 我已经通过了pin数据的pin实例,并将其捕获到您的ts文件中。
pins: any[];
selectedRow: Number;
setClickedRow: Function;
constructor(private proService: ProService) {
this.setClickedRow = function (index) {
this.selectedRow = index;
}
}
ngOnInit() {
// here the table items are called from webapi
this.proService.getPinnedAlerts().subscribe(res => {
this.pins = res;
});
}
setClickedRow( index, rowData)
console.log("row data", rowData);
}
I found out the answer over here, this might help others too 我在这里找到了答案,这也可能对其他人有帮助
https://angular.io/tutorial/toh-pt2 https://stackblitz.com/angular/xongoyjllpr?file=src%2Fapp%2Fheroes%2Fheroes.component.ts https://angular.io/tutorial/toh-pt2 https://stackblitz.com/angular/xongoyjllpr?file=src%2Fapp%2Fheroes%2Fheroes.component.ts
You are using setClickedRow
as function in markup, but it is defined as property in typescript file. 您将
setClickedRow
用作标记中的函数,但已在打字稿文件中定义为属性。 Remove the code inside the constructor. 删除构造函数中的代码。 Instead , do this
相反,这样做
selectedRow: Number;
setClickedRow(i){
this.selectedRow = i;
}
constructor(private proService: ProService) {
this.setClickedRow(index);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.