F
Filament2mo ago
o.m

Do you have some guides for creating your own Repeater?

I have tried making my own repeater component and honestly I have a bit of trouble implementing it (I used the default repeaters blade design). It has lots a problems especially for when generating a new data. I get a missing column even though I manually specified it. Do you have a good guide for making your own custom component? Using 3.x btw
No description
5 Replies
o.m
o.mOP2mo ago
Answers::make('options')
->label('Possible Answers')
->relationship('options')
->reorderable()
->simple(TextInput::make('label'))
->deleteAction(fn(Forms\Components\Actions\Action $action) =
> $action->requiresConfirmation(), )
->deleteAction(
fn(Forms\Components\Actions\Action $action) =
>
$action->link()
->label('Delete Answer')
->icon('heroicon-o-trash')
->color('danger')
->action(function(Forms\Get $get, Forms\Set $set,
array $state, array $arguments) {
$questionOptionID =
isset($arguments['item'])
? (int)str_replace('record-', '',
$arguments['item'])
: null;

if (!$questionOptionID) {
throw new \Exception('UUID not provided for deletion.');
}

QuestionOption::findOrFail($questionOptionID)->delete ();
$updatedState =
array_filter($state, fn($option) = > $option['id'] !=
= $questionOptionID);

$set('options', array_values($updatedState));
})),
Answers::make('options')
->label('Possible Answers')
->relationship('options')
->reorderable()
->simple(TextInput::make('label'))
->deleteAction(fn(Forms\Components\Actions\Action $action) =
> $action->requiresConfirmation(), )
->deleteAction(
fn(Forms\Components\Actions\Action $action) =
>
$action->link()
->label('Delete Answer')
->icon('heroicon-o-trash')
->color('danger')
->action(function(Forms\Get $get, Forms\Set $set,
array $state, array $arguments) {
$questionOptionID =
isset($arguments['item'])
? (int)str_replace('record-', '',
$arguments['item'])
: null;

if (!$questionOptionID) {
throw new \Exception('UUID not provided for deletion.');
}

QuestionOption::findOrFail($questionOptionID)->delete ();
$updatedState =
array_filter($state, fn($option) = > $option['id'] !=
= $questionOptionID);

$set('options', array_values($updatedState));
})),
For example in this code, if there are 5 items and I delete the one item it works fine, however whenever I delete a second Item it shows a "not found error" So In-order dete to work, I have to do a page refresh in order to continuously delete an item.
o.m
o.mOP2mo ago
No description
o.m
o.mOP2mo ago
Or if I add two Items and delete the newly added one, this will also happen
o.m
o.mOP2mo ago
No description
o.m
o.mOP2mo ago
addAction()
->addAction(fn(Forms\Components\Actions\Action $action) =
> $action->link()
->label('Add an Answer')
->icon('heroicon-o-plus')
->action(function(Forms\Get $get, Forms\Set $set,
array $state) {
$uuid = Str::uuid()->toString();

$state[] = [
'uuid' = > $uuid,
'label' = > '',
'is_correct' = > false,
'disabled' =
> false, // disable newly added item to options
'ordinal' = > count($state),
];

Question::find($get('id'))->options()->create([
'label' = > '',
'ordinal' = > count($state),
'uuid' = > $uuid,
'is_correct' = > false,
]);

$set('options', $state);
}))
->addAction(fn(Forms\Components\Actions\Action $action) =
> $action->link()
->label('Add an Answer')
->icon('heroicon-o-plus')
->action(function(Forms\Get $get, Forms\Set $set,
array $state) {
$uuid = Str::uuid()->toString();

$state[] = [
'uuid' = > $uuid,
'label' = > '',
'is_correct' = > false,
'disabled' =
> false, // disable newly added item to options
'ordinal' = > count($state),
];

Question::find($get('id'))->options()->create([
'label' = > '',
'ordinal' = > count($state),
'uuid' = > $uuid,
'is_correct' = > false,
]);

$set('options', $state);
}))
deleteAction()
->deleteAction(
fn(Forms\Components\Actions\Action $action) =
> $action->link()
->label('Delete Answer')
->icon('heroicon-o-trash')
->color('danger')
->action(function(Forms\Get $get, Forms\Set $set, array $state,
array $arguments) {
$questionOptionID =
isset($arguments['item'])
? (int)str_replace('record-', '', $arguments['item'])
: null;

if (!$questionOptionID) {
throw new \Exception('UUID not provided for deletion.');
}

QuestionOption::findOrFail($questionOptionID)->delete ();
$updatedState =
array_filter($state, fn($option) = > $option['id'] !=
= $questionOptionID);

$set('options', array_values($updatedState));
})),
->deleteAction(
fn(Forms\Components\Actions\Action $action) =
> $action->link()
->label('Delete Answer')
->icon('heroicon-o-trash')
->color('danger')
->action(function(Forms\Get $get, Forms\Set $set, array $state,
array $arguments) {
$questionOptionID =
isset($arguments['item'])
? (int)str_replace('record-', '', $arguments['item'])
: null;

if (!$questionOptionID) {
throw new \Exception('UUID not provided for deletion.');
}

QuestionOption::findOrFail($questionOptionID)->delete ();
$updatedState =
array_filter($state, fn($option) = > $option['id'] !=
= $questionOptionID);

$set('options', array_values($updatedState));
})),
My blade file came directly from Repeaters blade but with a slight modificaiton on the UI

Did you find this page helpful?