I would like to format a date column as a short date in Angular 2 NGX Datatable.
Here is the HTML:
<ngx-datatable
[rows]="toDos"
[columns]="columns">
</ngx-datatable>
Here is the component TypeScript
import { Component, OnInit } from '@angular/core';
import { IToDo } from '../shared/todo';
import { ToDoService } from '../shared/todo.service';
@Component({
selector: 'app-todo-list',
templateUrl: './todo-list.component.html',
styleUrls: ['./todo-list.component.css']
})
export class TodoListComponent implements OnInit {
toDos: IToDo[];
columns = [
{ prop: 'toDoId' },
{ name: 'To Do', prop: 'name' },
{ prop: 'priority' },
{ prop: 'dueDate' },
{ prop: 'completed' }
];
constructor(private _toDoService: ToDoService) {
}
ngOnInit() {
this.toDos = this._toDoService.getToDos();
}
}
I have tried this in the HTML but it gives a template parse error:
<ngx-datatable
[rows]="toDos"
[columns]="columns"
<ngx-datatable-column prop="dueDate">
<ng-template let-value="value" ngx-datatable-cell-template>
{{value | date[:shortDate]}}
</ng-template>
</ngx-datatable-column>
>
</ngx-datatable>
constructor(
private fb: FormBuilder,
private _currencyPipe: CurrencyPipe,
private _datePipe: DatePipe
) {
this.rows = [
{from: Date().toString(), to: Date(), amount: 100, verified: false},
{from: Date(), to: Date(), amount: 100, verified: true},
];
}
ngOnInit() {
this.columns = [
{ name: 'FROM', prop: 'from', pipe: this.datePipe()},
{ name: 'TO', prop: 'to', pipe: this.datePipe()},
{ name: 'AMOUNT', prop: 'amount', width: 85, pipe: this._currencyPipe},
{ name: 'CONFIRM | DELETE', cellTemplate: this.editCell, width: 155}
];
this.buildForm();
}
datePipe () {
return {transform: (value) => this._datePipe.transform(value, 'MM/dd/yyyy')};
}
You can always use the programatic aproach to applying pipes, infact you could create custom pipes with this aproach.
The pipe @Input
in the ngx-dataTable expects a Pipelike structure with a transform: Function. you could make any function that returns a transform function and manipulate data as you please.
You can just use date pipe
to format value.
{{value | date:'shortDate'}}
is equivalent to {{value | date:'ymd'}}
which outputs date like following: 22/08/2017
Hope this will help you
<ng-template #dateColumn let-row="row" let-value="value" let-i="index">
{{value | date:'dd-MM-yyyy'}}
</ng-template>
export class CvbankListComponent implements OnInit {
@ViewChild('dateColumn') dateColumn: TemplateRef<any>;
.
.
.
this.columns = [
.
.
{ name: 'Date', prop: 'Date', cellTemplate: this.dateColumn },
];
Your HTML is wrong. It should be as shown below.
<ngx-datatable
[rows]="toDos"
[columns]="columns">
<ngx-datatable-column name="dueDate">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.dueDate | date:'shortDate'}}
</ng-template>
</ngx-datatable-column>
</ngx-datatable>
Please find the example code for this here: https://github.com/swimlane/ngx-datatable/blob/master/demo/sorting/sorting-default.component.ts
The live example of this: http://swimlane.github.io/ngx-datatable/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With