custom background columns in table grid
Hello,
Another day another request 🤓
I'm trying to recreate a grid like the attached image
the background color of columns depend by a value stored in $record
my code is below
Solution:Jump to solution
```php
->recordClasses(fn (Model $record) => match ($record->id) {
21 => 'custom-bg-color-black',
22 => 'custom-bg-color-red',
23 => 'custom-bg-color-yellow',...
29 Replies
table function use custom view
custom view
the problem is that the style tag is always taken. without considering the if condition
maybe something like this:
may or may not need an
!important
ok i will try...thanks
but do you know why the style tag doesn't take the if condition?
i try with @if and <?php if
the style tag will reset the cascade, they aren't scoped.
might also need to wrap both the if and the div in an outer div, so there is only one root element.
if i print text inside if, it work
try with extraAttributes function...thanks @awcodes
Illuminate\View\ComponentAttributeBag::merge(): Argument #1 ($attributeDefaults) must be of type array, null given, called in vendor\filament\support\src\Concerns\HasExtraAttributes.php on line 37
@awcodes ok its works..thanks so much
but is it possible to apply the style to the column? the yellow bg
hmm. yea, sorry, i thought it would.
If you have any other suggestions let me know
i'm not 100% sure it works with stacks or not, but maybe https://filamentphp.com/docs/3.x/tables/advanced#custom-row-classes
but i'm not sure how that would work with colors from the DB
could also add styling with negative margins to overcome the padding
it might be the simplest solution
i think that's what i did in Curator for the media grid table.
i try this but not working
recordClasses not show nothing
yea, that's what i was getting at, it's going to add a class, not a style.
->recordClasses(fn (Model $record) => match ($record->status)
i use Model or Modelname?
either will work.
Activity extends Model so, they are the same thing there.
id isn't a string here, but this will tell it add a class of 'background-color: red;' to the element, not a style attribute. So you would need a class in your stylesheet to handle the appearance of the class. So, I don't think this is going to work, since your colors are coming from the DB.
You would have to do everything through the extraAttributes() instead
ok i think is the only way
thanks
try this
and in a css file
Actually, take —custom-bg-color: inherit; and put it in :root {} instead
Solution
this work
but how to put class in css?
recordClasses apply class to column...is the right way
ok worked