跳到主要内容
版本:5.x

Text column

简介​

文本列用于显示纯文本:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('title')
Text column

自定义颜色​

你可以为文本设置颜色:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('status')
->color('primary')
除了允许静态值之外,color() 方法也接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column in the primary color

添加图标​

文本列可以添加图标:

use Filament\Tables\Columns\TextColumn;
use Filament\Support\Icons\Heroicon;

TextColumn::make('email')
->icon(Heroicon::Envelope)
icon() 方法也接受函数来动态计算其图标。你可以将各种 utility 作为参数注入到函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column with icon

你也可以使用 iconPosition() 设置图标位置:

use Filament\Tables\Columns\TextColumn;
use Filament\Support\Enums\IconPosition;
use Filament\Support\Icons\Heroicon;

TextColumn::make('email')
->icon(Heroicon::Envelope)
->iconPosition(IconPosition::After) // `IconPosition::Before` or `IconPosition::After`
iconPosition() 方法也接受函数来动态计算其图标位置。你可以将各种 utility 作为参数注入到函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column with icon after

图标颜色默认与文本颜色相同,但可以使用 iconColor() 单独自定义图标颜色:

use Filament\Tables\Columns\TextColumn;
use Filament\Support\Icons\Heroicon;

TextColumn::make('email')
->icon(Heroicon::Envelope)
->iconColor('primary')
iconColor() 方法也接受函数来动态计算其图标颜色。你可以将各种 utility 作为参数注入到函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column with icon in the primary color

以“徽章”形式显示​

默认情况下,文本是纯色的,没有背景色。你可以使用 badge() 方法将其显示为“徽章”。这在状态栏中是一个很好的应用场景,你可能希望显示一个与状态相匹配的徽章颜色:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('status')
->badge()
->color(fn (string $state): string => match ($state) {
'draft' => 'gray',
'reviewing' => 'warning',
'published' => 'success',
'rejected' => 'danger',
})
Text column as badge

你也可以将其他东西添加到该徽章中,比如图标。

此外,你也可以传入一个布尔值用以控制文本是否以徽章显示:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('status')
->badge(FeatureFlag::active())
除了允许静态值外,badge() 方法也接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

格式化​

使用文本列时,你可能希望 UI 中实际输出的该列的原始状态(通常从 Eloquent 模型自动获取)有所不同。格式化状态既可以保留原始数据的完整性,又能使其以更友好的方式呈现。

要在不更改状态本身的情况下格式化文本列的状态,可以使用 formatStateUsing() 方法。此方法接受一个以状态为参数的函数,并返回格式化后的状态:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('status')
->formatStateUsing(fn (string $state): string => __("statuses.{$state}"))

在这种情况下,数据库中的 status 状态列可能包含 draft、reviewing、published 或 rejected 等值,但格式化后的状态将是这些值的翻译版本。

传入formatStateUsing() 的函数可以将多个 utility 作为参数注入其中。 Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

日期格式化​

除了将函数传递给 formatStateUsing() 之外,你还可以使用 date()、dateTime() 和 time() 方法来使用 PHP 日期格式化标记 格式化列的状态:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('created_at')
->date()

TextColumn::make('created_at')
->dateTime()

TextColumn::make('created_at')
->time()
Text column with date formatting

你可以通过将自定义格式字符串传递给 date()、dateTime() 或 time() 方法来自定义日期格式。你可以使用任何 PHP 日期格式标记:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('created_at')
->date('M j, Y')

TextColumn::make('created_at')
->dateTime('M j, Y H:i:s')

TextColumn::make('created_at')
->time('H:i:s')
除了允许静态值外,date()、dateTime() 和 time() 方法也接受函数动态计算其格式。你可以将各种 utility 作为参数注入到函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

使用 Carbon 宏格式进行日期格式化​

你也可以使用 isoDate()、isoDateTime() 和 isoTime() 方法,使用 Carbon 的宏(macro)格式格式化列的状态:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('created_at')
->isoDate()

TextColumn::make('created_at')
->isoDateTime()

TextColumn::make('created_at')
->isoTime()

你可以通过将自定义宏格式字符串传递给 isoDate()、isoDateTime() 或者 isoTime() 方法来自定义日期格式。你可以使用任何 Carbon 的宏格式:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('created_at')
->isoDate('L')

TextColumn::make('created_at')
->isoDateTime('LLL')

TextColumn::make('created_at')
->isoTime('LT')
除了允许静态值外,isoDate()、isoDateTime() 和 isoTime() 方法也接受函数动态计算其格式。你可以将各种 utility 作为参数注入到函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

