mariuszfoltak / angular2-datatable

DataTable - Simple table component with sorting and pagination for Angular2
202 stars 183 forks source link

Do you have plan to add onPageChage event ? #85

Open ashkrit opened 7 years ago

lacroixdavid1 commented 7 years ago

You can subscribe to onPageChange event

export class MyComponent implements OnInit {
  @ViewChild('dataTable') dataTable: DataTable; 
  ngOnInit() {
        this.dataTable.onPageChange.subscribe((x) =>{
        console.log('Current page : ' + x.activePage);
        console.log('Rows on page : ' + x.rowsOnPage);
        });
   }
}
ghost commented 7 years ago

@lacroixdavid1 Can you give me the code for HTML also? I am getting this error TypeError: Cannot read property 'onPageChange' of undefined

PavelDanyliuk commented 7 years ago

@UnnikrishnanMR in code below, you need to change

@ViewChild('dataTable') dataTable: DataTable; 
 to 
 @ViewChild(DataTable) dataTable: DataTable; 
lacroixdavid1 commented 7 years ago
<table [mfData]="dataService.productList"
       #dataTable="mfDataTable"
       [mfRowsOnPage]="dataService.rowsOnPage"
       [mfActivePage]="dataService.lastSelectedPage">
  <thead>
    <tr>
      <th>
        <mfDefaultSorter by="value">Col header</mfDefaultSorter>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let item of dataTable.data">
      <td>item.value</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>
        <mfBootstrapPaginator [rowsOnPageSet]="[20,50,100,250,1000]"></mfBootstrapPaginator>
      </td>
    </tr>
  </tfoot>
</table>

export class MyComponent implements OnInit {
  @ViewChild('dataTable') dataTable: DataTable; 
  ngOnInit() {
        this.dataTable.onPageChange.subscribe((x) =>{
        console.log('Current page : ' + x.activePage);
        console.log('Rows on page : ' + x.rowsOnPage);
        });
   }
}
lacroixdavid1 commented 7 years ago

Or you could do

<table [mfData]="dataService.productList"
       #dataTable="mfDataTable"
       (onPageChange)="pageChangedAction()"
       [mfRowsOnPage]="dataService.rowsOnPage"
       [mfActivePage]="dataService.lastSelectedPage">
  <thead>
    <tr>
      <th>
        <mfDefaultSorter by="value">Col header</mfDefaultSorter>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let item of dataTable.data">
      <td>item.value</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>
        <mfBootstrapPaginator [rowsOnPageSet]="[20,50,100,250,1000]"></mfBootstrapPaginator>
      </td>
    </tr>
  </tfoot>
</table>
export class MyComponent  {
  pageChangedAction(x) {
        console.log('Current page : ' + x.activePage);
        console.log('Rows on page : ' + x.rowsOnPage);
   }
}
AdonousTech commented 7 years ago

@lacroixdavid1 @UnnikrishnanMR If you get the undefined message above, it is because the ViewChild is not available in ngOnInit. You need to access it in ngAfterViewInit

export class MyComponent implements OnInit {
  @ViewChild('dataTable') 
  private dataTable: DataTable; 
  ngAfterViewInit() {
        this.dataTable.onPageChange.subscribe((x) =>{
        console.log('Current page : ' + x.activePage);
        console.log('Rows on page : ' + x.rowsOnPage);
        });
   }
}
HerrSlavchev commented 7 years ago

Please check this issue as well. I think it would allow an approach which is more native to Angular's subscription model #70