Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 405
Sayan Pal Avatar asked Mar 20 '15 08:03

Sayan Pal


People also ask

How do you apply an array filter?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.

Can you filter an array?

JavaScript Array filter()The filter() method creates a new array filled with elements that pass a test provided by a function. The filter() method does not execute the function for empty elements. The filter() method does not change the original array.

How do you filter a string array?

To filter strings of an Array based on length in JavaScript, call Array. filter() method on this String Array, and pass a function as argument that returns true for the specific condition on string length or false otherwise.

What is array prototype filter?

prototype. filter() The filter() method creates a new typed array with all elements that pass the test implemented by the provided function. This method has the same algorithm as Array.


1 Answers

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>

like image 156
Sayan Pal Avatar answered Sep 21 '22 08:09

Sayan Pal