Draggable list stops working on refresh #3830
Replies: 2 comments 2 replies
-
I suspect it's because on refresh DOM is being rebuilt again and the binding to your previous list element is broken. You need to recreate |
Beta Was this translation helpful? Give feedback.
-
Ok, after looking through the examples I was able to make a working Sortable Column that also supports ui.refreshable and doesn't do anything to hacky. It also supports the 'group' function of sortable allowing you to have multiple list that you can drag inbetween. The only issue is that I can't get an event that has both the new list id and old list id so there isn't a way to tell where the dragged item went. from typing import Callable, Optional
from nicegui import ui
class SortableColumn(ui.element, component='sortable_column.js'):
def __init__(self, *, on_change: Optional[Callable] = None, group:str = None) -> None:
super().__init__()
self.on('item-drop', self.drop)
self.on_change = on_change
self._classes.append('nicegui-column')
self._props['group'] = group
def drop(self, e) -> None:
if self.on_change:
self.on_change(e)
else:
print(e)
def makeSortable(self) -> None:
self.run_method('makeSortable')
def getitems(self) -> None:
return self.run_method('getitems')
def on_change(e):
print(e)
def refresh():
draw.refresh()
@ui.refreshable
def draw():
ui.button('reset').on_click(refresh)
with ui.row():
with ui.column():
with SortableColumn(on_change=on_change,group='test') as c1:
for i in range(10):
with ui.card():
ui.label(f"Card {i}")
ui.label(c1.id)
with ui.column():
with SortableColumn(on_change=on_change,group='test') as c2:
for i in range(10):
with ui.card():
ui.label(f"Card {i*10}")
ui.label(c2.id)
draw()
ui.run() then the js code in sortable_column.js import 'https://cdnjs.cloudflare.com/ajax/libs/Sortable/1.15.0/Sortable.min.js'
export default {
template: `
<div>
<slot></slot>
</div>
`,
props: {
group: String,
},
mounted() {
this.makesortable();
},
methods: {
makesortable() {
if (this.group === 'None') {
this.group = this.$el.id;
}
Sortable.create(this.$el, {
group: this.group,
animation: 150,
ghostClass: 'opacity-50',
onEnd: (evt) => this.$emit("item-drop", {
parent: parseInt(this.$el.id.slice(1)),
id: parseInt(evt.item.id.slice(1)),
new_index: evt.newIndex,
new_list: evt.to,
old_list: evt.from,
event: evt,
}),
});
},
},
}; Link to a repo with this example: https://github.com/itworkedlastime/nicegui-sortable-column |
Beta Was this translation helpful? Give feedback.
-
Question
I used this discussion to create a draggable list of ui.cards() Better support for drag&drop
This works well but the isssue is that there are other parts of the code that can update the order of the list and when I call refresh() the sortable JS code stops working.
Here is some example code that shows the issue
Beta Was this translation helpful? Give feedback.
All reactions