Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple pipes simultaneously in Angular 2

I am working on an Angular 2 project.

I have a table with multiple columns. Every column has different logic for sorting (number, string.lowercase, amountValue in % and INR). Rows are sorted on the basis of sorting logic of that column. To acheive this, I am using a custom pipe named sortTable with few arguments.

At the same time, there is an input field at the top that binds to searchTerm variable. To filter the data with searchTerm, I am using another custom pipe named sortTableRow.

Although it is very complicated, a very simplified snippet can be:

<input type="search" [(ngModel)]="searchTerm"/>

<table>
  <thead>
    <tr class="sortable-header">
        <th data-key="status" data-dt="boolean">Status</th>
        <th data-key="name" data-dt="string">Name</th>
        <th data-key="code" data-dt="string">Code</th>
        <th data-key="amountValue" data-dt="amount">Amount</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let row of rows | sortTable: sortType : {'key': key, 'dt': dt} | searchTableRow : searchTerm : ['name']">
        <td> {{row.status}} </td>
        <td> {{row.name}} </a> </td>
        <td> {{row.code}} </td> 
        <td> {{row.amountValue}} </td>
    </tr>
  </tbody>
</table>

Both pipes work fine individually. When I click on column header, key and dt(dataType) gets changed on a click event handler and rows get sorted accordingly. When I search for a term, results get filtered and I see desired output.

But when I try to sort the FILTERED RESULTS (by searchTerm), nothing happens. I think in this case, two pipes not working simultaneously. I don't want to remove any of these pipes.

like image 959
Anonym Avatar asked Apr 29 '17 18:04

Anonym


People also ask

Can we chain multiple pipes in Angular?

Router service needs to be explicitly provided in angular module to use it in another component via DI. You can chain multiple pipe in a single expression along with “async” pipe.

What is correct way to apply multiple pipes in Angular?

here i m using two pipe. one is calles filter with pass value second is itemdisplayno. Basically first apply the filter with pass value then run the itemdisplayno. Save this answer.

Which character is used for chaining multiple pipes in Angular?

Pipes are defined using the pipe “|” symbol. Pipes can be chained with other pipes.

When you chain multiple pipes together what order will they be executed Angular?

Pipes are executed in left-to-right order, so the order is important. In your example, only the second version makes sense. You want to filter first, then select the first two of what remains, and not the other way around.


3 Answers

You can apply the pipe using pipe sign. like

<div class="checkbox" *ngFor="let value of brand | filter: {name: searchbrand} | itemdisplayno: displayitems; let i = index">

here i m using two pipe. one is calles filter with pass value second is itemdisplayno. Basically first apply the filter with pass value then run the itemdisplayno.

like image 163
vishal dobariya Avatar answered Oct 20 '22 17:10

vishal dobariya


hm... very strange. may be this help

<tr *ngFor="let row of (rows | sortTable: sortType : {'key': key, 'dt': dt}) | searchTableRow : searchTerm : ['name']">

if not try to set console.log in each pipe and watch what they return

like image 20
dimson d Avatar answered Oct 20 '22 15:10

dimson d


This is happening because you first sort the list and then apply filtering. To fix it just simply move the filter pipe before sorting pipe in your html code.

`<tr *ngFor="let row of (rows | searchTableRow : searchTerm : ['name']) | sortTable: sortType : {'key': key, 'dt': dt}">`
like image 23
Ramin Ar Avatar answered Oct 20 '22 17:10

Ramin Ar