MoonShine MoonShine
EN
← All plugins

Sortable tree resource

Made by the authors of MoonShine

MoonShine Tree Resource - это расширение для MoonShine v2.0+, которое добавляет возможность создания и управления древовидными структурами с функцией сортировки (drag-and-drop), позволяя легко организовывать иерархические данные, такие как категории или меню.

Rating
Downloads
7444
Version
2.0.0
Last updated
18.12.2024
MoonShine version
v2, v3
Github stars
15
Danil Shutsky
Author
Danil Shutsky

MoonShine sortable tree resource

Requirements

  • MoonShine v2.0+

Installation

composer require lee-to/moonshine-tree-resource
composer require lee-to/moonshine-tree-resource

Get started

Example usage with tree

use Leeto\MoonShineTree\Resources\TreeResource;
 
class CategoryResource extends TreeResource
{
// Required
protected string $column = 'title';
 
protected string $sortColumn = 'sorting';
 
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';
}
 
public function sortKey(): string
{
return 'sorting';
}
 
// ...
}
use Leeto\MoonShineTree\Resources\TreeResource;
 
class CategoryResource extends TreeResource
{
// Required
protected string $column = 'title';
 
protected string $sortColumn = 'sorting';
 
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';
}
 
public function sortKey(): string
{
return 'sorting';
}
 
// ...
}

And add component

namespace App\MoonShine\Pages;
 
use Leeto\MoonShineTree\View\Components\TreeComponent;
use MoonShine\Pages\Crud\IndexPage;
 
class CategoryTreePage extends IndexPage
{
protected function mainLayer(): array
{
return [
...$this->actionButtons(),
TreeComponent::make($this->getResource()),
];
}
}
namespace App\MoonShine\Pages;
 
use Leeto\MoonShineTree\View\Components\TreeComponent;
use MoonShine\Pages\Crud\IndexPage;
 
class CategoryTreePage extends IndexPage
{
protected function mainLayer(): array
{
return [
...$this->actionButtons(),
TreeComponent::make($this->getResource()),
];
}
}

Just a sortable usage

use Leeto\MoonShineTree\Resources\TreeResource;
 
class CategoryResource extends TreeResource
{
// Required
protected string $column = 'title';
 
protected string $sortColumn = 'sorting';
 
// ... fields, model, etc ...
 
public function treeKey(): ?string
{
return null;
}
 
public function sortKey(): string
{
return 'sorting';
}
 
// ...
}
use Leeto\MoonShineTree\Resources\TreeResource;
 
class CategoryResource extends TreeResource
{
// Required
protected string $column = 'title';
 
protected string $sortColumn = 'sorting';
 
// ... fields, model, etc ...
 
public function treeKey(): ?string
{
return null;
}
 
public function sortKey(): string
{
return 'sorting';
}
 
// ...
}

Additional content

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

Turn off sortable or wrapable

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