jeissonp/orchid-repeater-field

为Orchid RAD平台添加重复字段支持。

1.1.1 2024-05-02 21:55 UTC

This package is auto-updated.

Last update: 2024-10-02 22:47:33 UTC


README

Support Ukraine Badge

Orchid Platform Repeater Field

简介

我们都曾在某个时候使用过WordPress。受Advanced Custom Fields的启发。

此包为Orchid RAD平台添加重复字段支持。

先决条件

您必须已安装并配置Orchid平台

版本支持: .*

对于 ^13.0.1 使用^13.0.0 标签及更高版本。

旧版本不支持大多数当前功能。

对于 6.* 版本使用2.0.5 标签

对于 5 版本使用1.0.0 标签

对于 4.7.1 版本使用0.0.8 标签

如何使用

  1. 使用composer安装包:composer require nakukryskin/orchid-repeater-field

    最新的Laravel版本将自动发现并使用该包。

  2. 在您的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()
         ];
     }
 }
  1. 简单地从您的屏幕中开始添加RepeaterField::make('repeater'):例如
public function layout(): array
{
   return [
       Layout::rows([
           RepeaterField::make('repeater')
               ->title('Repeater')
               ->layout(App\Http\Orchid\Layouts\Repeaters\RepeaterFields::class),
       ])
   ];
}
  1. 打开您的屏幕并检查此处是否有重复字段

高级用法

重复字段还支持必需、最大和最小参数。您可以通过调用重复字段来添加这些参数。

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;
      }),

Dynamic ajax data with anonymous function

您可以在重复字段中组合输出选项以及向用户输出什么和何时输出。请注意,您的布局不知道当前屏幕上其他字段的任何状态,并且如果您想传输有关当前字段的数据,您必须首先保存记录并使用主屏幕的数据和查询。

Repeater::make('repeater')
         ->title('Repeater')
         ->layout(\App\Orchid\Layouts\RepeaterFields::class)
         ->ajaxData([
             'select_options' => [
                 'default' => $this->query()['name'] ?? 'Default Name',
             ],
         ]),

Using other field from query to