beholdr/filament-trilist
Composer 安装命令:
composer require beholdr/filament-trilist
包简介
Filament plugin that adds components for working with tree data: treeselect and treeview
README 文档
README
Filament plugin for working with tree data: treeselect input and treeview page. Based on Trilist package.
Support
Do you like Filament Trilist? Please support me via Boosty.
Features
- Treeselect input and treeview page
- Tree items can have multiple parents
- Works with relationship or custom hierarchical data
Installation
| Filament version | Package version |
|---|---|
| ^5.x | ^1.1.x |
| ^4.x | 1.x.x |
| ^3.x | 0.5.x |
You can install the package via composer:
composer require beholdr/filament-trilist
Optionally, you can publish the views using
php artisan vendor:publish --tag="filament-trilist-views"
Tree data
You can use hierarchical data from any source when it follows format:
[
['id' => 'ID', 'label' => 'Item label', 'children' => [
['id' => 'ID', 'label' => 'Item label', 'children' => [...]],
...
]
]
For example, you can use special library like staudenmeir/laravel-adjacency-list to get tree data:
Category::tree()->get()->toTree()
Or use custom relationship schema and methods, even with ManyToMany (multiple parents) relationship.
Example for self-referencing entity
Migrations
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('professions', function (Blueprint $table) { $table->id(); $table->string('label'); }); Schema::create('profession_profession', function (Blueprint $table) { $table->primary(['parent_id', 'child_id']); $table->foreignId('parent_id')->constrained('professions')->cascadeOnDelete(); $table->foreignId('child_id')->constrained('professions')->cascadeOnDelete(); }); } public function down(): void { Schema::dropIfExists('professions'); Schema::dropIfExists('profession_profession'); } };
Model
namespace App\Models; use Illuminate\Contracts\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; class Profession extends Model { protected $with = ['children']; public function parents() { return $this->belongsToMany(Profession::class, 'profession_profession', 'child_id', 'parent_id'); } public function children() { return $this->belongsToMany(Profession::class, 'profession_profession', 'parent_id', 'child_id'); } public function scopeRoot(Builder $builder) { $builder->doesntHave('parents'); } }
With given model you can generate tree data like this:
Profession::root()->get();
Treeselect input
Import TrilistSelect class and use it on your Filament form:
use Beholdr\FilamentTrilist\Components\TrilistSelect // with custom tree data TrilistSelect::make('category_id') ->options($treeData), // or with relationship TrilistSelect::make('categories') ->relationship('categories') ->options($treeData) ->multiple(),
Full options list:
TrilistSelect::make(string $fieldName) ->label(string $fieldLabel) ->placeholder(string | Closure $placeholder) ->disabled(bool | Closure $condition) // array of tree items ->options(array | Closure $options), // first argument defines name of the relationship, second can be used to modify relationship query ->relationship(string | Closure $relationshipName, ?Closure $modifyQueryUsing = null) // array of ids (or single id) of disabled items ->disabledOptions(string | int | array | Closure $value) // multiple selection mode, default: false ->multiple(bool | Closure $condition) // animate expand/collapse, default: true ->animated(bool | Closure $condition) // expand initial selected options, default: true ->expandSelected(bool | Closure $condition) // in independent mode children auto selected when parent is selected, default: false ->independent(bool | Closure $condition) // in leafs mode, the selected value is not grouped as the parent when all child elements are selected, default: false ->leafs(bool | Closure $condition) // tree item id field name, default: 'id' ->fieldId(string | Closure $value) // tree item label field name, default: 'label' ->fieldLabel(string | Closure $value) // tree item children field name, default: 'children' ->fieldChildren(string | Closure $value) // hook for generating custom labels, default: '(item) => item.label' ->labelHook(string | Closure $value) // enable filtering of items, default: false ->searchable(bool | Closure $condition) // enable autofocus on filter field, default: false ->autofocus(bool | Closure $condition) // search input placeholder ->searchPrompt(string | Htmlable | Closure $message) // select button label ->selectButton(string | Htmlable | Closure $message) // cancel button label ->cancelButton(string | Htmlable | Closure $message)
Custom labels
If you want to customize labels you can use labelHook method. It should return a string that will be processed as JS (pay attention to escaping quotes and special characters):
TrilistSelect::make('parent_id') ->labelHook(fn () => <<<JS (item) => `\${item.label} \${item.data?.description ? '<div style=\'font-size: 0.85em; opacity: 0.5\'>' + item.data.description + '</div>' : ''}` JS)
Usage in filters
You can use treeselect in custom filter:
use App\Models\Category; use Filament\Tables\Filters\Filter; use Illuminate\Database\Eloquent\Builder; Filter::make('category') ->form([ TrilistSelect::make('category_id') ->multiple() ->independent() ->options(Category::tree()->get()->toTree()) ]) ->query(function (Builder $query, array $data) { $query->when( $data['category_id'], function (Builder $query, $values) { $ids = Category::query() ->whereIn('id', $values) ->get() ->map ->descendantsAndSelf ->flatten() ->pluck('id') ->toArray(); $query->whereIn('category_id', $ids); } ); }) ->indicateUsing(function (array $data) { if (! $data['category_id']) return null; return Category::whereIn('id', $data['category_id'])->pluck('name')->toArray(); }),
Treeview page
Create custom page class inside Pages directory of your Filament directory. Note that page class extends Beholdr\FilamentTrilist\Components\TrilistPage:
If you create custom page with
php artisan make:filament-pagecommand, then selectNooption for creating this page in a resource. After page creation you can delete created page view file and$viewproperty of the page class.
namespace App\Filament\Pages; use App\Filament\Resources\PostResource; use App\Models\Post; use Beholdr\FilamentTrilist\Components\TrilistPage; class TreePosts extends TrilistPage { // optional resource class if you want to link tree items to a resource edit page protected static ?string $resource = PostResource::class; // optional, if you want to override default title protected static ?string $title = 'Posts Tree'; // optional navigation parent page title protected static ?string $navigationParentItem = 'Categories'; // return array of tree items (see below about tree data) public function getTreeOptions(): array { return Post::root()->get()->toArray(); } }
Treeview options
You can set some tree options by overriding static methods in the custom page class:
class TreeCategories extends TrilistPage { public static function getFieldLabel(): string { return 'name'; } }
getFieldId(): tree item id field namegetFieldLabel(): tree item label field namegetFieldChildren(): tree item children field nameisAnimated(): animate expand/collapse, default: trueisSearchable(): enable filtering of items, default: falsegetSearchPrompt(): search input placeholder
Custom labels
If you want to customize labels of the tree items, you can override getLabelHook() method of the TrilistPage.
Example
Say, model for your tree items has a description field that you want to output below the item name. All additional properties of your model are under item.data property, so description will be at item.data.description:
public function getLabelHook(): string { if (! $editRoute = $this->getEditRoute()) { return 'undefined'; } $template = route($editRoute, ['record' => '#ID#'], false); return <<<JS (item) => `<a href='\${'{$template}'.replace('#ID#', item.id)}'>\${item.label}</a> \${item.data?.description ? '<div style=\'font-size: 0.85em; opacity: 0.5\'>' + item.data.description + '</div>' : ''}` JS; }
License
The MIT License (MIT). Please see License File for more information.
beholdr/filament-trilist 适用场景与选型建议
beholdr/filament-trilist 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.96k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2023 年 11 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「tree」 「laravel」 「treeview」 「filament」 「trilist」 「treeselect」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 beholdr/filament-trilist 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 beholdr/filament-trilist 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 beholdr/filament-trilist 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
jsTree widget for yii2
An enhanced tree management module with tree node selection and manipulation using nested sets.
Extension for Yii2 Framework to work with jQuery TreeGrid
Doctrine2 behavior traits - slowhop fork
A fast and dynamic Merkle tree implementation
Data structure collections, helpers and utilities for yii2 ActiveRecord models(trees, sorts and etc.)
统计信息
- 总下载量: 26.96k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-21