sortable icon indicating copy to clipboard operation
sortable copied to clipboard

Sortable capabalities are lost when injecting new content

Open Djoul opened this issue 6 years ago • 6 comments

When adding new content dynamically within the list of content items, the sort does not work anymore. The new item is added on top of the list but it's not possible to re-order / neither drag the items which are on the list. The pages has to be reloaded / refreshed to make it works again. For informaton - the same issue does not appears when content items are removed. Anway to solve this issue? Thanks

Djoul avatar Nov 24 '20 13:11 Djoul

Did you solve this? I have the same problem

willmkt avatar Dec 06 '20 01:12 willmkt

Nope. Not solved yet.

Djoul avatar Dec 15 '20 07:12 Djoul

I ran into the same trouble, tried restarting livewire and rescanning for livewire components after the update but didn't help.

When you put in:

    Livewire.hook('element.initialized', (element, component) => {
        console.log('initialized ' + element);
    })

The elements are initialized once they're added but the sorting doesn't work. My educated guess is that livewire is correctly initializing the elements but Sortable isn't initialized/refreshed after the new elements are added and therefore the livewire actions are neither called. Only strange that that doesn't produce any console errors, you'd expect some if sortable has trouble initializing on the new group of elements. I'm trying to figure it out but no luck so far.

bramcou avatar Jan 18 '21 01:01 bramcou

@Djoul @willmkt I resolved to use another sortable library and a very easy livewire implementation, 5min time to implement: Used: https://github.com/SortableJS/Sortable

The SortableJS library has a store event which runs after the sequence of elements you’re updating has changed. Simply emit the new sort in a livewire event to your livewire component and save the new order from there.

Run the initialize function on page load and when the component updates, emit an event from the component to trigger the Livewire.on method to reinitialize the sortable.

    //initialize sortable
    function initSortable() {

        //the sortable element
        var el = document.getElementById('sortableImages');

        var sortable = Sortable.create(el, {
            store: {
                set: function (sortable) {
                    Livewire.emit('imageSortUpdated', sortable.toArray());
                }
            }
        });
    }

window.onload = function() {
        initSortable();
    }

    Livewire.on('initiateSortable', () => {
        initSortable();
    });

The HTML part:

<div id="sortableImages" class="grid grid-cols-3 lg:grid-cols-4 gap-8">

        @foreach ($object->images as $image)
        <div data-id="{{ $image->id }}">
            <div class="w-full h-auto"><img src="{{ asset($image->path) }}"></div>
        </div>
        @endforeach

    </div>

Component:

    /**
     * Define event listeners
     */
    protected $listeners = ['imageSortUpdated'];

    /**
     * When the sort of the images is updated, apply the new order/ranks
     */
    public function imageSortUpdated($order)
    {
        foreach ($this->object->images as $image) {

            $image->update(['rank' => array_search($image->id, $order)]);
        }

        //refresh the model, since the updated image rank isn't reflected in the model's relations object after running update()
        $this->object->refresh();
    }

bramcou avatar Jan 18 '21 10:01 bramcou

@bramcou Thanks for helping!

Djoul avatar Jan 28 '21 12:01 Djoul

i solved this you should set unique key to components like : 'task-'.$task->id.rand() and it works

Adedsec avatar Aug 19 '21 06:08 Adedsec