0

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' }
 ];
}
Ivan
  • 13
  • 2
  • 4
  • What do you mean by get instance of current table? – eddyP23 Nov 02 '17 at 10:13
  • Also is `[object]` actually a valid type? I think you want here to create a class for input data, so that you get type inference in your code – eddyP23 Nov 02 '17 at 10:14
  • I mean some object that contains all data of current table. I've tried to use something like @ViewChild(DatatableComponent) table: DatatableComponent; But if we have 2 or more tables on one page 'table' contains only first table data. – Ivan Nov 02 '17 at 10:21

1 Answers1

3

@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

eddyP23
  • 6,420
  • 7
  • 49
  • 87