问题
I have component with table, that receive RowData from another component by @Input. How can i get instance of current table.
That's part of my html
<ngx-datatable
class="material"
[rows]="inputData"
[columns]="columns">
</ngx-datatable>
And component ts
export class GridComponent implements OnChanges {
@Input() inputData: [object];
columns1 = [
{ prop: 'name' },
{ name: 'Gender' },
{ name: 'Company' }
];
}
回答1:
@Viewchild
is the correct method, but you should be using it another way.
Here is what you want to do.
Html:
<ngx-datatable
#firstTable
class="material"
[rows]="inputData"
[columns]="columns">
</ngx-datatable>
<ngx-datatable
#secondTable
class="material"
[rows]="inputData"
[columns]="columns">
</ngx-datatable>
Component.ts:
export class GridComponent implements OnChanges {
@Input() inputData: [object]; // This line doesn't look right
@ViewChild('firstTable') myTable1: DatatableComponent;
@ViewChild('secondTable') myTable2: DatatableComponent;
columns1 = [
{ prop: 'name' },
{ name: 'Gender' },
{ name: 'Company' }
];
}
You can take a look at this page for reference: http://learnangular2.com/viewChild/
Addition:
You can also use @ViewChildren
that selects all of the elements, depending on your needs, docs here:
https://angular.io/api/core/ViewChildren
来源:https://stackoverflow.com/questions/47072556/ngx-datatables-how-to-get-current-table-instance