pojow/laravel-collection-table

从 Laravel 集合生成表格

v0.1.4 2024-06-16 10:56 UTC

This package is auto-updated.

Last update: 2024-09-16 11:36:32 UTC


README

Laravel Collection Table illustration

Latest Stable Version Total Downloads Build Status Coverage Status License: MIT

我们的包允许您从 Laravel 集合生成功能齐全的 HTML 表格。生成的表格支持多种功能,包括过滤、排序、搜索和操作。

这个轻量级的包专为开发者设计,提供易于定制的表格。您可以轻松创建新的过滤器、列格式等,以满足您的特定需求。

此包可以与以下 UI 框架一起使用

  • Bootstrap 5
  • TailwindCSS 3 (beta)
  • 自定义:包中的所有视图都可以被覆盖,允许您精确管理表格的显示方式。

兼容性

使用示例

使用以下命令创建您的表格

php artisan make:table UsersTable 

在生成的 UsersTable 类中配置您的表格,该类位于 app\Tables 目录中

namespace App\Tables;

use App\Models\User;
use Pojow\LaravelCollectionTable\Abstracts\AbstractTableConfiguration;
use Pojow\LaravelCollectionTable\Column;
use Pojow\LaravelCollectionTable\Filters\SelectFilter;
use Pojow\LaravelCollectionTable\RowActions\DestroyRowAction;
use Pojow\LaravelCollectionTable\RowActions\EditRowAction;
use Pojow\LaravelCollectionTable\Table;

class UsersTable extends AbstractTableConfiguration
{
    protected function table(): Table
    {
        return Table::make()
            ->collection(User::all())
            ->filters([
                (new SelectFilter(__('Role'), 'role'))->options(['user', 'administrator']),
            ])
            ->rowActions([
                new EditRowAction('user.edit', 'id'),
                new DestroyRowAction('user.destroy', 'id'),
            ]);
    }

    protected function columns(): array
    {
        return [
            Column::make('username')
                ->searchable()
                ->sortable(),
            Column::make('first_name')
                ->searchable()
                ->sortable(),
            Column::make('last_name'),
            Column::make('email')
                ->searchable()
                ->sortable()
                ->format(fn (User $user) => "<a href='mailto:{$user->email}'>{$user->email}</a>", false),
        ];
    }
}

并在视图中显示它

<x:laravel-collection-table :config="App\Tables\UsersTable::class"/>

查看结果 Table usage example

目录

安装

您可以通过 composer 安装此包

composer require pojow/laravel-collection-table

配置

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag=laravel-collection-table:config

在其配置中,此包允许您选择要使用的 UI 框架。

请注意,在使用此包之前,您必须安装并配置您想要的 UI 框架,或者您必须覆盖所有视图并创建自己的。

视图

您可以在需要时发布包视图以进行自定义

php artisan vendor:publish --tag=laravel-collection-table:views

测试

composer test

更新日志

有关最近更改的更多信息,请参阅 更新日志

鸣谢

许可

MIT 许可证(MIT)。有关更多信息,请参阅 许可文件