0.0.2 • Published 6 years ago

mat-multi-sort v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

NgxMultiSortTable

This is the implementation for a multiple sortable table based on the Angluar Material Design. The focus is on server-side loaded and sorted data. Next to that the libarry provides some unsefull classes to reduce the duplicated code when useing the material paginator. The code is based on Francisco Arantes Rodrigues repository repo, so thanks for your great work.

The provied example is based on Angular 8 and Angular Material 8, I have not tested previous versions.

Client side multi sorting is not implementes yet, it might follow in the future or feel free to make a pull request.

Demo

To run the demo: 1. clone the repository 2. cd ngx-multi-sort-table 3. npm install 4. ng build multi-sort-table 5. ng serve

Screenvideo

screen video

Documentation

TableData

The TabelData an an usefull class, which handels a lot of work for your app, such as page events (next, previous, sizeChange) and sorting event. Next to that it keeps the current state of the table, again sorting and pagnation.

Properties

NameDescriptiondefaultExampe
columnsAn array of the displayed columns of the table with id: name of the attribute and name: Name to display in the headernone[{ id: 'first_name', name: 'First Name' }]
displayedColumnsAn array of the currently displayed columns (id) and their orderall columns
dataSourceA MatMultiSortTableDataSource, which is special DataSource for sortingnone
pageSizeThe current selected pageSizefirst entry of pageSizeOptions
pageSizeOptionsThe options for the pageSize, which the user can see in the menu[10, 20, 50, 100]
pageIndexThe index of the page0
totalElementsThe total number of elemnts of the table, must be set from your componentnone
sortParamsAn Array of the columns (id), which the user had chosen to sort. The order of the sorting is represented by the order of the ids in the parameter[]['first_name', 'last_name']
sortDirsAn Array of the column's sortdirections, which the user had chosen to sort. The order is the same like sortParams[]['asc', 'desc']
nextObservableAn Observable that fires, when the user clicks the next button
previousObservableAn Observable that fires, when the user clicks the previous button
sizeObservableAn Observable that fires, when the user changes the pageSize
sortObservableAn Observable that fires, when the user changes the sorted columns or direction

Methods

NameDescriptionParameter
constructorThe constructor for the for the class, where you initalize your columns. Optionally, you can add the default ids of the default sort colum and direction.columns: string[], totalElements: number, options: { defaultSortParams?: string[], defaultSortDirs?: string[], pageSizeOptions?: number[], totalElements?: number }
onSortEventThe method to bind to the matSortChange output of the tablenone
onPagnationEventThe method to bin to the page output of the mat-paginator$event: PageEvent
setDisplayedColumnsA method to set the columns of the table. It should get bind to the activeColumnsChange output of mat-multi-sort-table-settings, in order to enable / disable and reorder the columnscolumns: string[]

MatMultiSortHeaderComponent

This component manages the sorting of the table. To use the multisort add matMultiSort to your table and pass the mat-multi-sort-header="<your-column-id>" to the <th mat-header-cell>.

MatMultiSortTableSettingsComponent

This component display some settings for your table. The user can select the columns he wants to see in his table, next to that he can change the order of the columns.

NameDescriptionParameter
columnsAn input to definde the columns to display in the menu. The id is the column id and name the name to display in the menu@Input: { id: string, name: string }[]
activeColumnsChangeThe ouput gives the order of th currently display columns id and their order@Output: string[]

Example code for the template

<div class="mat-elevation-z8">
  <mat-multi-sort-table-settings [columns]="table.columns" (activeColumnsChange)="table.setDisplayedColumns($event)"></mat-multi-sort-table-settings>
  <table mat-table [dataSource]="table.dataSource" matMultiSort  (matSortChange)="table.onSortEvent()">
    <ng-container matColumnDef="id">
      <th mat-header-cell *matHeaderCellDef mat-multi-sort-header="id"> ID </th>
      <td mat-cell *matCellDef="let row"> {{row.id}} </td>
    </ng-container>

    <ng-container matColumnDef="progress">
      <th mat-header-cell *matHeaderCellDef mat-multi-sort-header="progress"> Progress </th>
      <td mat-cell *matCellDef="let row"> {{row.progress}} % </td>
    </ng-container>

    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef mat-multi-sort-header="name"> Name </th>
      <td mat-cell *matCellDef="let row"> {{row.name}} </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="table.displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: table.displayedColumns;"></tr>
  </table>
  <mat-paginator [pageSize]="table.pageSize" [pageIndex]="table.pageIndex" [pageSizeOptions]="table.pageSizeOptions"
    [length]="table.totalElements ? table.totalElements : 0" (page)="table.onPagnationEvent($event)">
  </mat-paginator>
</div>

Example code for the component.ts

  displayedColumns = ['id', 'name', 'progress'];

  table: TableData<UserData>;
  @ViewChild(MatMultiSort, { static: false }) sort: MatMultiSort;

  constructor(
    private dummyService: DummyService
  ) {
    this.table = new TableData<UserData>(
      [
        { id: 'id', name: 'ID' },
        { id: 'name', name: 'Name' },
        { id: 'progress', name: 'Progess' }
      ], 100
    );

  }

  ngOnInit() {
    this.table.sortObservable.subscribe(() => { this.getData(); });
    this.table.nextObservable.subscribe(() => { this.getData(); });
    this.table.previousObservable.subscribe(() => { this.getData(); });
    this.table.sizeObservable.subscribe(() => { this.getData(); });

    setTimeout(() => {
      this.table.dataSource = new MatMultiSortTableDataSource(this.sort);
      this.getData();
    }, 0);
  }

  getData() {
    const res = this.dummyService.list(this.table.sortParams, this.table.sortDirs, this.table.pageIndex, this.table.pageSize);
    this.table.totalElements = res.totalElements;
    this.table.pageIndex = res.page;
    this.table.pageSize = res.pagesize;
    this.table.dataSource.setTableData(res.users);
  }