hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

v1.2.0 2024-03-18 08:32 UTC

This package is auto-updated.

Last update: 2024-04-26 14:34:13 UTC


README

Latest Version on Packagist Total Downloads

This package brings a simple toggle button to Filament tables, allowing end users to switch between Grid and Table layouts on tables. This approach allows mobile users to benefit from the Grid layout, while desktop users can still benefit from Table layout features, such as the table headers, sorting and so on.

Big shoutout to awcodes/filament-curator, which implemented the toggle feature first on their package. This package is mainly an extraction of the feature so that it can be used in any project, and some other adding such as saving the selected layout in the local storage.

Screenshots

screencast.mov

screenshot_table screenshot_grid

Installation

You can install the package via composer:

composer require hydrat/filament-table-layout-toggle

Optionally, you can publish the views using

php artisan vendor:publish --tag="table-layout-toggle-views"

If you are using this package outside of filament panels (standalone tables), you should publish the configuration file :

php artisan vendor:publish --tag="table-layout-toggle-config"

If using panels, this configuration file WILL NOT be read by the plugin, as the configuration happens on the plugin registration itself.

Usage

Please chose the appropriate section for your use case (Panels or Standalone tables).

Panels

First, register the plugin on your Filament panel :

use Hydrat\TableLayoutToggle\TableLayoutTogglePlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        ->plugins([
            TableLayoutTogglePlugin::make()
                ->defaultLayout('grid') // default layout for user seeing the table for the first time
                ->persistLayoutInLocalStorage(true) // allow user to keep his layout preference in his local storage
                ->shareLayoutBetweenPages(false) // allow all tables to share the layout option (requires persistLayoutInLocalStorage to be true)
                ->displayToggleAction() // used to display the toggle action button automatically
                ->toggleActionHook('tables::toolbar.search.after') // chose the Filament view hook to render the button on
                ->listLayoutButtonIcon('heroicon-o-list-bullet')
                ->gridLayoutButtonIcon('heroicon-o-squares-2x2'),
        ]);
}

Please note that all those configurations are optional, and have default values, which means you can omit them if you don't need to change the default behavior.

Then, on the component containing the table (ListRecord, ManageRelatedRecords, ...), you can use the HasToggleableTable trait :

use Hydrat\TableLayoutToggle\Concerns\HasToggleableTable;

class MyListRecords extends ListRecords
{
    use HasToggleableTable;
}

Finally, you need to configure your table so it dynamically sets the schema based on the selected layout. This is typically done on the resource's table() method :

public static function table(Table $table): Table
{
    $livewire = $table->getLivewire();

    return $table
        ->columns(
            $livewire->isGridLayout()
                ? static::getGridTableColumns()
                : static::getListTableColumns()
        )
        ->contentGrid(
            fn () => $livewire->isListLayout()
                ? null
                : [
                    'md' => 2,
                    'lg' => 3,
                    'xl' => 4,
                ]
        );
}

public static function getListTableColumns(): array;
public static function getGridTableColumns(): array;

Please note that you must use the Layout tools described in the filament documentation in order for your Grid layout to render correctly. You may also use the description() method to print labels above your values.

public static function getGridTableColumns(): array
{
    return [
        // Make sure to stack your columns together
        Tables\Columns\Layout\Stack::make([

            Tables\Columns\TextColumn::make('status')->badge(),

            // You may group columns together using the Split layout, so they are displayed side by side
            Tables\Columns\Layout\Split::make([
                Tables\Columns\TextColumn::make('customer')
                    ->description(__('Customer'), position: 'above')
                    ->searchable(),

                Tables\Columns\TextColumn::make('owner.name')
                    ->description(__('Owner'), position: 'above')
                    ->searchable(),
            ]),

        ])->space(3)->extraAttributes([
            'class' => 'pb-2',
        ]),
    ];
}

Standalone tables

You can manage the plugin settings via the published configuration file. The options are self-documented, and should be pretty straightforward to use.

Then, on the component containing the table, you can use the HasToggleableTable trait :

namespace App\Livewire\Users;

use Hydrat\TableLayoutToggle\Concerns\HasToggleableTable;

class ListUsers extends Component implements HasForms, HasTable, HasActions
{
    use InteractsWithTable;
    use InteractsWithActions;
    use InteractsWithForms;
    use HasToggleableTable; // <-- Add this line
}

If you plan to persist the layout in the local storage, you must also change your view to include the needed assets :

[...]
{{ $this->table }}

{{ $this->renderLayoutViewPersister() }} <-- Add this line

Finally, you need to configure your table so it dynamically sets the schema based on the selected layout. This is typically done on the component's table() method :

public function table(Table $table): Table
{
    return $table
        ->columns(
            $this->isGridLayout()
                ? $this->getGridTableColumns()
                : $this->getListTableColumns()
        )
        ->contentGrid(
            fn () => $this->isListLayout()
                ? null
                : [
                    'md' => 2,
                    'lg' => 3,
                    'xl' => 4,
                ]
        );
}

protected function getListTableColumns(): array;
protected function getGridTableColumns(): array;

Please note that you must use the Layout tools described in the filament documentation in order for your Grid layout to render correctly. You may also use the description() method to print labels above your values.

public static function getGridTableColumns(): array
{
    return [
        // Make sure to stack your columns together
        Tables\Columns\Layout\Stack::make([

            Tables\Columns\TextColumn::make('status')->badge(),

            // You may group columns together using the Split layout, so they are displayed side by side
            Tables\Columns\Layout\Split::make([
                Tables\Columns\TextColumn::make('customer')
                    ->description(__('Customer'), position: 'above')
                    ->searchable(),

                Tables\Columns\TextColumn::make('owner.name')
                    ->description(__('Owner'), position: 'above')
                    ->searchable(),
            ]),

        ])->space(3)->extraAttributes([
            'class' => 'pb-2',
        ]),
    ];
}

Change settings per-table

Some settings can be configured per-table, such as the default layout, the layout persistence, and the persistence local storage name :

namespace App\Livewire\Users;

class ListUsers extends Component implements HasForms, HasTable, HasActions
{
    use HasToggleableTable;

    public function getDefaultLayoutView(): string
    {
        return 'grid';
    }

    protected function persistToggleEnabled(): bool
    {
        return false;
    }

    protected function persistToggleStatusName(): string
    {
        return 'tableLayoutView::listUsersTable';
    }
}

Using own action

If you rather use your own action instead of the default one, you should first disable it on the plugin registration :

$panel
    ->plugins([
      TableLayoutTogglePlugin::make()
          ->displayToggleAction(false),
    ])

Then, you can get and extend base Action or TableAction from the provided helper :

use Hydrat\TableLayoutToggle\Facades\TableLayoutToggle;

# eg: Display action on top of the table :
return $table
    ->columns(...)
    ->headerActions([
        TableLayoutToggle::getToggleViewTableAction(compact: true),
    ]);

# eg: As Filament page header action :
protected function getHeaderActions(): array
{
    return [
        TableLayoutToggle::getToggleViewAction(compact: false)
            ->hiddenLabel(false)
            ->label('Toggle layout'),
    ];
}

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.