How to find the count of items in an ngFor after the pipes have been applied

前端 未结 9 1690
面向向阳花
面向向阳花 2020-12-06 04:18

I have an ngFor creating rows in a table that is both filtered and paged.



        
9条回答
  •  我在风中等你
    2020-12-06 05:01

    You can get the count of the items by transforming the array within a pipe.

    The idea is that the pipe would transform the array into another array where each element has an item property, and a parent property representing the filtered (or original) array:

    @Pipe({ name: 'withParent', pure: false })
    export class WithParentPipe implements PipeTransform {
        transform(value: Array, args: any[] = null): any {
    
            return value.map(t=> {
                return {
                    item: t,
                    parent: value
                }
            });
        }
    } 
    

    Here is how it would be used:

     
            Count:  {{d.parent.length }}
            Item:  {{ d.item.name}}
     
    

提交回复
热议问题