K

Sortable

API Reference

NyonCode\WireSortable\SortableTable

SortableTable

NyonCode\WireSortable\SortableTable

Extends NyonCode\WireTable\Table. All base Table methods remain available.

reorderable(?string $orderColumn = null, bool $condition = true): static

Enable drag & drop row reordering.

Parameter Type Default Description
$orderColumn ?string 'sort_order' Database column for sort position
$condition bool true Conditionally enable reordering
// Default column
$table->reorderable();
 
// Custom column
$table->reorderable('position');
 
// Conditional
$table->reorderable('position', $user->can('reorder'));

isReorderable(): bool

Returns whether row reordering is enabled.

getOrderColumn(): string

Returns the order column name.

paginatedWhileReordering(bool $enabled = true): static

Keep pagination enabled while in reorder mode. By default, pagination is disabled during reordering.

isPaginatedWhileReordering(): bool

Returns whether pagination is kept during reorder mode.

columnReorderable(bool $enabled = true): static

Enable or disable user-specific column reordering. Column order is persisted per user + model in the database.

isColumnReorderable(): bool

Returns whether column reordering is enabled.


WithSortable

NyonCode\WireSortable\Concerns\WithSortable

Livewire trait. Use alongside WithTable:

use WithTable, WithSortable;

Properties

Property Type Default Description
$isReordering bool false Whether the table is in row reorder mode
$reorderableColumnOrder array [] Current column order (loaded from DB on mount)

Public methods

toggleReordering(): void

Toggle row reorder mode on/off. Clears cached records to force re-query.

No-op if the table is not reorderable.

reorderRows(array $items): void

Handle row drag & drop. Called by Alpine.js after a drag operation completes. Updates the order column in a database transaction.

Each item: ['value' => string|int, 'order' => int]

No-op if:

  • The table is not reorderable
  • The table is not in reorder mode ($isReordering === false)

reorderColumns(array $columnOrder): void

Handle column drag & drop. Validates column names against the table definition and persists to the reorderable_column_orders table.

No-op if:

  • The table is not column-reorderable
  • The user is not authenticated (getReorderableUserId() returns null)
  • No valid column names are provided

resetColumnOrder(): void

Resets column order to the default (as defined in table()) and deletes the database entry.

getReorderableColumns(): array

Returns columns in the user's saved order. Newly added columns (not present in the saved order) are appended at the end. Removed columns (in saved order but no longer in the table definition) are silently skipped.

isTableReordering(): bool

Returns whether the table is currently in row reorder mode. Alias for $this->isReordering.

Protected overrides

These methods override WithTable's protected factory/hook methods. No insteadof clause is needed — PHP resolves them automatically because WithSortable is listed after WithTable.

getTableView(): string

Returns 'wire-sortable::tables.index' when row or column reordering is enabled. Falls through to 'wire-table::tables.index' otherwise.

interceptTableRecords(): LengthAwarePaginator|Paginator|CursorPaginator|Collection|null

In reorder mode (without paginatedWhileReordering): bypasses search, filters, sorting, and pagination. Returns all records ordered by the sort column ascending.

Otherwise: returns null to let WithTable handle record fetching normally.

Protected hooks

beforeReorder(array $items): void

Called before the database update. Override for authorization or pre-processing.

protected function beforeReorder(array $items): void
{
$this->authorize('reorder', Task::class);
}

afterReorder(array $items): void

Called after the database update. Override for cache invalidation or events.

protected function afterReorder(array $items): void
{
Cache::forget('tasks.ordered');
$this->dispatch('tasks-reordered');
}

getReorderableUserId(): ?int

Returns the user ID for column order persistence. Defaults to auth()->id().

Override for custom auth guards:

protected function getReorderableUserId(): ?int
{
return auth('admin')->id();
}

getReorderableModelType(): ?string

Returns the model type key for column order persistence. Defaults to the Eloquent model class name (e.g., App\Models\Task).

Override to use a custom key:

protected function getReorderableModelType(): ?string
{
return static::class; // component class instead of model
}

ReorderableColumnOrder

NyonCode\WireSortable\Models\ReorderableColumnOrder

Eloquent model for the reorderable_column_orders table.

Properties

Property Type Description
$user_id int Foreign key to the users table
$model_type string Eloquent model class name
$column_order array JSON-cast array of column names

Relationships

user(): BelongsTo

Belongs to the user model configured in wire-sortable.user_model.

Static methods

getOrder(int $userId, string $modelType, string $tableIdentifier): ?array

Get the saved column order for a user + model + table combination. Returns null if no record exists.

saveOrder(int $userId, string $modelType, string $tableIdentifier, array $columnOrder): void

Create or update the column order for a user + model + table combination (upsert).

deleteOrder(int $userId, string $modelType, string $tableIdentifier): void

Delete the column order record for a user + model + table combination.

Configuration

config/wire-sortable.php

Key Type Default Description
order_column string 'sort_order' Default order column name
sortablejs_cdn ?string CDN URL SortableJS source URL, null to disable
animation int 150 Drag animation duration in milliseconds
user_model string 'App\\Models\\User' User model class for column order relationships

Alpine.js component

wireSortable(config) is registered globally via Alpine.data().

Config options

Option Type Default Description
rowReorderable bool false Enable row reordering
columnReorderable bool false Enable column reordering
isReordering bool (entangled) false Livewire-synced reorder mode state
orderColumn string 'sort_order' Order column name
animation int 150 SortableJS animation duration (ms)

Behavior

  • isReordering is entangled with the Livewire $isReordering property via @entangle
  • When isReordering changes, the component automatically initializes or destroys SortableJS on the <tbody>
  • Drag handles are dynamically added/removed from the DOM
  • Column sorting is always active when columnReorderable is true (independent of reorder mode)
  • After Livewire updates the table, SortableJS is re-initialized

Translations

lang/{locale}/messages.php

Key EN CS Description
reorder Reorder Přeuspořádat Toggle button label (inactive)
done_reordering Done reordering Hotovo Toggle button label (active)