Go to Native Libraries and add this Line: <script src="https://cdn.jsdelivr.net/npm/muuri@0.9.3/dist/muuri.min.js"></script>
Add those two anvil roles to the Assets:
Muuri Drag and Drop constists of a grid (which is more or less the container that enables dragging) and items which can be dragged around.
They need special styling, thus we use Anvil Roles to implement that.
Disclaimer
On every update list i destroy and recreate the grid object.
This is not the intended way - but I couldnât get it to work otherwise and it didnât bother me to much.
Feel free to ask, reuse and help improve this drag and drop code for anvil.
In the demo, I hit a bug where if my window is too wide, the items distribute among two columns (and the dragging doesnât work for the second column):
@mark.breuss Have you developed this any further? For instance, have you created a way to access the changed order of the list after the drag and drop? Itâs hard for me to see how one could use the List component as it is.
I needed to get the order of the list after the drag and drop, as hugetim was talking about.
Here is a clone link with one (not particularly good) way of doing this. Basically I added a hidden label capturing the text I wanted and assign that a new âdragresultâ role. We call grid.synchronize per Muuri documentation to make sure the HTML DOM order is consistent with the order we see and then use jQuery to find the dragresult elements, extracting the text from that hidden label.
It seems like this should be possible using the grid.getItems method but youâd still need to use jQuery or something to get to the right HTML element. So I just query on that dragresult role directly.
Ideally this would return the Anvil Python elements in order but this implementation is a ways from that!
You can give it a list of dicts, which are then displayed as a draggable list.
With the method get_sort() the component returns the sorted result.
You should be able to figure out the rest via the code pretty quickly.
@mark.breuss , Iâve had great benefit of the work youâve shared on the dragable list. Thanks!
Iâm using this in my app, and I think it could be worth sharing the version Iâve ended up at that started from your demo app. It has events and works with arbitrary components.
And to keep the train going, I made my own modifications so I could more easily add and remove items from the list when itâs used as a custom component.
I also used Anvil Extras Utils module to inject the Muuri grid CSS that one of yâall added because for some reason it wasnât being respected when I used it as a Custom component in a different app.
Thanks so much for building this so I only had to make a few mods to get it working for my purposes @mark.breuss@stein !
Update: I also added an event for when the list changes (from adding or removing an item or from changing the order) that can be called on instances of the draggable list.
Secondly, is there any way to make the drag switchable - ie so you can turn it on and off from code.
I tried reading the docs and re initing the muuri grid but it clears all the items that have been added.
The second thing I tried was reading this section of the Muuri docs but have no idea how to implement it.