multiple pipes and does not work

12 views
Skip to first unread message

Dev C

unread,
Jul 24, 2019, 1:19:07 PM7/24/19
to ang...@googlegroups.com
     <li *ngFor="let c of (listName | sort ) | search : searchText;  let i = index;  " class="advance-list-heading" >

I have above .html file where in sort array does not work, I mean I put console.log in it but it never prints nor it sorts.

Thanks

Tito

unread,
Jul 24, 2019, 1:35:55 PM7/24/19
to Angular and AngularJS discussion
is this for AngularJS or angular ?

Dev C

unread,
Jul 24, 2019, 1:37:09 PM7/24/19
to ang...@googlegroups.com
angular 6

--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/234485a1-417f-4f9c-b460-6a4b3a02682d%40googlegroups.com.

Sander Elias

unread,
Jul 24, 2019, 1:54:21 PM7/24/19
to Angular and AngularJS discussion
Hi dev,

Can you put a sample on stackblitz?Iit's hard to help you without seeing your code.

Regards
Sander

Dev C

unread,
Jul 24, 2019, 11:08:33 PM7/24/19
to ang...@googlegroups.com
Hello Sander, below is the code for sort pipe.

import {Pipe, PipeTransform} from '@angular/core'
@Pipe({
name: "sort"
})
export class ArraySortPipe implements PipeTransform {
transform(array: Array<string>, args: string): Array<string> {
array.sort((a: any, b: any) => {
console.log("22")
if (a < b) {
console.log("!")
return -1;
} else if (a > b) {
console.log("2")
return 1;
} else {
console.log("3")
return 0;
}
});
return array;
}
}
and below is the code for search pipe
import {Pipe, PipeTransform} from '@angular/core';


@Pipe({name: 'search'})
export class SearchPipe implements PipeTransform {
transform(value: any[], term: string): any[] {
return value.filter((x: any) => x.name.toLowerCase().startsWith(term.toLowerCase()))
}
}



--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.

Sander Elias

unread,
Jul 25, 2019, 1:41:22 AM7/25/19
to Angular and AngularJS discussion
Hi Dev,

Your search pipe expects an object with at least the property name in each row, and your sort expects an array that contains only strings. Make sure you have those in sync.
I changed them a bit, so they now accept a property's name:

@Pipe({name: "sort"})
export class ArraySortPipe implements PipeTransform {
transform(array: Array<string>, propName: string): Array<string> {
return [...array.sort((a: any, b: any) => a[propName] < b[propName] ? -1 : 1)]
}
}

@Pipe({ name: 'search' })
export class SearchPipe implements PipeTransform {
transform(value: any[], prop,term: string): any[] {
return value.filter((x: any) => term === '' || x && x[prop] && x[prop].toLowerCase().includes(term.toLowerCase()))
}
}


Regards
Sander

Dev C

unread,
Jul 25, 2019, 3:24:29 AM7/25/19
to ang...@googlegroups.com
Hello Sendar
This helps , also array has name and description, is it possible to search in both rather just in one name ?
--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/c90588ab-3375-4f70-b992-3ad2c5626874%40googlegroups.com.


--
Sent from my mi note 4 phone.
Reply all
Reply to author
Forward
0 new messages