说明
在此代码中,在BadgeColumn中,我使用$this->getTableFilterState('created_at')['date']
来获取筛选器数据,但这显示错误
像
不在对象上下文中时使用$this
这是我的PayRollResource.php
public static function table(Table $table): Table
{
$date = Carbon::now()->format('Y-m-d');
return $table
->query(Employee::active()->whereDate('created_at', '<=', $date))
->columns([
TextColumn::make('name')
->searchable(),
TextColumn::make('email')
->searchable(),
TextColumn::make('phone')
->searchable(),
TextColumn::make('unique_id')
->searchable(),
TextColumn::make('salary'),
TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
BadgeColumn::make('payroll_status')
->label("PayRoll Status")
->colors([
'success' => static fn ($state): bool => $state == 'Paid',
'danger' => static fn ($state): bool => $state == 'Unpaid',
])
->getStateUsing(function (Employee $record) {
return $record->payrollStatus($this->getTableFilterState('created_at')['date']);
}),
TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
Filter::make('created_at')
->form([
DatePicker::make('date'),
])
->indicateUsing(function (array $data): array {
$indicators = [];
if ($data['date']) {
$indicators['date'] = 'Date ' . Carbon::parse($data['date'])->toFormattedDateString();
}
return $indicators;
}),
])
这是我的工资单页面
<?php
namespace App\Filament\Pages;
use Carbon\Carbon;
use Pages\ViewEmployee;
use App\Models\Employee;
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
use Filament\Pages\Page;
use Filament\Tables\Table;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Contracts\HasTable;
use Filament\Forms\Components\DatePicker;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Actions\Action as ActionsAction;
use Filament\Tables\Columns\BadgeColumn;
class Payroll extends Page implements HasTable
{
use InteractsWithTable, HasPageShield;
protected static ?string $navigationIcon = 'heroicon-o-banknotes';
protected static string $view = 'filament.pages.payroll';
public function table(Table $table): Table
{
$date = Carbon::now()->format('Y-m-d');
return $table
->query(Employee::active()->whereDate('created_at', '<=', $date))
->columns([
TextColumn::make('name')
->searchable(),
TextColumn::make('email')
->searchable(),
TextColumn::make('phone')
->searchable(),
TextColumn::make('unique_id')
->searchable(),
TextColumn::make('salary'),
TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
BadgeColumn::make('payroll_status')
->label("PayRoll Status")
->colors([
'success' => static fn ($state): bool => $state == 'Paid',
'danger' => static fn ($state): bool => $state == 'Unpaid',
])
->getStateUsing(function (Employee $record) {
return $record->payrollStatus($this->getTableFilterState('created_at')['date']);
}),
TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
Filter::make('created_at')
->form([
DatePicker::make('date'),
])
->indicateUsing(function (array $data): array {
$indicators = [];
if ($data['date']) {
$indicators['date'] = 'Date ' . Carbon::parse($data['date'])->toFormattedDateString();
}
return $indicators;
}),
])
->actions([
ActionsAction::make('View')
->color('success')
->icon('heroicon-o-banknotes')
->url(function (Employee $record): string {
return route('payroll.view', [$record->id, 'date' => $this->getTableFilterState('created_at')['date']]);
})
])
->bulkActions([]);
}
}
在本页中,$this->getTableFilterState
工作正常。我想改变这个自定义页面到灯丝资源,但在默认的资源页$this->getTableFilterState
是不工作!为什么会这样?对此有何建议,请参考
我想在Filament资源页面上使用$this->getTableFilterState
1条答案
按热度按时间4bbkushb1#
您遇到的问题是因为$this->getTableFilterState在默认的Filament资源页面中不可用。您似乎有一个自定义Filament页面正在使用此方法,但当您使用默认资源页面时,它无法工作,因为该方法在该上下文中不可用。
如果您想在Filament资源页面中使用$this->getTableFilterState或类似功能,则需要创建自定义资源页面。下面是一个基本示例,说明如何创建具有所需功能的自定义资源页面:
public static $icon = 'hereoicon-o-softes';
}
使用Filament\Tables\Concerns\InteractsWithTable;
类YourCustomTable扩展Table { use InteractsWithTable;
}