相对日期格式​

你可以使用 since() 方法来使用 Carbon 的 diffForHumans() 对列状态进行格式化:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('created_at')
->since()
Text column with relative date formatting

在工具提示(tooltip)中显示格式化日期​

另外,你也可以使用 dateTooltip()、dateTimeTooltip()、timeTooltip()、isoDateTooltip()、isoDateTimeTooltip()、isoTime()、isoTimeTooltip() 或者 sinceTooltip() 方法在 Tooltip 中展示格式化日期,通常用于提供额外信息。

use Filament\Tables\Columns\TextColumn;

TextColumn::make('created_at')
->since()
->dateTooltip() // Accepts a custom PHP date formatting string

TextColumn::make('created_at')
->since()
->dateTimeTooltip() // Accepts a custom PHP date formatting string

TextColumn::make('created_at')
->since()
->timeTooltip() // Accepts a custom PHP date formatting string

TextColumn::make('created_at')
->since()
->isoDateTooltip() // Accepts a custom Carbon macro format string

TextColumn::make('created_at')
->since()
->isoDateTimeTooltip() // Accepts a custom Carbon macro format string

TextColumn::make('created_at')
->since()
->isoTimeTooltip() // Accepts a custom Carbon macro format string

TextColumn::make('created_at')
->dateTime()
->sinceTooltip()
Text column with a date tooltip on relative dates

设置格式化日期的时区​

上面列出的每种日期格式设置方法都接受一个 timezone 参数,允许你将状态中设置的时间转换为不同的时区:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('created_at')
->dateTime(timezone: 'America/New_York')

你也可以将时区传递给列的 timezone() 方法,以便一次性将时区应用于所有日期时间格式化方法:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('created_at')
->timezone('America/New_York')
->dateTime()
除了允许静态值外,timezone() 方法也接受函数来动态计算时区。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

如果未传入时区(timezone())到列中,它将使用 Filament 的默认时区。你可以在服务提供者(如 AppServiceProvider)的 boot() 方法中使用 FilamentTimezone::set() 设置默认时区:

use Filament\Support\Facades\FilamentTimezone;

public function boot(): void
{
FilamentTimezone::set('America/New_York');
}

如果你想为应用中的所有文本列设置默认时区,这很有用。它同时也在 Filament 中其他使用到时区的地方中生效。

NOTE

Filament 的默认时区只在列存储时间时应用。如果列只保存日期(date() 而非 dateTime()),时区将不会适用。这是为了避免没有存储时间只存储日期时,出现时区转换。

数字格式化​

除了将函数传入到 formatStateUsing()中实现之外,使用 numeric() 方法,你可以将字段格式化为数字:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('stock')
->numeric()
Text column with numeric formatting

如果你想自定义用于格式化数字的小数位数,你可以使用 decimalPlaces 参数:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('stock')
->numeric(decimalPlaces: 0)
除了允许静态值外,decimalPlaces 参数可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

默认情况下,应用的区域设置(locale)将用于格式化数值,如果你想自定义使用的本地化语言,你可以将其传递给 locale 参数:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('stock')
->numeric(locale: 'nl')
除了允许静态值外,locale 参数可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

金额格式化​

除了传递函数到 formatStateUsing() 之外,你也可以使用 money() 方法以任何货币快速格式化金额值:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('price')
->money('EUR')
Text column with money formatting
除了允许静态值外,money() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

money() 有一个 divideBy 参数,它允许你在格式化之前将原始值除以某个数字。这对在数据库中将价格存储为分(cent)非常有用,比如:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('price')
->money('EUR', divideBy: 100)
除了允许静态值外,divideBy 参数可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

默认情况下,应用会使用区域设置(locale)格式化金额。如果你想自定义使用的本地语言环境,可以将其传递给 locale 参数:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('price')
->money('EUR', locale: 'nl')
除了允许静态值外,locale 参数可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

如果你想要自定义用于格式化数字的小数点后位数,你可以使用 decimalPlaces 参数:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('price')
->money('EUR', decimalPlaces: 3)
除了允许静态值外,decimalPlaces 参数可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

渲染 Markdown​

如果列中的值是 Markdown 格式,你可以使用 markdown() 进行渲染:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
->markdown()
Text column with Markdown rendering

此外,你也可以传入一个布尔值控制该文本是否作为 Markdown 渲染:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
->markdown(FeatureFlag::active())
除了允许静态值外,markdown() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

渲染 HTML​

