Search code examples
angularngx-datatable

NGX-Datatables. How to get current table instance?


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' }
 ];
}

Solution

  • @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