Languages

Version

Theme

表格 - Columns

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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The 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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The 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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The 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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The 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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

格式化

当使用文本列字段时,你可能希望在 UI 中实际输出不同于该字段原始状态值的文本,原始值通常自动从 Eloquent 模型中检索而来。格式化状态在保留原始数据的完整性的同时,允许你将其以对用户更友好的方式展示。

要在不改变原始状态值的同时格式化文本列的值,你可以使用 formatStateUsing() 方法。该方法允许你接受一个函数,该函数接受状态值作为参数并返回格式化后的值:

use Filament\Tables\Columns\TextColumn;

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

上例中,数据库的 status 字段可能包含 draftreviewingpublished 或者 rejected 等值,而格式化后的状态值为这些值的翻译后的版本。

传入 formatStateUsing() 的函数可以将多个 utility 作为参数注入到该函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

日期格式化

除了传入函数到 formatStateUsing() 之外,你也可以使用 date()dateTime()time() 方法,使用 PHP 日期格式化 token 对列状态进行格式化:

use Filament\Tables\Columns\TextColumn;

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

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

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

你也可以传入自定义格式化字符串到 date()dateTime() 或者 time() 方法,自定义日期格式。你可以使用 PHP 日期格式化 token作为格式化字符串值:

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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

使用 Carbon 的 macro 格式格式化日期

你也可以使用 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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

相对日期格式化

你也可以使用 since(),使用 Carbon 的 diffForHumans() 将列状态值格式化:

use Filament\Tables\Columns\TextColumn;

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

在 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()

为日期格式化设置时区

上面列出的每个日期格式化方法都接受一个 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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

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

use Filament\Support\Facades\FilamentTimezone;

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

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

NOTE

Filament 的默认时区只有在列字段中保存时间时适用。如果字段只保存日期(实用 date() 而非 dateTime()),那么时区将不适用。这是为了避免在只保存日期而未保存时间时产生时区转换。

数字格式化

使用 numeric() 方法,你可以将字段格式化为数字,而不必通过传递函数到 formatStateUsing()中实现:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('stock')
    ->numeric()

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

use Filament\Tables\Columns\TextColumn;

TextColumn::make('stock')
    ->numeric(decimalPlaces: 0)
除了允许静态值之外,decimalPlaces 参数也可以接受函数来动态计算它们的值。你可以将多个 utility 作为参数注入到函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

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

use Filament\Tables\Columns\TextColumn;

TextColumn::make('stock')
    ->numeric(locale: 'nl')
除了允许静态值之外,locale 参数也可以接受函数来动态计算它们的值。你可以将多个 utility 作为参数注入到函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

金额格式化

你可以使用 money() 方法以任何货币快速格式化金额值,而不必通过传递函数到 formatStateUsing() 来实现:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('price')
    ->money('EUR')
除了允许静态值之外,money() 方法也可以接受函数来动态计算其值。你可以将多个 utility 作为参数注入到函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The 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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

默认情况下,应用会使用本地化语言格式化数字。如果你想自定义使用的本地语言,可以将其传递给 locale 参数:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('price')
    ->money('EUR', locale: 'nl')
除了允许静态值之外,locale 参数也可以接受函数来动态计算它们的值。你可以将多个 utility 作为参数注入到函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

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

use Filament\Tables\Columns\TextColumn;

TextColumn::make('price')
    ->money('EUR', decimalPlaces: 3)
除了允许静态值之外,decimalPlaces 参数也可以接受函数来动态计算它们的值。你可以将多个 utility 作为参数注入到函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

渲染 Markdown

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

use Filament\Tables\Columns\TextColumn;

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

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

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
    ->markdown(FeatureFlag::active())
除了允许静态值之外,markdown() 方法也可以接受函数来动态计算其值。你可以将多个 utility 作为参数注入到函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

渲染 HTML

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

use Filament\Tables\Columns\TextColumn;

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

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

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
    ->html(FeatureFlag::active())
除了允许静态值之外,html() 方法也可以接受函数来动态计算其值。你可以将多个 utility 作为参数注入到函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

渲染无净化的原始 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)
The function passed to description() can inject various utilities as parameters. Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The 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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

列表中添加点号

使用 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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

限制列表中值的数量

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

use Filament\Tables\Columns\TextColumn;

TextColumn::make('authors.name')
    ->listWithLineBreaks()
    ->limitList(3)
除了允许静态值之外,limitList() 方法也可以接受函数来动态计算其值。你可以将多个 utility 作为参数注入到函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The 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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

将单个值拆分成多个列表项目

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

use Filament\Tables\Columns\TextColumn;

TextColumn::make('tags')
    ->badge()
    ->separator(',')
除了允许静态值之外,separator() 方法也可以接受函数来动态计算其值。你可以将多个 utility 作为参数注入到函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

自定义文本大小

文本列默认使用 small 字体,你可以将其改为 TextColumnSize::ExtraSmallTextColumnSize::MediumTextColumnSize::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

自定义字体粗细

文本列默认使用常规字体,不过你可以将其改为以下选项:FontWeight::ThinFontWeight::ExtraLightFontWeight::LightFontWeight::MediumFontWeight::SemiBoldFontWeight::BoldFontWeight::ExtraBoldFontWeight::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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.
Text column in a bold font

自定义字体系列

你可以将字体改为以下选项:FontFamily::SansFontFamily::SerifFontFamily::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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The 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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

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

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
    ->limit(50, end: ' (more)')
除了允许静态值之外,end 参数也可以接受函数来动态计算它们的值。你可以将多个 utility 作为参数注入到函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The 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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

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

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
    ->words(10, end: ' (more)')
除了允许静态值之外,end 参数也可以接受函数来动态计算它们的值。你可以将多个 utility 作为参数注入到函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

Allowing text wrapping

By default, text will not wrap to the next line if it exceeds the width of the container. You can enable this behavior using the wrap() method:

use Filament\Tables\Columns\TextColumn;

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

Optionally, you may pass a boolean value to control if the text should wrap or not:

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
    ->wrap(FeatureFlag::active())
除了允许静态值之外,wrap() 方法也可以接受函数来动态计算其值。你可以将多个 utility 作为参数注入到函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

将文本限制为指定行数

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

use Filament\Tables\Columns\TextColumn;

TextColumn::make('description')
    ->wrap()
    ->lineClamp(2)
除了允许静态值之外,lineClamp() 方法也可以接受函数来动态计算其值。你可以将多个 utility 作为参数注入到函数中。 Learn more about utility injection.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

允许将文本复制到剪贴板

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

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.
Utility Type Parameter Description
Column Filament\Tables\Columns\Column $column The current column instance.
Livewire Livewire\Component $livewire The Livewire component instance.
Eloquent record ?Illuminate\Database\Eloquent\Model $record The Eloquent record for the current table row.
Row loop stdClass $rowLoop The row loop object for the current table row.
State mixed $state The current value of the column, based on the current table row.
Table Filament\Tables\Table $table The current table instance.

NOTE

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

Edit on GitHub

Still need help? Join our Discord community or open a GitHub discussion

Previous
概述