Binding to sort order
Hi, I wont to save the sort order in the model list.
Hoe can i do so please? it is be nice is changing the order of the array...
Hey @yantrab,
At the moment the grid doesn't sort the items at all and stores them in the order that they were originally added to the grid. I'd like to update the cascade functionality to actually have some concept of item order, but I can imagine that being a long way off, I'm afraid. In the mean time, if you bind to the positions of your items, you can sort using a variant of the following:
items.sort((a, b) => {
if (a.row == b.row) {
return a.col == b.col ? 0 : (a.col < b.col ? -1 : 1);
} else {
return a.row < b.row ? -1 : 1;
}
});
Hey Ben.
Finally i found the way:
onDragStop(item) {
item._ngGrid._items.forEach((e) => {
var id = e._ngEl.nativeElement.id;
this.categories.filter(a => a.id == id)[0].sort = e._row;
});
this.categories = this.categories.sort(function (a, b) { return (a.sort > b.sort) ? 1 : ((b.sort > a.sort) ? -1 : 0); });
}
tenks for the great package!
If you wanted to simplify that a bit there's a payload property for the items, meaning you could just do:
onDragStop(item) {
item._ngGrid._items.forEach((e) => {
var id = e.payload;
this.categories.filter(a => a.id == id)[0].sort = e._row;
});
this.categories = this.categories.sort((a, b) => a.sort - b.sort);
}
Alternatively, there's also an onItemChange in the grid which returns all items after a drag or resize which you could bind to using the following:
onItemChange(items) {
items.forEach((item) => {
this.categories.filter(a => a.id == item.payload)[0].sort = e._row;
});
this.categories = this.categories.sort((a, b) => a.sort - b.sort);
}