Filter array in aurelia view

余生颓废 提交于 2019-11-30 08:41:06

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>

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!