简体   繁体   中英

How to pass multiple pipes for ngx-datatable-column in Angular?

I would like to ask if do you have any ideas how to pass multiple pipes in Angular?

Currently, my code is is passing only one pipe.

const customPipe1 = new CustomPipe2();
const customPipe2 = new CustomPipe2();

  <ngx-datatable-column
      [width]="width"
      [name]="cname"
      [prop]="cbindProperty"
      [pipe]="customPipe1">
  </ngx-datatable-column>

I want to add two or multiple pipes in that column. Something like this in a plain html.

{{ birthday | customPipe1 | customPipe2 }}

This is not possible with ngx-datatable . You can see that here in the source code.

You can however create another pipe which does the same:

It's not necessary to make it a Pipe using the annotation, because you won't use the name, this way you also don't have to add it to the declarations

export class ColumnPipe implements PipeTransform {
  pipes: any[] = [
    new BirthdayPipe(),
    new CustomPipe1(),
    new CustomPipe2()
  ];

  public transform(input: any): any {
    return this.pipes.reduce((output, pipe) => pipe.transform(output), input);
  }
}

It could be possible that your inner pipes need to inject some services. In that case you need to add the pipe to the providers array, together with the pipes that needs injecting and inject them in the constructor of the ColumnPipe .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM