How can I show styled text if a cell is empty?

My datatable has the following column:
TextColumn::make('description') ->label('Description') ->placeholder('empty'), Instead of simply the text "empty" I want to show this styled version: '<span class="px-3 py-1 bg-blue-100 text-blue-800 text-sm rounded-full">Empty</span>' Is this possible?
3 Replies
toeknee
toeknee3mo ago
use Illuminate\Support\HtmlString;

TextColumn::make('description')
->label('Description')
->placeholder('empty')
->formatState(fn($state) => empty($state) ? new HtmlString('<span class="px-3 py-1 bg-blue-100 text-blue-800 text-sm rounded-full">Empty</span>') : $state),
use Illuminate\Support\HtmlString;

TextColumn::make('description')
->label('Description')
->placeholder('empty')
->formatState(fn($state) => empty($state) ? new HtmlString('<span class="px-3 py-1 bg-blue-100 text-blue-800 text-sm rounded-full">Empty</span>') : $state),
morty
morty3mo ago
Alternatively, I do this with a macro:
Tables\Columns\TextColumn::macro('placeholderDash', function () {
return $this->placeholder(fn (): HtmlString => new HtmlString('&mdash;'));
});
Tables\Columns\TextColumn::macro('placeholderDash', function () {
return $this->placeholder(fn (): HtmlString => new HtmlString('&mdash;'));
});
urbycoz
urbycoz3mo ago
Thanks both. HtmlString was the key for me. TextColumn::make('description') ->label('Description') ->default(new HtmlString('<span class="px-3 py-1 bg-blue-100 text-blue-800 text-sm rounded-full">Empty</span>'))