filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
19.28k stars 2.96k forks source link

Column Toggleable() doesn't work with Table Split #7572

Closed antoinemy closed 1 year ago

antoinemy commented 1 year ago

Package

filament/filament

Package Version

v3

Laravel Version

v10

Livewire Version

v3

PHP Version

8.1

Problem description

Here is a video of a classic table toggleable() in filamentphp:

https://github.com/filamentphp/filament/assets/9550130/b8046318-512b-49e2-a015-21ea64d7e0bf

However when we find in a split view or another classic off-table method the toggleable() does not work:

https://github.com/filamentphp/filament/assets/9550130/0e843ece-4440-4130-b795-d665a1477129

Expected behavior

Whether the toggleable() works in a split or other views

Steps to reproduce

Here is the difference between the two videos. The codes are very basic, you will be able to reproduce it quite easily.

Classic tables:

Tables\Columns\ImageColumn::make('image')
    ->label('')
    ->circular()
    ->grow(false),

Tables\Columns\TextColumn::make('name')
    ->label('Nom')
    ->searchable()
    ->grow(true)
    ->sortable(['first_name', 'last_name']),

Tables\Columns\TextColumn::make('created_at')
    ->label('Date de création')
    ->searchable()
    ->badge()
    ->grow(false)
    ->sortable()
    ->toggleable(),

Table with Split:

Tables\Columns\Layout\Split::make([
    Tables\Columns\ImageColumn::make('image')
        ->label('')
        ->circular()
        ->grow(false),

    Tables\Columns\TextColumn::make('name')
        ->label('Nom')
        ->searchable()
        ->grow(true)
        ->sortable(['first_name', 'last_name']),

    Tables\Columns\TextColumn::make('created_at')
        ->label('Date de création')
        ->searchable()
        ->badge()
        ->grow(false)
        ->sortable()
        ->toggleable(),
]),

Reproduction repository

https://github.com/antoinemy

Relevant log output

No response

danharrin commented 1 year ago

Yeah this is deliberate, there's not a good way to consistently hide table columns in table layout components without it having knock-on effects on how the layout itself looks. Maybe someone can submit a PR if they have an idea on how to implement it, but I have no interest in it.

bendbot commented 3 weeks ago

Мне тоже не понравилось такое поведение и попробовал его исправить. Добавил метод isToggledHidden() в класс Filament\Tables\Columns\Layout с помощью mixin.

Файл app/Macros/Filament/Tables/Columns/Layout/ComponentMacros.php

namespace App\Macros\Filament\Tables\Columns\Layout;

use Filament\Tables\Columns\Column;

class ComponentMacros
{
    public function isToggledHidden()
    {
        return function () {
            /** @var \Filament\Tables\Columns\Layout\Component $this */

            $items = $this->getColumns();
            $itemsCount = count($items);

            $toggledHiddenItems = count(array_filter(
                $items,
                fn(Column $column): bool => $column->isToggledHidden(),
            ));

            return $itemsCount === $toggledHiddenItems;
        };
    }
}

Далее в сервисе провайдере нужно зарегистрировать миксин .

public function register(): void
{
    parent::register();

    \Filament\Tables\Columns\Layout\Component::mixin(new \App\Macros\Filament\Tables\Columns\Layout\ComponentMacros());
}

Далее нужно переопределить файл blade шаблона, скопировав vendor/filament/tables/resources/views/components/columns/layout.blade.php в resources/views/vendor/filament-tables/components/columns/layout.blade.php.

И теперь добавить проверку ! $layoutComponent->isToggledHidden() в этот блок.

https://github.com/user-attachments/assets/83b30972-cd80-43a4-bb0b-0b45f202ed6d