invaders-xx/filament-nested-list

Filament嵌套列表布局插件

v1.0 2024-05-13 15:45 UTC

This package is auto-updated.

Last update: 2024-09-24 09:00:46 UTC


README

invaders-xx-nested-list

Filament Nested List

Filament Nested List是Filament Admin的一个插件,它创建了一个具有遗产嵌套列表结构视图的模型管理页面。此插件可用于创建菜单等。

此插件为Filament Admin创建了一个具有遗产嵌套列表结构视图的模型管理页面。它可以用于创建菜单等。

安装

要安装此包,请运行以下命令

composer require invaders-xx/filament-nested-list
php artisan filament:assets

注意:将插件Blade文件添加到您的自定义主题tailwind.config.js以启用暗色模式。

要设置自己的自定义主题,您可以访问Filament网站上的官方说明页面

将插件的视图添加到您的tailwind.config.js文件。

content: [
    '<path-to-vendor>/invaders-xx/filament-nested-list/resources/**/*.blade.php',
]

然后,使用以下命令发布配置文件

php artisan vendor:publish --tag="filament-nested-list-config"

您可以通过将以下代码添加到您的config/filament-nested-list.php文件来设置您首选的选项

<?php

return [
    /**
     * Tree model fields
     */
    'column_name' => [
        'order' => 'order',
        'parent' => 'parent_id',
        'title' => 'title',
    ],
    /**
     * Tree model default parent key
     */
    'default_parent_id' => -1,
    /**
     * Tree model default children key name
     */
    'default_children_key_name' => 'children',
];

用法

准备数据库和模型

要使用Filament Nested List,请遵循以下表结构约定

提示:字段parent_id必须始终默认为-1!!!

Schema::create('product_categories', function (Blueprint $table) {
    $table->id();
    $table->integer('parent_id')->default(-1);
    $table->integer('order')->default(0)->index();
    $table->string('title');
    $table->timestamps();
});

此插件提供了一个名为nestedListColumns()的便捷方法,您可以使用它更轻松地将嵌套列表结构所需列添加到您的表中。以下是一个示例

Schema::create('product_categories', function (Blueprint $table) {
    $table->id();
    $table->nestedListColumns();
    $table->timestamps();
});

这将自动将所需的列添加到您的表中,用于嵌套列表结构。

上述表结构包含三个必需字段:parent_idordertitle,其他字段没有要求。

相应的模型是app/Models/ProductCategory.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use InvadersXX\FilamentNestedList\Concern\ModelNestedList;

class ProductCategory extends Model
{
     use ModelNestedList;

    protected $fillable = ["parent_id", "title", "order"];

    protected $table = 'product_categories';
}

表结构中三个字段parent_idordertitle的名称也可以修改

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use InvadersXX\FilamentNestedList\Concern\ModelNestedList;

class ProductCategory extends Model
{
    use ModelNestedList;

    protected $fillable = ["parent_id", "title", "order"];

    protected $table = 'product_categories';

    // Default if you need to override

    // public function determineOrderColumnName(): string
    // {
    //     return "order";
    // }

    // public function determineParentColumnName(): string
    // {
    //     return "parent_id";
    // }

    // public function determineTitleColumnName(): string
    // {
    //     return 'title';
    // }

    // public static function defaultParentKey()
    // {
    //     return -1;
    // }

    // public static function defaultChildrenKeyName(): string
    // {
    //     return "children";
    // }

}

小部件

Filament提供了一个强大的功能,允许您在页面上显示小部件,在页眉下方和页脚上方。这可以用于向资源页面添加额外的功能。

要创建嵌套列表小部件并将其应用于资源页面,您可以按照以下步骤操作

1. 创建Filament资源页面

要创建资源页面,请运行以下命令

php artisan make:filament-resource ProductCategory

2. 创建嵌套列表小部件

准备filament-nested-list小部件,并在资源页面中显示它。

php artisan make:filament-nested-list-widget ProductCategoryWidget

现在您可以在Filament文件夹中看到小部件

<?php

namespace App\Filament\Widgets;

use App\Models\ProductCategory as ModelsProductCategory;
use App\Filament\Widgets;
use Filament\Forms\Components\TextInput;
use InvadersXX\FilamentNestedList\Widgets\NestedList as BaseWidget;

class ProductCategoryWidget extends BaseWidget
{
    protected static string $model = ModelsProductCategory::class;

    // you can customize the maximum depth of your tree
    protected static int $maxDepth = 2;

    protected ?string $itle = 'ProductCategory';

    protected bool $enableTitle = true;

    protected function getFormSchema(): array
    {
        return [
            TextInput::make('title'),
        ];
    }
}

3. 在资源页面上显示小部件

一旦创建了小部件,请修改资源页面的getHeaderWidgets()getFooterWidgets()方法以显示嵌套列表视图

<?php

namespace App\Filament\Resources\ProductCategoryResource\Pages;

use App\Filament\Resources\ProductCategoryResource;
use App\Filament\Widgets\ProductCategory;
use Filament\Pages\Actions;
use Filament\Resources\Pages\ListRecords;

class ListProductCategories extends ListRecords
{
    protected static string $resource = ProductCategoryResource::class;

    protected function getActions(): array
    {
        return [
            Actions\CreateAction::make(),
        ];
    }

    protected function getHeaderWidgets(): array
    {
        return [
            ProductCategory::class
        ];
    }
}

发布视图

要发布视图,请使用

php artisan vendor:publish --tag="filament-nested-list-views"

发布翻译

要发布翻译,请使用

php artisan vendor:publish --tag="filament-nested-list-translations"

测试

要运行测试,请运行

composer test

更改日志

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

贡献

有关详细信息,请参阅CONTRIBUTING

安全漏洞

如果您发现任何与安全相关的问题,请通过电子邮件info+package@solutionforest.net报告,而不是使用问题跟踪器。

致谢

许可证

丝材嵌套列表是开源软件,使用MIT许可证许可。

关于我们