Hello,
I need to have an ability to save the layout order. As example: I have the data of each block size, now I make a reorder of all those blocks and now I need to save it in order to load all the blocks in the order I saved them. Which data should I save to load it in proper order?
Thanks.
I'm pretty sure I've answered this a couple of times here. There should be an old issue or two somewhere in the archives.
<div class="item" data-id="1"></div>.
var order = grid.getItems().map(item => item.getElement().getAttribute('data-id'))
Now you have an array of item ids in correct order, save it where you want in any format you want.
And now when you want to load the saved order do, you can do the following:
var grid = new Muuri('.grid', {
layoutOnInit: false,
sortData: {
id: function (item, element) {
return parseFloat(element.getAttribute('data-id'));
}
}
})
grid.sort('id');
Thank you very much! :)
@ankushgarg1998 how about saving size as well?
Original question seems to be resolved -> closing.
@ankushgarg1998 has given a good method to save item orders in an array.
Further question: The method is not good ennough when length of target list is large, >100 for example.
It's better to get current order of target item before dragging and new order after dragging operation.
Thus a item with data-id="abc"
<div class="item" data-id="abc"></div>
which moved from order 25 to 40 just return data-id, cur_order, new_order:
arr = [data-id, cur_order, new_order];
return arr;
So I can update order of items just between order 25-40 in database rather than all items's order each time.
Would it be posible to do that or there is already a method existed?
@misybing The "move" event provides such data: https://github.com/haltu/muuri/blob/master/README.md#move
It's really cool, love u!
A fully working demo for saving and loading the layout can be found here: https://codepen.io/niklasramo/pen/YveqNN
Most helpful comment
Now you have an array of item ids in correct order, save it where you want in any format you want.
And now when you want to load the saved order do, you can do the following: