I'm trying to sort a Map using a value convertor on a repeat-binding in Aurelia:
Original code:
<div repeat.for="[id, obj] of myMap">
${obj.SomeProperty}
</div>
This works as expected, the values of the map are displayed and when I add something to the map, it gets added as well.
I then created the following value convertor:
export class SortValueConverter {
toView(map, propertyName, direction) {
const factor = direction === 'desc' ? -1 : 1;
return new Map([...map.entries()].sort((a, b) => (a[propertyName] - b[propertyName]) * factor));
}
}
<div repeat.for="[id, obj] of myMap | sort:'SomeProperty':'desc'">
${obj.SomeProperty}
</div>
The values are getting passed in correctly to the value convertor and the return value is indeed a new map. The repeat binding is broken however, nothing is displayed in the UI.
I tried different approaches as well:
Converting the map into an array before passing it into the value convertor and then using a regular array value convertor:
export class SortValueConverter { toView(array, propertyName, direction) { const factor = direction === 'desc' ? -1 : 1; return array.sort((a, b) => (a[propertyName] - b[propertyName]) * factor)); } } <div repeat.for="obj of Array.from(myMap.values()) | sort:'SomeProperty':'desc'"> ${obj.SomeProperty} </div> // or alternatively: <div repeat.for="obj of [...myMap.values()] | sort:'SomeProperty':'desc'"> ${obj.SomeProperty} </div>
This gives me an undefined
as the first parameter in the value convertor.
Accept a Map as the first parameter of the value convertor, but returning an Array:
export class SortValueConverter { toView(map, propertyName, direction) { const factor = direction === 'desc' ? -1 : 1; return [...map.values()].sort((a, b) => (a[propertyName] - b[propertyName]) * factor)); } } <div repeat.for="obj of myMap | sort:'SomeProperty':'desc'"> ${obj.SomeProperty} </div>
The result is the same as in my first example: the array is returned correctly, but nothing is displayed.
How do you create a value convertor to sort a Map on a repeat-binding?