Created
May 28, 2016 00:30
-
-
Save httpJunkie/431561420d62a746e02eef2d218d07d1 to your computer and use it in GitHub Desktop.
Aurelia ValueConverter - Array Sort & Filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class FilterValueConverter { | |
toView(array, property, query) { | |
if (query === void 0 || query === null || query === "" || !Array.isArray(array)) { | |
return array; | |
} | |
let properties = (Array.isArray(property) ? property : [property]), | |
term = String(query).toLowerCase(); | |
return array.filter((entry) => | |
properties.some((prop) => | |
String(entry[prop]).toLowerCase().indexOf(term) >= 0)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
repeat.for="s of surveys | filter: ['name','description']: searchterm | sort: 'name':'ascending'" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class SortValueConverter { | |
toView(array, property, direction) { | |
if (!array) | |
return array; | |
let pname = property; | |
let factor = direction.match(/^desc*/i) ? 1 : -1; | |
var retvalue = array.sort((a, b) => { | |
var textA = a.toUpperCase ? a[property].toUpperCase() : a[property]; | |
var textB = b.toUpperCase ? b[property].toUpperCase() : b[property]; | |
return (textA < textB) ? factor : (textA > textB) ? -factor : 0; | |
}); | |
return retvalue; | |
} | |
} |
- You can drop
let pname = property;
because it is not used
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I believe there is an error here, the items sorted probably wont have a "toUpperCase" method. I changed those rows to