如果列中的值是 HTML 格式,你可以使用 html() 进行渲染:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
->html()
Text column with HTML rendering

此外,你也可以传入一个布尔值控制该文本是否渲染为 HTML:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
->html(FeatureFlag::active())
除了允许静态值外,html() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

NOTE

Filament 内置的 HTML 净化器允许行内 style 属性,以支持富文本格式化功能,如字体颜色、文本突出显示和图像大小。这意味着像 background: url(...) 或 position:fixed 这样的 CSS 属性不会从经过净化的 HTML 中删除。如果你的内容来自不受信任的用户,应该考虑限制默认配置。有关如何自定义净化器的详细信息,请参阅安全文档。

渲染未经净化的原始 HTML​

如果使用此方法,则 HTML 将在渲染之前进行净化,以删除任何潜在的不安全内容。如果你想退出这种行为,你可以通过格式化将 HTML 包装在一个 HtmlString 对象中:

use Filament\Tables\Columns\TextColumn;
use Illuminate\Support\HtmlString;

TextColumn::make('description')
->formatStateUsing(fn (string $state): HtmlString => new HtmlString($state))

NOTE

渲染原始 HTML 时要小心,因为它可能包含恶意内容,这可能会导致应用中存在安全漏洞,如跨站脚本(XSS)攻击。在使用此方法之前,始终确保你正在渲染的 HTML 是安全的。

或者,你可以从 formatStateUsing() 方法返回一个 view() 对象,其中内容也不会被净化:

use Filament\Tables\Columns\TextColumn;
use Illuminate\Contracts\View\View;

TextColumn::make('description')
->formatStateUsing(fn (string $state): View => view(
'filament.tables.columns.description-column-content',
['state' => $state],
))

显示描述​

描述用于在列内容上方或下方渲染额外的文本。

使用 description() 方法,你可以在文本列的内容下方显示描述:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('title')
->description(fn (Post $record): string => $record->description)
传入 description() 的函数,你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column with description

默认情况下,描述显示在主文本下方,不过你也可以使用 'above' 作为第二个参数将其移动到上方:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('title')
->description(fn (Post $record): string => $record->description, position: 'above')
Text column with description above the content

列出多个值​

如果文本列的 state 是数组,那么该文本列可以渲染多个值。如果你的 Eloquent 属性使用了 array 强制转换(cast),或者 Eloquent 关联有多个结果,又或者你将一个数组传入到 state() 方法中,就会出现这种情况。如果你的文本列中有多个值,那么它们会使用逗号隔开。你可以使用 listWithLineBreaks() 方法将其显示为多行:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('authors.name')
->listWithLineBreaks()

或者,你也可以传入布尔值以控制是否应该以换行显示每一项:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('authors.name')
->listWithLineBreaks(FeatureFlag::active())
除了允许静态值外,listWithLineBreaks() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column with values displayed on separate lines

在列表中添加项目符号​

使用 bulleted() 方法,你可以为每个列表项添加项目符号:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('authors.name')
->bulleted()

此外,你也可以传入一个布尔值,控制文本是否添加项目符号:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('authors.name')
->bulleted(FeatureFlag::active())
除了允许静态值外,bulleted() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column with bulleted list of values

限制列表中值的数量​

你可以使用 limitList() 方法限制列表中的值的数量:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('authors.name')
->listWithLineBreaks()
->limitList(3)
除了允许静态值外,limitList() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

展开受限列表​

你可以使用 expandableLimitedList() 方法允许展开和折叠受限列表项:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('authors.name')
->listWithLineBreaks()
->limitList(3)
->expandableLimitedList()

NOTE

这只是 listWithLineBreaks() 或 bulleted() 的一个功能,其中每个项目都在自己的行上。

此外,你也可以选择传入一个布尔值来控制文本是否可展开:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('authors.name')
->listWithLineBreaks()
->limitList(3)
->expandableLimitedList(FeatureFlag::active())
除了允许静态值外,expandableLimitedList() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column with an expandable limited list

将单个值拆分为多个列表项​

如果你想将模型中的文本字符串打散(explode)成多个列表项,你可以使用 separator() 方法。这对于将以逗号分隔的标签以徽章的方式显示很有用。比如:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('tags')
->badge()
->separator(',')
Text column with badges from comma-separated values
除了允许静态值外,separator() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

自定义文本大小​

文本列默认使用 small 字体,你可以将其改为 TextColumnSize::ExtraSmall、TextColumnSize::Medium 或 TextColumnSize::Large。

比如,使用 size(TextColumnSize::Large) 你可以让文本变大:

