Creating server side pagination with ng2-smart-table in Angular 6
Asked Answered
E

2

6

I am trying to use the component <ng2-smart-table>. I actually did it, but I need to refresh the data after the user click in a page or in a column for sorting, is there any way to capture those events and refresh the data?

table.component.html

<ng2-smart-table 
[settings]="settings" 
[source]="characters">
</ng2-smart-table>

table.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class TableService {
  constructor(private http: HttpClient) { }
  url = 'http://localhost:4000';
  getCharacters() {
    return this
            .http
            .get(`${this.url}/characters`);
  }
}

table.interface.ts

export interface Table {
   ticket: String;
   title: String;
   state: String;
   owner: String;
   age: String;
   priority: String;
}

table.component.ts

import { Component, OnInit } from '@angular/core';
import { TableService } from './table.service';
import { Table } from './table.interface';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.scss']
})
export class TableComponent implements OnInit {

  characters: Table[];
  settings = {
        columns: {
          ticket: {
            title: 'Ticket',
            filter: false
          },
          title: {
            title: 'Title',
            filter: false
          },
          state: {
            title: 'State',
            filter: false
          },
          owner:{
            title: 'Owner',
            filter: false
          },
          age:{
            title: 'Age',
            filter: false
          },
          priority:{
            title: 'Priority',
            filter: false
          }
        },
        actions:{
          columnTitle: '',
          add: false,
          edit: false,
          delete: false
        }
  };    

  constructor(private tservice: TableService) { }

  ngOnInit() {
      this
        .tservice
        .getCharacters()
        .subscribe((data: Table[]) => {
          this.characters = data;
      });
  }
}

parent.component.ts

import { Component, OnInit } from '@angular/core';
import { TableComponent } from '../../utils/table/table.component';

@Component({
  selector: 'app-ticket-item-list',
  templateUrl: './ticket-item-list.component.html',
  styleUrls: ['./ticket-item-list.component.scss']
})
export class TicketItemListComponent implements OnInit {

  pagination = {
    TotalItems: 100,
    CurrentPage: 1,
    PageSize: 10,
    TotalPageLinkButtons: 5,
    RowsPerPageOptions: [10, 20, 30, 50, 100]
  };

  /* Paging Component metod */
  myChanges(event) {
    this.pagination.CurrentPage = event.currentPage;
    this.pagination.TotalItems = event.totalItems;
    this.pagination.PageSize = event.pageSize;
    this.pagination.TotalPageLinkButtons = event.totalPageLinkButtons;
  }

  constructor() { }

  ngOnInit() {
  }

}

parent.component.html

<app-table></app-table>
Ejector answered 5/7, 2018 at 19:9 Comment(1)
Thanks for the question. Do you have any search criteria things? If yes, could you please share us, how do you track your search criteria changes?Pandemic
F
0

You can use the ng2-smart-table LocalDataSource to filter, set filters add filters etc. So you could store your service result in a LocalDataSource object. In your init something like: this.characters = new LocalDataSource(tableService.getCharacters());

Here is the source for reference: https://github.com/akveo/ng2-smart-table/blob/master/projects/ng2-smart-table/src/lib/lib/data-source/local/local.data-source.ts

Firm answered 5/7, 2018 at 19:32 Comment(3)
can you please share an example in code, I come from angular 1, it is kind of complicated tu understand tsAlgebraic
url provided not found, 404Extranuclear
Working URL github.com/akveo/ng2-smart-table/blob/master/projects/…Quite
M
0

For pagination use setPaging like

this.YourlocalDataSource.setPaging(1, 10, true);

1 is the page, 10 is number of rows per page, true forces render the page

Martinic answered 22/1, 2019 at 20:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.