Filter array in aurelia view

I am using aurelia and want to filter a collection (array) in view rather than in view model.

I am trying the following syntax to do so:

<div class="col-sm-7  col-md-7 col-lg-7 ${errors.filter(function(err){return err.Key==='car.Model';}).length >0? 'alert alert-danger' :''}">
            <span repeat.for="error of errors">
                <span if.bind="error.Key==='car.Model'">
                    ${error.Message}
                </span>
            </span>
</div>

And I am getting following error in browser console:

Error: Parser Error: Missing expected ) at column 28 in [errors.filter(function(err){return err.Key==='car.Model';].

This is possible in angularJS as follows:

 <div class="small-7  medium-7 large-7 columns end">
        <span class="error" ng-repeat="error in errors  | filter:{ Key: 'car.Model'}">
            <span class="error-message">
                {{error.Message}}
            </span>
        </span>
    </div>

Is similar thing also possible in aurelia?

I would also love to know how a collection/array can be filtered in repeat.for in aurelia (similar to ng-repeat). I tried to use filter function in similar fashion but it too didn't work and I got similar error.

Answers:

Answer

It's a little embarrassing. But after a little research (which I should have done beforehand :P) I have got the answer.

It can be done using ValueConverter as shown here: http://jdanyow.github.io/aurelia-converters-sample/.

And I think it's quite cool.

Now my code looks like this:

<div class="col-sm-7  col-md-7 col-lg-7 alert alert-danger" if.bind="errors | hasPropertyValue:'Key':'car.Model'">
    <span repeat.for="error of errors | filterOnProperty:'Key':'car.Model'">
        <span>
           ${error.Message}
        </span>
    </span>
</div>

And I have defined couple of valueconverters in TypeScript (valueconverters.ts):

export class FilterOnPropertyValueConverter {
toView(array: {}[], property: string, exp: string) {

    if (array === undefined || array === null || property === undefined || exp === undefined) {
        return array;
    }
    return array.filter((item) => item[property].toLowerCase().indexOf(exp.toLowerCase()) > -1);
}
}

export class HasPropertyValueValueConverter {
toView(array: {}[], property: string, exp: string) {

    if (array === undefined || array === null || property === undefined || exp === undefined) {
        return false;
    }
    return array.filter((item) => item[property].toLowerCase().indexOf(exp.toLowerCase()) > -1).length > 0;
}
}

And I finally imported these in my view: <import from="yourPath/valueconverters"></import>

Answer

Good to learn how to use Aurelia in the similiar way. How about get the count of the filtered list like the following angular 1 code?

<div class="small-7  medium-7 large-7 columns end">
        <span class="error" ng-repeat="error in filtered = (errors  | filter:{ Key: > 'car.Model'})">
            <span class="error-message">
                {{error.Message}}
            </span>
        </span>
    </div>
            <div class="row text-center" ng-if="errors.length>0">
                Total records: {{filtered.length}}
            </div>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.