rappasoft / laravel-livewire-tables

A dynamic table component for Laravel Livewire
https://rappasoft.com/docs/laravel-livewire-tables/v2/introduction
MIT License
1.7k stars 319 forks source link

[Bug]: Items go missing when reordering #1685

Closed gnewmann closed 2 months ago

gnewmann commented 3 months ago

What happened?

Hey there,

I implemented reordering in my Laravel Livewire table, but I'm encountering an issue with it. When I attempt to reorder items, two out of the four items in the table go missing.

My DataTableComponent

`<?php

namespace App\Livewire;

use Illuminate\Database\Eloquent\Builder; use Rappasoft\LaravelLivewireTables\DataTableComponent; use Rappasoft\LaravelLivewireTables\Views\Column; use App\Models\Song;

class SongTable extends DataTableComponent { protected $model = Song::class; public $setlistId;

public function mount($setlistId)
{
    $this->setlistId = $setlistId;
}

public function builder(): Builder
{
    return Song::whereHas('setlist', function ($query) {
        $query->where('setlist_id', $this->setlistId);
    })->orderBy('order');
}

public function edit($id)
{
    $this->dispatch('editSong', $id);
}

public function delete($id)
{
    $this->dispatch('deleteSong', $id);
}
public function configure(): void
{
    $this->setAdditionalSelects(['songs.id as id']);
    $this->setPrimaryKey('id');
    $this->setReorderStatus(true);
    $this->setReorderEnabled();
    $this->setDefaultReorderSort('order', 'asc');
    $this->setCurrentlyReorderingEnabled();
}

public function reorder(array $items): void
{
    foreach ($items as $item) {
        Song::find($item[$this->getPrimaryKey()])->update(['order' => (int)$item[$this->getDefaultReorderColumn()]]);
    }
}

public function columns(): array
{
    return [
        Column::make("Order", "order")
            ->collapseOnMobile(),
        Column::make("Artist", "artist")
            ->searchable(),
        Column::make("Title", "title")
            ->searchable(),
        Column::make("Album", "album")
            ->searchable(),
        Column::make('Actions')
            ->label(
                function ($row, Column $column) {
                    return view('song.button', ['id' => $row->id]);
                }
            )->collapseOnMobile(),
    ];
}

}` Thank you in advance.

Regards Greg

How to reproduce the bug

When I reorder items, two out of four items go missing

Package Version

No response

PHP Version

8.3.x

Laravel Version

No response

Alpine Version

No response

Theme

Tailwind 3.x

Notes

No response

Error Message

No response

gnewmann commented 3 months ago

After trying out different things .. I recognized.. when I click on "Reorder" and reload the page.. it works without any problems. When I don't reload the page I still got the same problem. Maybe this helps out.

stale[bot] commented 2 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.