I recently started using covalent datatable's atomic components as I hav to display a custom column with action buttons. Right now I am just trying to bind/display my data but something is going wrong. I get the following error:
ng:///AppModule/CashierMaintenanceComponent.ngfactory.js:45 ERROR TypeError: Cannot read property 'name' of undefined
I have no idea what could be causing this... Any idea what's going on here/how to fix it?
my html code:
<br /><br /><br />
<div class="noOverflow" fxLayout fxLayoutAlign="center start" fxLayoutWrap>
<div fxFlex="100%">
<div fxLayout fxLayoutAlign="start start">
<div fxFlex="50%">
<h1>Cashier Maintenance</h1>
</div>
<div fxFlex="40%" fxFlexOffset="60%">
<button color="primary" mat-raised-button><mat-icon class="fa fa-plus"></mat-icon> New</button>
<button mat-raised-button><mat-icon class="fa fa-edit"></mat-icon> Edit</button>
<button mat-raised-button><mat-icon class="fa fa-trash"></mat-icon> Delete</button>
</div>
</div>
<div fxLayout fxLayoutAlign="center start">
<div fxFlex="100%">
<mat-form-field floatPlaceholder="never">
<input matInput #filterTbl placeholder="filter">
</mat-form-field>
</div>
</div>
<div fxLayout class="noOverflow" fxLayoutAlign="center start">
<div fxFlex="80%" fxFlexOffset="10%">
<div class="mat-elevation-z8 noOverflow">
<table *ngIf="filteredEmployees.length > 0" td-data-table>
<thead>
<tr td-data-table-column-row>
<th td-data-table-column [sortable]="true" [sortOrder]="ASC" *ngFor="let column of columns">
{{column.label}}
</th>
</tr>
</thead>
<tbody>
<tr td-data-table-row *ngFor="let row of filteredEmployees">
<td td-data-table-cell="let column of columns">
{{row[column.name]}}
</td>
</tr>
</tbody>
</table>
<td-paging-bar #pagingBar [pageSize]="pageSize" [total]="filteredTotal" (change)="page($event)">
<span>Rows per page:</span>
<mat-select class="noOverflow" [style.width.px]="50" [(ngModel)]="pageSize">
<mat-option *ngFor="let size of [10, 20, 50,100,200,500]" [value]="size">
{{size}}
</mat-option>
</mat-select>
<span class="noOverflow">{{pagingBar.range}} of {{pagingBar.total}}</span>
</td-paging-bar>
</div>
</div>
</div>
</div>
</div>
my ts/component code:
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import { TdDataTableService, TdDataTableSortingOrder, ITdDataTableSortChangeEvent, ITdDataTableColumn } from '@covalent/core';
import { IPageChangeEvent } from '@covalent/core';
import { RestService } from '../../services/rest.service';
import { IEmployee } from '../../interfaces/employee.interface';
import { StringToDatePipe } from '../../pipes/string-to-date.pipe';
@Component({
selector: 'app-cashier-maintenance',
templateUrl: './cashier-maintenance.component.html',
styleUrls: ['./cashier-maintenance.component.css']
})
export class CashierMaintenanceComponent implements OnInit {
@ViewChild('filterTbl') filterTbl: ElementRef;
employees: IEmployee[];
filteredEmployees: IEmployee[] = [];
filteredTotal: number = 100;
columns: ITdDataTableColumn[] = [
{ name: 'EMP_ID', label: 'ID', sortable: true, width: 100 },
{ name: 'EMP_NM', label: 'Name', sortable: true, width: 200 },
{ name: 'EMP_TYP', label: 'Type', sortable: true, width: 200 },
{ name: 'TEL', label: 'Telephone', sortable: true, width: 200 },
{ name: 'ADDR', label: 'Address', sortable: true, width: 200 },
{ name: 'DT_STRT', label: 'Start Date', sortable: false, width: 200 }
];
searchTerm: string = '';
sortBy: string = 'EMP_NM';
fromRow: number = 1;
currentPage: number = 1;
pageSize: number = 10;
sortOrder: TdDataTableSortingOrder = TdDataTableSortingOrder.Ascending;
constructor(private _dataTableService: TdDataTableService, private restService: RestService) { }
sort(sortEvent: ITdDataTableSortChangeEvent): void {
this.sortBy = sortEvent.name;
console.log(sortEvent.name);
this.filter();
}
search(searchTerm: string): void {
this.searchTerm = searchTerm;
this.filter();
}
page(pagingEvent: IPageChangeEvent): void {
this.fromRow = pagingEvent.fromRow;
this.currentPage = pagingEvent.page;
this.pageSize = pagingEvent.pageSize;
this.filter();
}
filter(): void {
let newData: IEmployee[] = this.employees;
let excludedColumns: string[] = this.columns
.filter((column: ITdDataTableColumn) => {
return ((column.filter === undefined && column.hidden === true) ||
(column.filter !== undefined && column.filter === false));
}).map((column: ITdDataTableColumn) => {
return column.name;
});
newData = this._dataTableService.filterData(newData, this.searchTerm, true, excludedColumns);
this.filteredTotal = newData.length;
newData = this._dataTableService.sortData(newData, this.sortBy, this.sortOrder);
newData = this._dataTableService.pageData(newData, this.fromRow, this.currentPage * this.pageSize);
this.filteredEmployees = newData;
}
getEmployees() {
this.restService.getEmployees()
.subscribe(
(res) => {
console.log(res);
this.employees = res;
this.employees.forEach((emp: IEmployee) => {
emp.DT_STRT = new StringToDatePipe().transform(emp.DT_STRT);
});
this.filter();
}, (err) => {
console.log(err);
});
}
ngOnInit() {
Observable.fromEvent(this.filterTbl.nativeElement, 'keyup')
.debounceTime(150)
.distinctUntilChanged()
.subscribe(() => {
this.searchTerm = this.filterTbl.nativeElement.value;
this.filter();
});
this.getEmployees();
}
}
There is a typo in your td-data-table-row
component. You should be using *ngFor
. See this stackblitz for the working code
<tr td-data-table-row *ngFor="let row of filteredEmployees">
<td td-data-table-cell *ngFor="let column of columns">
{{row[column.name]}}
</td>
</tr>
For simple use cases, using the data table without the atomic components can help avoid the more complex template code. Simply pass in your data and configuration as inputs to <td-data-table>
<td-data-table
#dataTable
[data]="filteredData"
[columns]="columns"
[sortable]="true"
sortOrder="ASC"
[style.height.px]="200">
</td-data-table>