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.
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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With