Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call an Angular2 pipe from a pipe?

I get back from the server an object containing an array of columns with their definitions and an array of rows. I would like to iterate over the columns and rows as I build my HTML table and format the cells according to the column type returned using a "format" pipe.

For example

WS Response:

{
  "columns" [{"precision":10,"name":"PAGES","typeName":"INTEGER","scale":0...,
  "rows":[{"PAGES":6531....}, [{"PAGES":6531....}]
}

HTML fragment:

<tr *ngFor="let row of invoices?.rows">
  <td *ngFor="let column of invoices?.columns>
    {{row[column.name] | format : column}}
  </td>
</tr>

Is there any way my "format" pipe can just act a a delegator to the correct built-in pipe (where one exits), depending on the type of the column? I don't want to have to re-implement DecimalPipe, DatePipe, etc. etc.

like image 208
Tim Avatar asked Nov 16 '16 13:11

Tim


1 Answers

Yes you can call pipes - just instatiate them and call transform:

transform(cell: any, column: any): any {
    if (column.typeName === "INTEGER") {
        let pipe = new DecimalPipe();
        return pipe.transform(cell, "1.0-0");
    }
    // more here ...
}
like image 191
rinukkusu Avatar answered Oct 17 '22 18:10

rinukkusu