简体   繁体   English

Filamentphp,Ho刷新/重新计算header上的计数值

[英]Filamentphp, Ho to refresh/re-calculate the count value on header

I made CommentsRelationManager for PostResource.我为 PostResource 创建了 CommentsRelationManager。

How I can refresh/recalculate the count value on header, whatever the comments is added or deleted.无论添加或删除评论,我如何刷新/重新计算 header 上的计数值。

在此处输入图像描述

Image note: I add a new comment, but the count value not refreshing.图片说明:我添加了一条新评论,但计数值没有刷新。

This is my PostResource Form:这是我的 PostResource 表格:

public static function form(Form $form): Form
{
    return $form
        ->schema([
            TextInput::make('title')->required(),
            TextInput::make('body')->required(),
            TextInput::make('count')
                ->reactive()
                ->label('count')
                ->disabled()
                ->placeholder(fn ($record) => $record->comments()->count())

        ]);
}

And this is my RelationManager这是我的 RelationManager

class CommentsRelationManager extends RelationManager
{ ...
    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('comment'),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\CreateAction::make(),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\DeleteBulkAction::make(),
            ]);
    }
}

the last suggestion is:最后的建议是:

Emit a Livewire event from the relation manager after() the Edit or Create action, 
and register a listener on the page, calling $refresh. This will refresh the value of the placeholder.

But It's not clear for me, and I don't know how to do it.但这对我来说还不清楚,我不知道该怎么做。

Anyone can help me Please... Thank you.任何人都可以帮助我请...谢谢。

Following your last suggestion I have achieved the desired result with this solution:根据您的最后建议,我已经通过此解决方案取得了预期的结果:

Assuming these files exist in the Filament/Resources directory假设这些文件存在于 Filament/Resources 目录中

  • PostResource.php PostResource.php
  • PostResource/Pages/EditPost.php PostResource/Pages/EditPost.php
  • PostResource/RelationManagers/CommentsRelationManager.php PostResource/RelationManagers/CommentsRelationManager.php

In the EditPost.php file register the livewire event listener and use the fillForm() function to refresh the formEditPost.php文件中注册 livewire 事件侦听器并使用 fillForm() function 刷新表单

<?php

class EditPost extends EditRecord
{
    ....

    protected $listeners = ['refresh'=>'refreshForm'];

    ....

    public function refreshForm()
    {
        $this->fillForm();
    }
}

In the CommentsRelationManager.php file use the after() function to emit the livewire event registered before在 CommentsRelationManager.php 文件中使用 after() function 发出之前注册的 livewire 事件

<?php

....

use Filament\Resources\RelationManagers\RelationManager;

class CommentsRelationManager extends RelationManager
{
    ....

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                ....
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\CreateAction::make()
                    ->after(function (RelationManager $livewire){
                        $livewire->emit('refresh');
                    })
                ,
            ])
    }    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM