To achieve live sorting of a Repeater list using a TextInput for order input, you can utilize the ->afterStateUpdated() method to trigger a re-sort whenever the order value changes. Here's a step-by-step solution:
-
Set Up the Repeater: Ensure your
TableRepeateris set up with anorderColumnthat will be used for sorting. -
Use
afterStateUpdated: Attach the->afterStateUpdated()method to theTextInputthat captures the order value. This method will be triggered whenever the input value changes. -
Implement Sorting Logic: Inside the
afterStateUpdatedcallback, implement the logic to sort the repeater items based on the updated order values.
Here's a code example to illustrate the solution:
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\TextInput;
Repeater::make('items')
->orderColumn('order')
->schema([
TextInput::make('order')
->numeric()
->afterStateUpdated(function ($state, callable $set, $get) {
// Get all items
$items = $get('items');
// Sort items based on the 'order' value
usort($items, function ($a, $b) {
return $a['order'] <=> $b['order'];
});
// Update the items with the new order
$set('items', $items);
}),
// Other fields...
]);
Explanation:
TextInput::make('order'): This is the input field where users will enter the order value.->afterStateUpdated(): This method is called whenever theorderinput value changes. It receives the new state, a callable to set values, and a callable to get current values.- Sorting Logic: We retrieve all items using
$get('items'), sort them usingusort()based on theorderfield, and then update the items with the new order using$set('items', $items).
This approach ensures that the list is re-sorted live whenever the order value is updated in the TextInput. Adjust the logic as needed to fit your specific use case and data structure.