ngx-datatable的使用

ionic5+angular7混合开发中ngx-datatable的使用

效果图:移动端模式,固定表头和第一列,左右上下滑动

安装环境:

npm install @swimlane/ngx-datatable --save

1)先在app.module.ts中引入并注册:
(在ngx-datatable中使用过滤器需要)

import { DateFilterPipe } from "./common/pipes/date-filter/date-filter.pipe";
@NgModule({
   providers: [ DatePipe ]
})

2)在module.ts中引入注册

import { NgxDatatableModule } from '@swimlane/ngx-datatable';

@NgModule({
  imports: [
    NgxDatatableModule
  ],
  declarations: [IroncanMsgPage]
})

3)在用到的页面.ts中:
引入过滤器:

`import { DateFilterPipe } from "src/app/common/pipes/date-filter/date-filter.pipe";`

声明过滤器:

`private dateFilterPipe: DateFilterPipe`

应用过滤器:

`{ prop: 'GROSS_WEIGHT_TIME', name: '毛重时间', pipe: this.dateFilterPipe }`

4).html

  <ngx-datatable
    class="material fixedheader1"
    style="height:99%; text-align: center;"
    [rows]="rows"
    [columns]="columns"
    columnMode="'force'"
    [headerHeight]="50"
    [rowHeight]="50"
    [scrollbarH]="true"
    [scrollbarV]="true"
    >
  </ngx-datatable>

//data-filter.pipe 定义时间过滤器

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
  name: 'dateFilter'
})
export class DateFilterPipe implements PipeTransform {
  transform(value: any, ...args: any[]): any {
    if (value === undefined) {
      return '';
    } else {
      if (value.length === 8) {
        return value.slice(0, 4) + '-' + value.slice(4, 6) + '-' + value.slice(6, 8);
      } else {
        return value.slice(0, 4) + '-' + value.slice(4, 6) + '-' + value.slice(6, 8) + ' ' + value.slice(8, 10) + ':' + value.slice(10, 12);
      }
      }
  }
}

//columns 表头

  public rows: any[] = []; //查询数据
  public columns = [
    { prop: 'TPC_NO', name: '罐号', width: 80, frozenLeft: true },
    { prop: 'IR_TAP_NO', name: '铁次号', width: 80 },
    { prop: 'DEST', name: '去向', width: 80 },
    { prop: 'TPC_STATUS', name: '罐子状态', width: 80 },
    { prop: 'GROSS_WEIGHT', name: '毛重', width: 80 },
    { prop: 'GROSS_WEIGHT_TIME', name: '毛重时间', pipe: this.dateFilterPipe },
    { prop: 'BACKN5', name: '皮重', width: 80 },
    { prop: 'TARE_WEIGHT_TIME', name: '皮重时间', pipe: this.dateFilterPipe },
    { prop: 'TRUCK_NO', name: '架号', width: 80 },
    { prop: 'CURRENT_CAR_SEQ', name: '工位号', width: 80 },
    { prop: 'APPLY_FULL_TIME', name: '计量预报时间', pipe: this.dateFilterPipe },
    { prop: 'PIRON_START_TIME', name: '出铁开始时间', pipe: this.dateFilterPipe },
    { prop: 'PIRON_END_TIME', name: '受铁结束时间', pipe: this.dateFilterPipe }
  ];

//后台返回的数据 rows格式:

//参考:
https://stackoverflow.com/questions/57405789/need-to-apply-pipe-for-specific-property-out-of-many-property
在这里插入图片描述
官方文档:https://swimlane.gitbook.io/ngx-datatable/api/column/inputs

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值