use Filament\Tables\Columns\TextColumn;
use Filament\Support\Enums\TextSize;

TextColumn::make('title')
->size(TextSize::Large)
Text column in a large font size

自定义字体粗细​

文本列默认使用常规(regular)字体,不过你可以将其改为以下选项:FontWeight::Thin、FontWeight::ExtraLight、FontWeight::Light、FontWeight::Medium、FontWeight::SemiBold、FontWeight::Bold、FontWeight::ExtraBold 或 FontWeight::Black。

比如,使用 weight(FontWeight::Bold) 你可以将字体改为粗体:

use Filament\Tables\Columns\TextColumn;
use Filament\Support\Enums\FontWeight;

TextColumn::make('title')
->weight(FontWeight::Bold)
除了允许静态值外,weight() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column in a bold font

自定义字体系列​

你可以将字体改为以下选项:FontFamily::Sans、FontFamily::Serif 或 FontFamily::Mono。

比如,使用 fontFamily(FontFamily::Mono) 你可以将字体改为 Mono:

use Filament\Support\Enums\FontFamily;
use Filament\Tables\Columns\TextColumn;

TextColumn::make('email')
->fontFamily(FontFamily::Mono)
除了允许静态值外,fontFamily() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column in a monospaced font

处理长文本​

限制文本长度​

使用 limit() 方法,你可以限制列值的长度:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
->limit(50)
除了允许静态值外,limit() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column with limited text length

默认情况下,当文本被截断时,省略号(...)会附加到文本末尾。你可以通过向 end 参数传递自定义字符串来对此进行自定义:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
->limit(50, end: ' (more)')
除了允许静态值外,end 参数可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

通过使用 getCharacterLimit() 方法获取,你还可以在函数中重用传入到 limit() 中的值:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
->limit(50)
->tooltip(function (TextColumn $column): ?string {
$state = $column->getState();

if (strlen($state) <= $column->getCharacterLimit()) {
return null;
}

// Only render the tooltip if the column contents exceeds the length limit.
return $state;
})

限制字数​

使用 words() 方法,你可以限制显示在列中的字数::

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
->words(10)
除了允许静态值外,words() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column with limited word count

默认情况下,当文本被截断时,省略号(...)会附加到文本末尾。你可以通过向 end 参数传递自定义字符串来对此进行自定义:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
->words(10, end: ' (more)')
除了允许静态值外,end 参数可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

允许文本换行​

默认情况下,当文本超过其容器宽度时,不会换行。使用 wrap() 方法可以启用换行:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
->wrap()

或者,你也可以传入布尔值控制文本是否换行:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
->wrap(FeatureFlag::active())
除了允许静态值外,wrap() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column with wrapping text

将文本限定到特定行数​

你可以将文本限制到指定的行数,而不是限制成固定长度。将文本行数锁定到指定行数在响应式界面中很有用,这样可以确保在所有屏幕中都有一致性体验。使用 lineClamp() 方法可以实现此效果:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
->wrap()
->lineClamp(2)
除了允许静态值外,lineClamp() 也可以接受函数动态计算其值。你可以将各种 utility 作为参数注入到该函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.
Text column with line clamping

允许将文本复制到剪切板​

你可以让文本变为可复制,这样在点击列后,文本将会复制到剪切板,同时可以指定自定义确认消息以及消息的展示间隔(以毫秒计):

use Filament\Tables\Columns\TextColumn;

TextColumn::make('email')
->copyable()
->copyMessage('Email address copied')
->copyMessageDuration(1500)
Text column with a button to copy it

或者,你也可以传入布尔值以控制文本是否可复制:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('email')
->copyable(FeatureFlag::active())
除了允许静态值外,copyable()、copyMessage() 和 copyMessageDuration() 方法也可以接受函数来动态计算它们的值。你可以将多个 utility 作为参数注入到这些函数中。Learn more about utility injection.
UtilityTypeParameterDescription
ColumnFilament\Tables\Columns\Column$columnThe current column instance.
LivewireLivewire\Component$livewireThe Livewire component instance.
Eloquent record?Illuminate\Database\Eloquent\Model$recordThe Eloquent record for the current table row.
Row loopstdClass$rowLoopThe <a href="https://laravel.com/docs/blade#the-loop-variable" target="_blank">row loop</a> object for the current table row.
Statemixed$stateThe current value of the column, based on the current table row.
TableFilament\Tables\Table$tableThe current table instance.

NOTE

此特性只有在应用启用 SSL 时有效。