F
Filament4mo ago
Liam

Repeater "Table" with dynamic columns

I'm trying to make a dynamic "table" form using repeaters to add any number of rows and columns. Row headers need to be customisable so the existing Repeater@table doesn't fit the bill. I've tried a few odd things but just trimmed it down to this for an example. Examples of problems in thread
return $schema->columns(1)->components([
Hidden::make('columns')->default(2)->live(),

Section::make('Table Structure')
->schema([
Repeater::make('headers')
->label('Table Headers')
->simple(
TextInput::make('header')
->placeholder('Column header')
->required()
)
->addActionLabel('Add Header')
->defaultItems(2)
->minItems(2)
->addable(false)
->deletable(false)
->reorderable(false)
->grid(fn (Get $get) => $get('../columns')),

Repeater::make('rows')
->label('Table Rows')
->simple(
Repeater::make('row')
->simple(
TextInput::make('cell')
->placeholder('Cell content')
->default('')
)
->minItems(1)
->defaultItems(fn (Get $get) => $get('../../columns'))
->addable(false)
->deletable(false)
->reorderable(false)
->grid(fn (Get $get) => $get('../../columns'))
)
->addActionLabel('Add Row')
->defaultItems(1)
->minItems(1)
->collapsed(),
]),
Actions::make([
Action::make('Add Column')
->action(fn (Get $get, Set $set) => $set('columns', $get('columns') + 1)),
]),

]);
return $schema->columns(1)->components([
Hidden::make('columns')->default(2)->live(),

Section::make('Table Structure')
->schema([
Repeater::make('headers')
->label('Table Headers')
->simple(
TextInput::make('header')
->placeholder('Column header')
->required()
)
->addActionLabel('Add Header')
->defaultItems(2)
->minItems(2)
->addable(false)
->deletable(false)
->reorderable(false)
->grid(fn (Get $get) => $get('../columns')),

Repeater::make('rows')
->label('Table Rows')
->simple(
Repeater::make('row')
->simple(
TextInput::make('cell')
->placeholder('Cell content')
->default('')
)
->minItems(1)
->defaultItems(fn (Get $get) => $get('../../columns'))
->addable(false)
->deletable(false)
->reorderable(false)
->grid(fn (Get $get) => $get('../../columns'))
)
->addActionLabel('Add Row')
->defaultItems(1)
->minItems(1)
->collapsed(),
]),
Actions::make([
Action::make('Add Column')
->action(fn (Get $get, Set $set) => $set('columns', $get('columns') + 1)),
]),

]);
6 Replies
Liam
LiamOP4mo ago
Issue #1 the default row doesn't show any fields - only when you click "Add row"
No description
Liam
LiamOP4mo ago
Issue #2 when adding a column (and updating the hidden field which other things rely on), there is no input added to the existing rows (or header)
No description
Liam
LiamOP4mo ago
Presumably the same problem for both and I'm sure it's just my implementation - need to inject the fields into the existing row data somehow? Just need a pointer on what to look at for that
awcodes
awcodes4mo ago
You’re using 2 repeaters for headers and rows, there’s no way for those to be tied together. Each repeater has its own headers and rows.
Liam
LiamOP4mo ago
Regardless of the headers - get rid of that and the other problems persist
toeknee
toeknee4mo ago
Can't see how a hidden field can be live either tbh, does that actually work? For the headers
Repeater::make('headers')
->label('Table Headers')
->simple(
TextInput::make('header')
->placeholder('Column header')
->required()
)
->addActionLabel('Add Header')
->defaultItems(fn (Get $get) => $get('../columns'))
->minItems(fn (Get $get) => $get('../columns'))
->addable(false)
->deletable(false)
->reorderable(false)
->grid(fn (Get $get) => $get('../columns')),
Repeater::make('headers')
->label('Table Headers')
->simple(
TextInput::make('header')
->placeholder('Column header')
->required()
)
->addActionLabel('Add Header')
->defaultItems(fn (Get $get) => $get('../columns'))
->minItems(fn (Get $get) => $get('../columns'))
->addable(false)
->deletable(false)
->reorderable(false)
->grid(fn (Get $get) => $get('../columns')),
no?

Did you find this page helpful?