Skip to content

Instantly share code, notes, and snippets.

@httpJunkie
Created May 28, 2016 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save httpJunkie/431561420d62a746e02eef2d218d07d1 to your computer and use it in GitHub Desktop.
Save httpJunkie/431561420d62a746e02eef2d218d07d1 to your computer and use it in GitHub Desktop.
Aurelia ValueConverter - Array Sort & Filter
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));
}
}
repeat.for="s of surveys | filter: ['name','description']: searchterm | sort: 'name':'ascending'"
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;
}
}
@deap82
Copy link

deap82 commented Mar 20, 2019

I believe there is an error here, the items sorted probably wont have a "toUpperCase" method. I changed those rows to

var textA: string = a[property] && a[property].toUpperCase ? a[property].toUpperCase() : a[property];
var textB: string = b[property] && a[property].toUpperCase ? b[property].toUpperCase() : b[property];

@DonkeyKongJr
Copy link

  • 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