Skip to content

Instantly share code, notes, and snippets.

@daniele-zurico
Created May 28, 2018 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniele-zurico/1905eedcdf912240325d3cedf69a9cee to your computer and use it in GitHub Desktop.
Save daniele-zurico/1905eedcdf912240325d3cedf69a9cee to your computer and use it in GitHub Desktop.
import { Component, OnInit, ViewChild, Input } from '@angular/core';
import { MatPaginator, MatSort } from '@angular/material';
import { NgxDataTableDataSource } from './ngx-data-table-datasource';
import {
trigger,
state,
style,
transition,
animate,
} from '@angular/animations';
@Component({
selector: 'ngx-data-table',
templateUrl: './ngx-data-table.component.html',
styleUrls: ['./ngx-data-table.component.css'],
animations: [
trigger('detailExpand', [
state(
'collapsed',
style({ height: '0px', minHeight: '0', visibility: 'hidden' })
),
state('expanded', style({ height: '*', visibility: 'visible' })),
transition(
'expanded <=> collapsed',
animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')
),
]),
],
})
export class NgxDataTableComponent {
@Input()
set data(_data: any[]) {
this.dataSource = new NgxDataTableDataSource(
this.paginator,
_data,
this.sort
);
this.displayedColumns = Object.keys(_data[0]).filter(
key => key !== 'details'
);
}
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
dataSource: NgxDataTableDataSource;
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns: Array<string>;
expandedElement: Array<string>;
isExpansionDetailRow = (i: number, row: Object) =>
row.hasOwnProperty('detailRow');
/**
* expand collapse a row
* @param row
*/
toggleRow(row) {
if (this.expandedElement === row) {
this.expandedElement = null;
} else {
this.expandedElement = row;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment