filament table hidden record null

this is my code , I want to hidden table column with condition
Tables\Columns\TextColumn::make('stok_sp_hd')
->visible(fn (?Model $record) => $record?->category === "PERDANA LITE")
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_sp_hd')
->visible(fn (?Model $record) => $record?->category === "PERDANA LITE")
->numeric()
->sortable(),
the record always null
Solution:
So to manage the label, I use a description to clarify the data. ```php TextColumn::make('stok_1') ->getStateUsing(fn (StockDigipos $record): int => $record->category === "PERDANA LITE" ? $record->stok_sp_ld : $record->stok_inj_357)...
Jump to solution
10 Replies
Marco
Marco4mo ago
You cant hide a column based on the record, it wont be able to create the table. If you have 2 entries where one has the category and the other hasn't, how would you display the table
Jr.Pikong
Jr.PikongOP4mo ago
Here's a revised version of your question for the forum: --- "I have a table in a Resource with the following columns:
Tables\Columns\TextColumn::make('stok_sp_ld')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_sp_sd')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_sp_md1')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_sp_md2')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_sp_hd')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_sp_total')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_sp_ld')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_sp_sd')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_sp_md1')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_sp_md2')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_sp_hd')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_sp_total')
->numeric()
->sortable(),
And another set of columns:
Tables\Columns\TextColumn::make('stok_inj_357')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_inj_4gb')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_inj_10gb')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_inj_14gb')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_inj_other')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_inj_total')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_inj_357')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_inj_4gb')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_inj_10gb')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_inj_14gb')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_inj_other')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('stok_inj_total')
->numeric()
->sortable(),
If the category is PERDANA LITE, I only want to show the first set of columns and hide the second set. Conversely, if the category is VOUCHER, I want to show only the second set of columns. How can I achieve this?"
Marco
Marco4mo ago
I dont think that you can. if one record has PERDANA LITED and the record below has VOUCHER The table cant render both columns in the same place Maybe create an extra resource where you filter the query using based on the category: https://filamentphp.com/docs/3.x/panels/resources/listing-records#customizing-the-table-eloquent-query
Dennis Koch
Dennis Koch4mo ago
As Marco said: You cannot hide columns based on the record. If you always have the same amount of columns you can swap the content though:
TextColumn::make('stok_1')
->getStateUsing(fn ($record) => $record->category === "PERDANA LITE" ? $record->stok_sp_ld : $record->stok_inj_357)
TextColumn::make('stok_1')
->getStateUsing(fn ($record) => $record->category === "PERDANA LITE" ? $record->stok_sp_ld : $record->stok_inj_357)
But you will loose sortable functionality
Jr.Pikong
Jr.PikongOP3mo ago
thanks @Marco and @Dennis Koch . with this code I Have problem with the Label
Solution
Jr.Pikong
Jr.Pikong3mo ago
So to manage the label, I use a description to clarify the data.
TextColumn::make('stok_1')
->getStateUsing(fn (StockDigipos $record): int => $record->category === "PERDANA LITE" ? $record->stok_sp_ld : $record->stok_inj_357)
->description(fn (StockDigipos $record): string => $record->category === "PERDANA LITE" ? "Stok SP LD" : "STOK INJ 357"),
TextColumn::make('stok_1')
->getStateUsing(fn (StockDigipos $record): int => $record->category === "PERDANA LITE" ? $record->stok_sp_ld : $record->stok_inj_357)
->description(fn (StockDigipos $record): string => $record->category === "PERDANA LITE" ? "Stok SP LD" : "STOK INJ 357"),
This explanation highlights the purpose of the description , emphasizing that it provides clarity regarding the data associated with each label.
Dennis Koch
Dennis Koch3mo ago
You can just set the ->label()
Jr.Pikong
Jr.PikongOP3mo ago
But I want label with condition 😆, and I can do that with label .
Dennis Koch
Dennis Koch3mo ago
Ah yeah. Probably doesn't work. So if description works, that makes sense.
Jr.Pikong
Jr.PikongOP3mo ago
Yes, but thank @Dennis Koch for your help 👌
Want results from more Discord servers?
Add your server