djnew/moonshine-nested-set

moonshine 的树形资源

0.0.8 2024-05-23 15:21 UTC

This package is auto-updated.

Last update: 2024-09-23 16:06:29 UTC


README

此包的资源树基于 kalnoy/nestedset

要求

  • MoonShine v2.0+

安装

composer require djnew/moonshine-nested-set

开始使用

使用树形结构的示例

use Djnew\MoonShineNestedSet\Resources\NestedsetResource;

class CategoryResource extends NestedsetResource
{
    // Required
    protected string $column = 'title';

    protected string $model    = Page::class;

    // Custom child relation name
    public string $treeRelationName = 'children';

    // Use pagination
    protected bool $usePagination = true;

    // Show Up/Down element buttons for sort
    public bool $showUpDownButtons = false;

    // Items per page
    protected int $itemsPerPage = 10;


    protected function pages(): array
    {
        return [
            CategoryTreePage::make($this->title()),
            FormPage::make(
                $this->getItemID()
                    ? __('moonshine::ui.edit')
                    : __('moonshine::ui.add')
            ),
            DetailPage::make(__('moonshine::ui.show')),
        ];
    }

    // ... fields, model, etc ...

    public function treeKey(): ?string
    {
        return 'parent_id';
    }

    // ...
}

并添加组件

namespace App\MoonShine\Pages;

use Djnew\MoonShineNestedSet\View\Components\NestdSetComponent;
use MoonShine\Pages\Crud\IndexPage;

class CategoryTreePage extends IndexPage
{
    protected function mainLayer(): array
    {
        return [
            ...$this->actionButtons(),
            NestdSetComponent::make($this->getResource()),
        ];
    }
}

向模型添加迁移

use Kalnoy\Nestedset\NestedSet;

Schema::create('table', function (Blueprint $table) {
    ...
    NestedSet::columns($table);
});

删除列

...
use Kalnoy\Nestedset\NestedSet;

Schema::table('table', function (Blueprint $table) {
    NestedSet::dropColumns($table);
});

向模型添加特性

namespace App\Models;

use App\Traits\MoonshineNestedSetTrait;
use Illuminate\Database\Eloquent\Model;


class Page extends Model
{
    use moonshineNestedSetTrait;
    
    // ...
}

迁移现有数据

从其他嵌套集扩展迁移

如果你的前一个扩展使用了不同的列集,你只需要在你的模型类中重写以下方法

public function getLftName()
{
    return 'left';
}

public function getRgtName()
{
    return 'right';
}

public function getParentIdName()
{
    return 'parent';
}

// Specify parent id attribute mutator
public function setParentAttribute($value)
{
    $this->setParentIdAttribute($value);
}

从基本父级信息迁移

如果你的树包含 parent_id 信息,你需要在你的模式中添加两列

$table->unsignedInteger('_lft');
$table->unsignedInteger('_rgt');

设置模型 后,你只需要调整树以填充 _lft_rgt

MyModel::fixTree();

附加内容

public function itemContent(Model $item): string
{
    return 'Custom content here';
}

关闭可排序或可包装

public function wrapable(): bool
{
    return false;
}

使用异步重新加载树形资源

namespace App\MoonShine\Pages;

use Djnew\MoonShineNestedSet\View\Components\NestdSetComponent;
use MoonShine\Pages\Crud\IndexPage;

class CategoryTreePage extends IndexPage
{
    protected function mainLayer(): array
    {
        return [
            ...$this->actionButtons(),
            Fragment::make(
                [
                    NestdSetComponent::make($this->getResource())
                        ->setFragmentName('fragment-name')
                ]
            )->name('fragment-name')
            ->updateAsync(['page' => request()->get('page')]),
        ];
    }
}```