Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular get length of filtered ng-repeat

I have upgraded to Angular 1.2.9 from v 1.0.8 to help with the performance of my app.

I have introduced track by which has improved the speed.

What I used to have

<div class="result row"
    data-ng-repeat="result in (filteredItems =
            (results |
            filter: filterPrice |
            filter: filterCategories |
            filter: filterTypes |
            filter: filterAirConditioning |
            filter: filterTransmission |
            filter: filterFourByFour |
            filter: filterFuelType |
            filter: filterNumberOfDoors |
            filter: filterOnAirport |
            filter: filterFuelPolicy |
            filter: filterUnlimitedMileage |
            filter: filterFreeCancellation |
            filter: filterTheftProtection |
            filter: filterCDW |
            filter: filterTaxesIncluded |
            filter: filterProviders ) )  |
        orderBy: [orderByOptions, orderByPriceLowToHigh, orderByPriority] |
        limitTo: pageSize">

what I have now

<div class="result row"
    data-ng-repeat="result in results |
        filter: filterPrice |
        filter: filterCarCategories |
        filter: filterCarTypes |
        filter: filterAirConditioning |
        filter: filterTransmission |
        filter: filterFourByFour |
        filter: filterFuelType |
        filter: filterNumberOfDoors |
        filter: filterOnAirport |
        filter: filterFuelPolicy |
        filter: filterUnlimitedMileage |
        filter: filterFreeCancellation |
        filter: filterTheftProtection |
        filter: filterCDW |
        filter: filterTaxesIncluded |
        filter: filterProviders |
        orderBy: [orderByOptions, orderByPriceLowToHigh, orderByPriority] |
        startFrom: currentPage*pageSize |
        limitTo: pageSize track by $index">

I have pagination which looked for the length of filteredItems. That no longer exists. Also, some other JS needs the filteredItems for logging, analytics, etc

In the HTML view I can get the length of the ng-repeat after filters have been applied

{{(results |
    filter: filterPrice |
    filter: filterCarCategories |
    filter: filterCarTypes |
    filter: filterAirConditioning |
    filter: filterTransmission |
    filter: filterFourByFour |
    filter: filterFuelType |
    filter: filterNumberOfDoors |
    filter: filterOnAirport |
    filter: filterFuelPolicy |
    filter: filterUnlimitedMileage |
    filter: filterFreeCancellation |
    filter: filterTheftProtection |
    filter: filterCDW |
    filter: filterTaxesIncluded |
    filter: filterProviders)
    .length}}

which is a bit of a mouthful but does return the number I want. How can I pass this to a $scope ?

like image 717
Leads Avatar asked Jan 27 '14 11:01

Leads


1 Answers

You can create an intermediate variable that will hold the filtered array:

data-ng-repeat="result in filteredResults = (results | filter:filterPrice | filter:filterCarCategories | etc.)"

and then can use:

filteredResults.length
like image 125
Gruff Bunny Avatar answered Oct 21 '22 02:10

Gruff Bunny