F
Filamentβ€’6mo ago
Kyle

How do I get the index of the current TextInput inside a simple Repeater?

I have a simple repeater, inside it is a TextInput. I just want to get the index of the current TextInput so that I can show it as the prefix. Maybe something like,
Repeater::make('scores')
->simple(
TextInput::make('score')
->prefix(fn ($index) => $index + 1)
->required()
)
Repeater::make('scores')
->simple(
TextInput::make('score')
->prefix(fn ($index) => $index + 1)
->required()
)
I tried my best to look everywhere on how I can do this but I just can't find an answer. I would be really grateful if anyone can give me hints to this... If it's impossible, please tell me so that I can try to find another way. Thank you very much in advance.
2 Replies
LeandroFerreira
LeandroFerreiraβ€’6mo ago
It could be better πŸ˜…
->prefix(function (TextInput $component) {

$repeater = $component->getParentRepeater();

$componentId = str($component->getKey())
->replace($repeater->getKey() . '.', '')
->replace('.' . $component->getName(), '');

return array_search($componentId, array_keys($repeater->getState())) + 1;
})
->prefix(function (TextInput $component) {

$repeater = $component->getParentRepeater();

$componentId = str($component->getKey())
->replace($repeater->getKey() . '.', '')
->replace('.' . $component->getName(), '');

return array_search($componentId, array_keys($repeater->getState())) + 1;
})
->prefix(fn (TextInput $component) => array_search(
str($component->getId())
->beforeLast('.')
->afterLast('.'),
array_keys($component->getParentRepeater()->getState())
) + 1
)
->prefix(fn (TextInput $component) => array_search(
str($component->getId())
->beforeLast('.')
->afterLast('.'),
array_keys($component->getParentRepeater()->getState())
) + 1
)
Thank you @Dennis Koch
Kyle
KyleOPβ€’6mo ago
I ended up making a custom field that extends Repeater, that shows a modified view, so I can access the blade $loop variable and it worked so I gave up, but I knew there had to be a way!! Thank you very much for this!! I think these are the things that would be very helpful to have on the documentation if it isn't already... I would love to contribute but unfortunately, I know nothing about the advanced features of Filament like this one.
Want results from more Discord servers?
Add your server