How do I dynamically create table columns in my admin panel?

My database table has name and value columns. I currently use TextColumn for both, like this:
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('name')
->columns([
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('value'),
])
...
...
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('name')
->columns([
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('value'),
])
...
...
How do I dynamically set the column for value based on the value of name?
6 Replies
damms005
damms005OP14mo ago
I checked the table API and can't find anything to help me with this. The only option seems to be custom column. This feature is going to be similar to the form schema API that accepts a closure. This allows for dynamically setting form components at runtime. Tables' columns seems to be a good candidate for this, unless I am missing something. Is a PR acceptable for this?
awcodes
awcodes14mo ago
Define you columns outside of the return then return the columns array as the the ->columns()
damms005
damms005OP14mo ago
But how do I get access to the $record so I can inspect and define columns based on the row value?
awcodes
awcodes14mo ago
Good point. Tables aren’t the same as forms some things work with forms that don’t work with tables. Just the nature of html. Columns have to be more rigid because you run the risk of one row having a column and another not and that’s just not possible with html tables.
damms005
damms005OP14mo ago
I understand your point. Thanks man But I think it is doable. Will be a fun feature to play hack if allowed into the framework. columns(array...) currently accept only array of table columns. The new feature will simply allow closure columns(array... | callable...) and the callable will return array of table columns. Flexible like forms, preserves the behavior via different APIs.
awcodes
awcodes14mo ago
Maybe, but forms aren’t repeated data. They are set. In regards to a table, every record has to have the same fields / columns. Even if the value of that column is null.
Want results from more Discord servers?
Add your server