Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Pipe ShortDate on NGX Data Table

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>
like image 702
Greg Finzer Avatar asked Mar 30 '17 20:03

Greg Finzer


4 Answers

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.

like image 141
Sebastian Gomez Avatar answered Sep 19 '22 00:09

Sebastian Gomez


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

like image 44
mxr7350 Avatar answered Sep 18 '22 00:09

mxr7350


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 },
    ];
like image 38
Amalan Shenll Avatar answered Sep 21 '22 00:09

Amalan Shenll


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/

like image 38
Sudhakar Avatar answered Sep 19 '22 00:09

Sudhakar