6

我想在 Angular 2 NGX Datatable 中将日期列格式化为短日期。

这是HTML:

<ngx-datatable
  [rows]="toDos"
  [columns]="columns">
</ngx-datatable>

这是组件 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();
  }
}

我在 HTML 中试过这个,但它给出了一个模板解析错误:

<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>
4

5 回答 5

8
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')};
}

您始终可以使用编程方法来应用管道,事实上您可以使用这种方法创建自定义管道。@Inputngx-dataTable 中的管道需要一个带有转换的管道结构:函数。您可以制作任何返回转换函数并随意操作数据的函数。

于 2020-03-08T16:54:48.680 回答
6

您可以只使用date pipe格式化值。

{{value | date:'shortDate'}}相当于{{value | date:'ymd'}}which 输出日期如下:22/08/2017

于 2017-08-22T13:04:23.447 回答
4

希望对你有帮助

  <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 },
    ];
于 2019-07-09T09:34:25.273 回答
2

我认为这种pipe在列配置上使用属性的方式非常干净(不确定 Angular 2 是否能够像 OP 要求的那样处理这个问题,但 Angular 10 可以)

class DateOnlyPipe extends DatePipe {
  public transform(value): any {
    return super.transform(value, 'MM/dd/y');
  }
}

const columns = [{
  name: 'Date',
  prop: 'dateReported',
  pipe: new DateOnlyPipe('en-US')
}];

来源:https ://github.com/swimlane/ngx-datatable/issues/401#issuecomment-311635384

于 2021-01-09T00:28:49.483 回答
2

你的 HTML 是错误的。它应该如下图所示。

<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>

请在此处找到示例代码:https ://github.com/swimlane/ngx-datatable/blob/master/demo/sorting/sorting-default.component.ts

活生生的例子:http ://swimlane.github.io/ngx-datatable/

于 2018-09-19T12:00:13.610 回答