terehinis / orchid-repeater-field
为Orchid RAD平台添加重复字段支持。
Requires
- laravel/framework: ^10.0
- orchid/platform: ^14.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- mockery/mockery: ^1.1
- phpunit/phpunit: ^10.4
- sempro/phpunit-pretty-print: ^1.0
This package is auto-updated.
Last update: 2024-09-19 09:40:59 UTC
README
简介
我们都曾在某个时候使用过WordPress。受Advanced Custom Fields的启发。
此包向Orchid RAD平台添加重复字段支持。
先决条件
您必须已安装并配置Orchid Platform
版本支持: .*
对于^13.0.1版本,请使用^13.0.0标签和更高版本。
旧版本不支持大多数当前功能。
对于6.*版本,请使用2.0.5标签。
对于5版本,请使用1.0.0标签。
对于4.7.1版本,请使用0.0.8标签。
如何使用
-
使用composer安装包:
composer require nakukryskin/orchid-repeater-field
最新版本的Laravel会自动发现并使用此包。
-
在您的Orchid布局目录中创建
RepeaterFields.php
。示例
<?php namespace App\Http\Orchid\Layouts\Repeaters; use Orchid\Screen\Layouts\Rows; use Orchid\Screen\Fields\Input; use Orchid\Screen\Fields\Select; class RepeaterFields extends Rows { function fields(): array { return [ Input::make('repeater_name') ->type('text') ->max(255) ->required() ->title('Nested Field'), Select::make('select.') ->multiple() ->options([ 'test' => 'Test', 'test2' => 'Test2' ]) ->required() ]; } }
- 简单地在您的屏幕中添加
RepeaterField::make('repeater')
:示例
public function layout(): array { return [ Layout::rows([ RepeaterField::make('repeater') ->title('Repeater') ->layout(App\Http\Orchid\Layouts\Repeaters\RepeaterFields::class), ]) ]; }
- 打开您的屏幕,检查此处是否有重复字段
高级使用
重复字段还支持必需、最大和最小参数。您可以通过调用RepeaterField来添加这些参数。
RepeaterField::make('repeater') ->title('Repeater') ->layout(App\Http\Orchid\Layouts\Repeaters\RepeaterFields::class) ->required() ->min(10) ->max(20)
如果将->required()
传递给构造函数,则自动将min
设置为1。如果用户尝试删除此字段,则会阻止并显示消息。
您还可以使用->confirmText('Are you sure that you want to delete the block?')
方法更改删除块时的文本。
要显示保存时的必需消息,您必须在屏幕规则中添加此规则,例如:'content.*.repeater' => 'required'
您还可以使用buttonLabel()
方法重命名按钮标签。例如。
RepeaterField::make('repeater') ->title('Repeater') ->layout(App\Http\Orchid\Layouts\Repeaters\RepeaterFields::class) ->buttonLabel('Add new repeater field')
在极端情况下,如果您正在使用某种动态数据加载,并且需要将额外数据传递到您的布局中,请使用->ajaxData()
方法。此方法既可以与可调用函数一起使用,也可以与数据数组一起使用。这可以在您需要为每个字段过滤数据时非常有用。
要使用ajaxData,首先将trait AjaxDataAccess
连接到您的布局
<?php namespace App\Orchid\Layouts; use Nakukryskin\OrchidRepeaterField\Traits\AjaxDataAccess; use Orchid\Screen\Layouts\Rows; use Orchid\Screen\Fields\Input; use Orchid\Screen\Fields\Select; class RepeaterFields extends Rows { use AjaxDataAccess; public function fields(): array { return [ Select::make('select.') ->title('Select') ->multiple() ->options($this->getSelectOptions()) ->required(), ]; } protected function getSelectOptions() { return $this->getAjaxData()->get('select_options'); } }
然后,在主屏幕上,确定您想要传输的内容。在示例中,我们根据当前用户角色生成列表
Repeater::make('repeater') ->title('Repeater') ->layout(\App\Orchid\Layouts\RepeaterFields::class) ->ajaxData(function () { $data = [ 'select_options' => [ 'default' => 'Default option', ], ]; if (request()->user()->id === 1) { $data['select_options']['only_first'] = 'Option only for user with id #1'; } return $data; }),
您可以在重复字段中组合输出选项、要输出什么以及何时向用户输出。请注意,您的布局不知道当前屏幕上其他字段的任何状态,如果您想要传输有关当前字段的数据,您必须首先保存记录,并使用主屏幕的数据和查询。
Repeater::make('repeater') ->title('Repeater') ->layout(\App\Orchid\Layouts\RepeaterFields::class) ->ajaxData([ 'select_options' => [ 'default' => $this->query()['name'] ?? 'Default Name', ], ]),