Languages

Version

Theme

信息列表

自定义 Entry

简介

你可以创建你自己的自定义条目(Entry)类及视图,它可以在整个应用中进行重用,甚至可以将其作为插件发布到社区。

你可以创建自定义条目(Entry)类及视图,请要使用如下命令:

php artisan make:filament-infolist-entry AudioPlayerEntry

该命令将创建以下组件类:

use Filament\Infolists\Components\Entry;

class AudioPlayerEntry extends Entry
{
    protected string $view = 'filament.infolists.components.audio-player-entry';
}

同时生成一个视图文件:resources/views/filament/infolists/components/audio-player-entry.blade.php

NOTE

Filament 信息列表条目不是 Livewire 组件。在条目类中定义公共属性和方法不会使之在 Blade 视图中可访问到。

在 Blade 视图中访问条目状态

在 Blade 视图中,你可以使用 $getState() 函数访问条目的状态值

<x-dynamic-component
    :component="$getEntryWrapperView()"
    :entry="$entry"
>
    {{ $getState() }}
</x-dynamic-component>

在 Blade 视图中访问其他组件的状态

在 Blade 视图中,你可以使用 $get() 函数访问 Schema 中其他组件的状态值:

<x-dynamic-component
    :component="$getEntryWrapperView()"
    :entry="$entry"
>
    {{ $get('email') }}
</x-dynamic-component>

TIP

除非表单字段是响应式的,否则当字段值改变时,Blade 视图不会刷新,只有当下一次用户交互向服务器发起请求时才会刷新。如果你需要在字段值改变时响应,请使用 live()

在 Blade 视图中访问 Eloquent 记录

在 Blade 视图中,你可以使用 $record 变量访问当前 Element 记录:

<x-dynamic-component
    :component="$getEntryWrapperView()"
    :entry="$entry"
>
    {{ $record->name }}
</x-dynamic-component>

在 Blade 视图中访问当前操作

在 Blade 视图中,你可以使用 $operation 变量访问当前操作(通常为 createedit 或者 view):

<x-dynamic-component
    :component="$getEntryWrapperView()"
    :entry="$entry"
>
    @if ($operation === 'create')
        This is a new conference.
    @else
        This is an existing conference.
    @endif
</x-dynamic-component>

在 Blade 视图中访问当前 Livewire 组件

在 Blade 视图中,你可以使用 $this 访问当前 Livewire 组件实例:

@php
    use Filament\Resources\Users\RelationManagers\ConferencesRelationManager;
@endphp

<x-dynamic-component
    :component="$getEntryWrapperView()"
    :entry="$entry"
>
    @if ($this instanceof ConferencesRelationManager)
        You are editing conferences the of a user.
    @endif
</x-dynamic-component>

在 Blade 视图中访问当前 Entry 实例

在 Blade 视图中,你可以使用 $entry 访问当前 Entry 组件实例。你可以在该对象上调用公用方法,访问其他以变量形式访问不到的信息:

<x-dynamic-component
    :component="$getEntryWrapperView()"
    :entry="$entry"
>
    @if ($entry->isLabelHidden())
        This is a new conference.
    @endif
</x-dynamic-component>

添加配置方法到自定义 Entry 类中

你可以添加 public 方法到自定义 Entry 类中,使之接受一个配置值,然后将该值保存到 protected 属性中,并在另一个 public 函数中返回该值:

use Filament\Infolists\Components\Entry;

class AudioPlayerEntry extends Entry
{
    protected string $view = 'filament.infolists.components.audio-player-entry';
    
    protected ?float $speed = null;

    public function speed(?float $speed): static
    {
        $this->speed = $speed;

        return $this;
    }

    public function getSpeed(): ?float
    {
        return $this->speed;
    }
}

现在,在自定义 Entry 的 Blade 视图中,你可以使用 $getSpeed() 函数访问该 speed:

<x-dynamic-component
    :component="$getEntryWrapperView()"
    :entry="$entry"
>
    {{ $getSpeed() }}
</x-dynamic-component>

任何你在自定义 Entry 类中定义的 public 方法,可以在 Blade 视图中以这样的方式作为变量函数访问。

要传递配置值到自定义 Entry 类中,你可以使用 public 方法:

use App\Filament\Infolists\Components\AudioPlayerEntry;

AudioPlayerEntry::make('recording')
    ->speed(0.5)

在自定义 Entry 配置方法中允许 utility 注入

Utility 注入是 Filament 的一个强大的功能,它允许用户使用可以访问各种 utility 的函数配置组件。你可以通过确保配置的参数类型和属性类型允许用户传递 Closure 闭包来允许 utility 注入。在 getter 方法中,你应该传入配置值到 $this-?evaluate() 方法中,如果用户传入函数,该方法会将 utility 注入到用户函数中,或者,如果传入的是静态值将返回该值:

use Closure;
use Filament\Infolists\Components\Entry;

class AudioPlayerEntry extends Entry
{
    protected string $view = 'filament.infolists.components.audio-player-entry';
    
    protected float | Closure | null $speed = null;

    public function speed(float | Closure | null $speed): static
    {
        $this->speed = $speed;

        return $this;
    }

    public function getSpeed(): ?float
    {
        return $this->evaluate($this->speed);
    }
}

现在,你可以传入一个静态值或者函数到 speed() 方法中,并注入 utiltiy作为参数:

use App\Filament\Infolists\Components\AudioPlayerEntry;

AudioPlayerEntry::make('recording')
    ->speed(fn (Conference $record): float => $record->isGlobal() ? 1 : 0.5)
Edit on GitHub

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

Previous
Repeatable entry