项目作者: oauthentik

项目描述 :
Angular Material Enhanced Table with less boilerplate
高级语言: TypeScript
项目地址: git://github.com/oauthentik/mat-advanced-table.git
创建时间: 2020-04-10T19:00:17Z
项目社区:https://github.com/oauthentik/mat-advanced-table

开源协议:MIT License

下载


material advanced table

Angular Material Enhanced Table with less boilerplate semantic-release Build Status npm codecov

Features

  • Abstraction of html elements|typescript boilerplate needed for creating a table
  • Easy and precise column mapping with class decorators
  • Advanced column filtering
  • Loading / Empty Data Templates
  • All MatTable features included (Pagination, Sorting, Search…)

Installation

  1. Install using npm

    1. npm i mat-advanced-table

Demo

View demo

API

@Column

verboseName: string The column label to be displayed

key: string; the column key: By default it’s the same as the attribute name

order: number The displayed order of the column

propertyType: "String" | "Date" | "Number" | "Object" The popriety primitive type collected from the ProprietyDescriptor and can be overriden by setting a value

canSort: boolean Whether this column is sortable

sortBy: "asc" | "desc" The default sorting order

visible: boolean Whether the column is visible by default

format: string | false Date|number format as in Angular format pipe

sortByAccessor: (instance) => any Callback used for sorting the complex object

propertyAccessor: (obj: any, thisRef?) => any Callback used for accessing complex object primitives

eg:

  1. @Column({ propertyAccessor: (obj:UserModel, thisRef) => obj.role.name })
  2. rolename :string

MatAdvancedTableComponent

@Input() columns: ColumnModel[] (Required) : The table column array definition. it comes from the @Column decorator

@Input() data: any[] (Optional) : The data to be displayed

@Input() loading: boolean (Optional) : Whether data is loading

@Input() legend: TemplateRef<any> (Optional) : The legend template

@Input() noDataTemplate: TemplateRef<any> (Optional) : The empty data template

@Input() loadingTemplate: TemplateRef<any> (Optional) : The loading data template

@Input() selection: SelectionModel (Optional) : The table initial selection

@Input() rowNgClassFun: (item: any) => string (Optional) : A helper function to returns a row dependant class string

@Input() options:NgxMatTableOptions (Optional) : The table options with it’s defaults values : NgxMatTableOptionsDefaults
| Option | Definition | Type | Default |
| ——————- | ————————————————————————————————————————————— | ———— | ————————- |
| minCellWidth | The table cell’s min-width style attribute in px | number | 80 |
| maxCellWidth | The table cell’s max-width style attribute in px | number | 200 |
| classList | a list of classes to add to the table (eg: [‘table-responsive’,’compact’]… | string[] | [] |
| title | The table header title | string | null |
| styles: NgxMatTableStyleOptions | The table custom styles attributes | { denseDisplay: false, selectedRowClass: "ngx-mat-selected", tableHeaderClass: "ngx-thead", tableRowHeight: "1rem", }, |
| actions | Whether to show the actions column (Template is issued with the MatCellTemplate Directive) | boolean | false |
| actionsLabel | The actions column header label | string | Actions |
| paging | Whether to show the MatPaginator | boolean | true |
| search | Whether to show the Search bar and activate the advanced filter | boolean | true |
| selection | whether to show the selection column | boolean | false |
| placeholder | Empty value placeholder | string | N/A |
| emptyDataText | The no data message to be displayed under the table columns | string | No Data available |
| loadingText | The loading text to be displayed when data is loading | string | Please wait |

MatCellTemplateDirective (Selector : matATCellTemplate)

@Input('matATCellTemplate') name:string (Required) : The column cell template name and it must be present in the @Table definition

Usage

Quick usage

import the MatAdvancedTableModule to the modules you are using it in

  1. import { MatAdvancedTableModule } from "mat-table-advanced";
  2. @NgModule({
  3. declarations: [AppComponent],
  4. imports: [CommonModule, MatAdvancedTableModule],
  5. })
  6. export class AppModule {}

Use @Table and @Column decorators to define table columns

  1. export enum UserRoles {
  2. Moderator,
  3. Guest,
  4. }
  5. @Table
  6. export class UserModel {
  7. @Column({
  8. verboseName: "User ID",
  9. canSort: true,
  10. sortBy: "desc",
  11. })
  12. id: number;
  13. @Column({ verboseName: "User name", canSort: true })
  14. username: string;
  15. @Column({ verboseName: "Role", canSort: true })
  16. role: UserRoles;
  17. }

then load the columns in your host component ts file

  1. import {MatAdvancedTableService} from 'mat-advanced-table';
  2. constructor(
  3. private tableService: MatAdvancedTableService
  4. ) {}
  5. users$: Observable<UserModel[]>;
  6. userModelColumns: ColumnModel[];
  7. ngOnInit() {
  8. this.users$ = of([
  9. userId: 1,
  10. username: "admin",
  11. displayName: "Admin ",
  12. isAdmin: true,
  13. role: UserRoles.Moderator,
  14. ])
  15. this.userModelColumns = this.tableService.getColumnsOfType(
  16. UserModel
  17. );
  18. }

after that you’re ready to use it in the component template as

  1. <mat-table-advanced
  2. [columns]="userModelColumns"
  3. [data]="users$ | async"
  4. ></mat-table-advanced>

Advanced usage

1. Using actions cell with a template cell directive:

CAUTION: Use actions as the template name to allow the table to bind correctly to the actions column, if another column in your defined table contains the same key, pray to change it in the ColumnModel.key option.

  1. @Component({
  2. selector: "app-component",
  3. template: `<mat-advanced-table
  4. [data]="data"
  5. [columns]="columns"
  6. [options]="{ actions: true }"
  7. >
  8. <ng-template [matATCellTemplate]="'actions'">
  9. <button mat-button (click)="deleteUser($event)">delete</button>
  10. </ng-template>
  11. </mat-advanced-table>`,
  12. styles: [``],
  13. })
  14. export class AppComponent implements OnInit {
  15. constructor(private matAdvancedService: MatAdvancedTableService) {}
  16. columns;
  17. data;
  18. ngOnInit(): void {
  19. this.columns = this.matAdvancedService.getColumnsOfType(MockClass);
  20. this.data = [
  21. {
  22. // some mock data
  23. },
  24. ];
  25. }
  26. deleteUser(user) {
  27. console.log("User Deleted", user);
  28. }
  29. }

2. Using custom data loader

  1. @Component({
  2. selector: "app-component",
  3. template: `<mat-advanced-table
  4. [data]="data"
  5. [columns]="columns"
  6. [loadingTemplate]="loadingTmp"
  7. [loading]="isLoading"
  8. >
  9. <ng-template #loadingTmp>
  10. <your-custom-loader [text]="'Please wait...'"></your-custom-loader>
  11. </ng-template>
  12. </mat-advanced-table>`,
  13. styles: [``],
  14. })
  15. export class AppComponent implements OnInit {
  16. constructor(private matAdvancedService: MatAdvancedTableService) {}
  17. columns;
  18. data;
  19. isLoading = false;
  20. ngOnInit(): void {
  21. this.columns = this.matAdvancedService.getColumnsOfType(MockClass);
  22. this.loadData();
  23. }
  24. loadData(user) {
  25. this.isLoading = true;
  26. setTimeout(() => {
  27. this.data = [
  28. {
  29. // some mock data
  30. },
  31. ];
  32. this.isLoading = false;
  33. }, 5000);
  34. }
  35. }

License

MIT