Skip to content

Repository files navigation

Table component with server sorting and server pagination for Angular2

npm versionBuild Statusnpm downloads

Demo

Check live demo in plunker

Installation

npm i -S angular2-serverpagination-datatable

Usage example

AppModule.ts

import{NgModule}from"@angular/core";
...
import{DataTableModule}from"angular2-serverpagination-datatable";
@NgModule({imports: [
...
DataTableModule],
...
})exportclassAppModule{}

UserComponent.html

<tableclass="table table-striped" [mfData]="data | dataFilter : filterQuery" #mf="mfDataTable"
[mfRowsOnPage]="rowsOnPage" [(mfSortBy)]="sortBy" [(mfSortOrder)]="sortOrder" [mfActivePage]="activePage"
(mfOnPageChange)="onPageChange($event)" [(mfAmountOfRows)]="itemsTotal" (mfSortOrderChange)="onSortOrder($event)"><thead><tr><thstyle="width: 10%"></th><thstyle="width: 20%"><mfDefaultSorterby="name">Name</mfDefaultSorter></th><thstyle="width: 40%"><mfDefaultSorterby="email">Email</mfDefaultSorter></th><thstyle="width: 10%"><mfDefaultSorterby="age">Age</mfDefaultSorter></th><thstyle="width: 20%"><mfDefaultSorter[by]="sortByWordLength">City</mfDefaultSorter></th></tr></thead><tbody><tr*ngFor="let item of mf.data"><td><button(click)="remove(item)" class="btn btn-danger">x</button></td><td>{{item.name}}</td><td>{{item.email}}</td><tdclass="text-right">{{item.age}}</td><td>{{item.city | uppercase}}</td></tr></tbody><tfoot><tr><tdcolspan="5"><mfBootstrapPaginator[rowsOnPageSet]="[5,10,15]"></mfBootstrapPaginator></td></tr></tfoot></table>

UserComponent.ts

ngOnInit(): void{this.loadData();}publicloadData(){this.http.get("/app/data.json").subscribe((data)=>{setTimeout(()=>{this.data=_.orderBy(data.json(),this.sortBy,[this.sortOrder]);this.data=_.slice(this.data,(this.activePage-1)*this.rowsOnPage,(this.activePage-1)*this.rowsOnPage+this.rowsOnPage);this.itemsTotal=data.json().length;},2000);});}publictoInt(num: string){return+num;}publicsortByWordLength=(a: any)=>{returna.city.length;}publicremove(item){letindex=this.data.indexOf(item);if(index>-1){this.data.splice(index,1);}}publiconPageChange(event){this.rowsOnPage=event.rowsOnPage;this.activePage=event.activePage;this.loadData();}publiconSortOrder(event){this.loadData();}

API

mfData directive

  • selector: table[mfData]
  • exportAs: mfDataTable
  • inputs
    • mfData: any[] - array of data to display in table
    • mfRowsOnPage: number - number of rows should be displayed on page (default: 1000)
    • mfActivePage: number - page number (default: 1)
    • mfSortBy: any - sort by parameter
    • mfSortOrder: string - sort order parameter, "asc" or "desc"
  • outputs
    • mfSortByChange: any - sort by parameter
    • mfSortOrderChange: any - sort order parameter
    • mfOnPageChange: any - page change parameter(rowsOnPage,activePage)

mfDefaultSorter component

  • selector: mfDefaultSorter
  • inputs
    • by: any - specify how to sort data (argument for lodash function _.sortBy )

mfBootstrapPaginator component

Displays buttons for changing current page and number of displayed rows using bootstrap template (css for bootstrap is required). If array length is smaller than current displayed rows on page then it doesn't show button for changing page. If array length is smaller than min value rowsOnPage then it doesn't show any buttons.

  • selector: mfBootstrapPaginator
  • inputs
    • rowsOnPageSet: number - specify values for buttons to change number of diplayed rows

About

DataTable - Simple table component with server sorting and server pagination for Angular2

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages