material-dynamic-table
Dynamic table component for angular built on top of angular material table. It offers sorting, pagination, filtering per column and the ability to specify content types and components used for displaying them. The initial purpose of this library was to display data coming from OData API, although it can work with MatTableDataSource (however it needs to be extended to enable filtering - see example).
Demo
Online demo: https://stackblitz.com/edit/dynamic-table
Run ng serve
for the main project to launch demo for this library.
Getting started
1. Prerequisites:
Angular material: Please follow https://material.angular.io/guide/getting-started The supported version of Angular Material will be indicated by the major version number of this library. Version 8.3.0 is for Angular Material ^8.0.0, version 9.3.0 is for ^9.0.0, while versions <1.3.0 should work for versions <8.0.0.
Filter is using material icon, so adding material icons may be needed as well: https://material.angular.io/guide/getting-started#step-6-optional-add-material-icons
2. Install material-dynamic-table:
npm install material-dynamic-table --save
3. Import the installed libraries:
;; ; ;
mdt-dynamic-table
in your component:
4. Include mdt-dynamic-table ="columns" ="dataSource"/mdt-dynamic-table
5. Specify column definitions and data source:
; ;
Further info
API reference for material-dynamic-table
Properties
Name | Description |
---|---|
@Input() columns: ColumnConfig[] | Column definition for dynamic table, order will determine column order |
@Input() dataSource: DataSource | Data source that provides data for dynamic table |
@Input() pageSize: number | Initial page size for pagination - default 20 |
@Input() pageSizeOptions : number[] | The set of provided page size options to display to the user. |
@Input() showFilters: boolean | If the filters are defined adds the ability to turn them off - default true |
@Input() stickyHeader : boolean | Whether the table should have sticky header |
@Input() paginator : MatPaginator | Paginator to be used instead of internal paginator or null to hide internal |
Methods
Name | Description |
---|---|
getFilter(columnName: string): any | Returns currently set filter for the column with provided name |
setFilter(columnName: string, filter: any) | Sets the filter for the column with provided name |
getFilters() | Returns all set column filters |
clearFilters() | Removes all applied filters |
ColumnConfig definition
ColumnConfig is used to provide specification for the columns to be displayed
Property | Description |
---|---|
name | Name of the property to display - it should match propery name from data source |
displayName | Name to be displayed in column header |
type | Type of the data displayed by this column - it should match one of your defined cell types |
options | Optional field that can be used to pass extra data for cells |
sticky | Optional field that can make column sticky to start or end of table. Values: 'start', 'end' |
sort | Optional field that can disable sort on the column if the value is false |
Cell types
By default there are two types provided:
string
This displays plain string value for property defined under name
in ColumnConfig.
This is the default type used if there is no type specified for the data type.
date
This type will format property from ColumnConfig.name
as date. It can take additional parameter in ColumnConfig.options
- dateFormat
, which specifies what date format should be used (default is 'short')
Adding additional cell types
New cell types can be defined by adding a component, inheriting from CellComponent
Here is an example of options cell that can be used for showing possible actions
;;;
Cell types then need to be registered:
;
Adding filters
Filters icon an the column will be displayed whenever there is a registered filter for that columns type. To add a filter first create a component for modal dialog with a model that implements your filter interface. Then register it in the following way:
;
In this case it is a filter for string
cell type named TextFilterComponent
. See the example project for full design.
To make use of filters you need to have data source that can handle them. See FilteredDataSource
from the example to see how MatTableDataSource
can be extended to handle it.
Filters can have a description that is displayed when the filter is applied. To set the description for the filter the filter model should have a method getDescription that returns a string. Implement interface 'FilterDescription' for your filter model to have the description displayed.