0

how to create a custom sorting pipe in angular 4

component.ts

@Component({
    selector: 'app-cinemas',
    templateUrl: './cinemas.component.html',
    encapsulation: ViewEncapsulation.None,
    styleUrls: ['./cinemas.component.css']
})
export class CinemasComponent implements OnInit {

    filteredCinema = [];   

 master() {
        this.cinemaService.getCinMaster()
            .subscribe(
                (response: any) => {
                    this.filteredCinema = response;
                },
            //  (error) => console.log(error)
        );}

component.html

<div class="table-scrollable">
<table class="table table-striped table-bordered table-advance table-hover">
<thead>
   <tr>
   <th>Cinema</th>
    <th>Cinema Operator</th>
    <th>Capacity</th>
     <th>Cinema Status</th>
     <th>Unavailable From Date</th>
     <th>Unavailable To Date</th>
     <th>Actions</th>
     </tr>
</thead>
   <tbody>
 <!-- Loop starts here -->
 <tr class="cinema-row cathay" *ngFor="let cintable of filteredCinema | sorting:'cintable.cinema_name' ">
 <td>
  <p class="uppercase">{{cintable.cinema_name }}</p>
 </td>
   <td> 
   <img [src]="getOperatorImg(cintable.cinema_operator_id)" height="30">
     </td>
    <td>
 <small>
Halls:
 <strong>{{cintable.halls.length}}</strong>
 </small>
  </td>
  <td>
<p class="uppercase">{{getStatusName(cintable.status)}}</p>
 </td>
 <td>
 <p class="uppercase">{{cintable.available_date_from | date}}</p>
 </td>
 <td>
 <p class="uppercase">{{cintable.available_date_to | date}} </p>
</td>
 <td>
 <p class="uppercase">{{cintable.entity}} </p>
</td>
<!-- Loop ends here -->
  </tbody>
 </table>
</div>

Pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'sorting'
})
export class SortingPipe implements PipeTransform {  
  transform(value: any, args?: any): any {
    return null;
  }
}

need to sort the cintable.cinema_name but not able to find the solution. Here are the link i have gone through http://hashtagcoder.com/an-introduction-to-angular-cli-part-3-sort-and-filter-data-with-custom-pipes/

https://hassantariqblog.wordpress.com/2017/03/16/angular2-creating-custom-sort-filter-pipe-for-datetime-column-ngfor-directive/

Please help me with this

1 Answer 1

1

The orderBy pipe was removed from Angular 2 because generally filtering logic like this should be done within the component. Since you already return filteredCinema from your component, it would be best to apply custom sorting here.

Assuming you want to have a case-insensitive sorting by cinema_name, do something like this in your subscribe callback:

this.filteredCinema = return items.sort((a, b) => {
    let aLC: string = a.cinema_name.toLowerCase();
    let bLC: string = b.cinema_name.toLowerCase();
    return aLC < bLC ? -1 : (aLC > bLC ? 1 : 0);
});

(Also you should first ensure that response is not empty in this case, for simplicity I left that out)

If you wish filtering to be case-sensitive, simply remove the .toLowerCase calls.

If you still want to do it via a pipe, you should continue reading the tutorial first, because you are lacking the sorting mechanism.

Let's say you wanted a pipe orderByCinemaName:

@Pipe({
  name: 'orderByCinemaName'
})
export class OrderByCinemaNamePipe implements PipeTransform {  
  transform(items: any[]): any[] {
    return items.sort((a, b) => {
        let aLC: string = a.cinema_name.toLowerCase();
        let bLC: string = b.cinema_name.toLowerCase();
        return aLC < bLC ? -1 : (aLC > bLC ? 1 : 0);
    });
  }
}

UPDATE

Fixed the sorting algorithm.

Sign up to request clarification or add additional context in comments.

2 Comments

ERROR in src/app/venues/cinemas/sorting.pipe.ts(10,27): error TS2345: Argument of type '(a: any, b: any) => boolean' is not assignable to parameter of type '(a: any, b: any) => number'. Type 'boolean' is not assignable to type 'number'. getting this error
Sorry, I have used a simplified sorting. I have now fixed that in the code, so it should be fully functional for you now, if you just copy & paste it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.