repeater - 渲染一组重复的表单字段,使用关联记录或 可JSON化属性。
extra_information:
type: repeater
form:
fields:
added_at:
label: Date Added
type: datepicker
details:
label: Details
type: textarea以下 字段属性 受支持且常用。
| Property | Description |
|---|---|
| label | a name when displaying the form field to the user. |
| default | specifies a default array value, optional. |
| comment | places a descriptive comment below the field. |
| form | inline field definitions or a reference to form field definition file. |
| prompt | text to display for the create button. Default: Add new item. |
| displayMode | controls how the interface is displayed, as either accordion or builder. Default: accordion |
| useTabs | shows tabs when enabled, allowing fields to specify a tab property. Default false |
| itemsExpanded | if repeater items should be expanded by default when using accordion mode. Default: true. |
| titleFrom | name of field within items to use as the title for the collapsed item, optional. |
| minItems | minimum items required. Pre-displays those items when not using groups. For example if you set minItems: 1 the first row will be displayed and not hidden. |
| maxItems | maximum number of items to allow within the repeater. |
| groups | references a group of form fields placing the repeater in group mode (see below). An inline definition can also be used. |
| groupKeyFrom | the group key attribute stored along with the saved data. Default: _group |
| showReorder | displays an interface for sorting items. Default: true |
| showDuplicate | displays an interface for cloning items. Default: true |
titleFrom 属性可用于指定重复器折叠时使用的值。
extra_information:
type: repeater
titleFrom: title_when_collapsed
form:
fields:
# ...
title_when_collapsed:
label: This field is the title when collapsed
type: text复用器字段通过将 useTabs 属性设置为 true 来支持使用标签页。
extra_information:
type: repeater
useTabs: true
form:
added_at:
label: Date added
type: datepicker
tab: Date
details:
label: Details
type: textarea
tab: Details重复字段支持使用 groups 的分组模式允许为每次迭代选择一组自定义字段。
content:
type: repeater
prompt: Add content block
groups: $/acme/blog/config/fields_repeater.yaml这是一个组配置文件示例,它将位于 /plugins/acme/blog/config/fields_repeater.yaml。为了更好地组织,groups 可以为每个组定义指定一个文件。
groups:
textarea: $/acme/blog/config/fields_textarea.yaml
quote: $/acme/blog/config/fields_quote.yaml或者,这些定义可以与中继器内联指定。如果组键以下划线 (_) 开头,那么它将被忽略。
groups:
textarea:
name: Textarea
description: Basic text field
icon: icon-file-text-o
fields:
text_area:
label: Text Content
type: textarea
size: large
quote:
name: Quote
description: Quote item
icon: icon-quote-right
fields:
quote_position:
span: auto
label: Quote Position
type: radio
options:
left: Left
center: Center
right: Right
quote_content:
span: auto
label: Details
type: textarea每个组必须指定一个唯一的键,并且该定义支持以下选项。
| Option | Description |
|---|---|
| name | the name of the group. |
| description | a brief description of the group. |
| icon | defines an icon for the group, optional. |
| titleFrom | name of a field for the item title, optional. |
| fields | form fields belonging to the group. |
| useTabs | shows tabs for the group only, optional. |
组键与已保存的数据一同存储,作为
_group属性。这可以通过groupKeyFrom选项进行自定义。
中继器表单小部件将自动检测模型属性是否是关联字段并使用它。以下提供一个示例实现,供您使用。例如,如果您的模型使用 hasMany 关系,它引用一个 RepeaterItem 模型,中继器将为每个项使用此关联模型。
public $hasMany = [
'extra_information' => [
RepeaterItem::class,
'key' => 'parent_id',
'delete' => true
],
];一个简单的数据库表结构可以为该模型定义,其中包含对父模型 id 的引用以及用于动态属性的序列化 JSON value(见下文)。
Schema::create('acme_blog_repeater_items', function($table) {
$table->increments('id');
$table->integer('parent_id')->unsigned()->nullable()->index();
$table->mediumText('value')->nullable();
$table->integer('sort_order')->nullable();
$table->timestamps();
});该模型扩展了 October\Rain\Database\ExpandoModel 基类以支持在模型上设置的动态属性,并以 JSON 格式保存到数据库中。该模型可以包含附件以及任何其他相关字段。
use October\Rain\Database\ExpandoModel;
class RepeaterItem extends ExpandoModel
{
use \October\Rain\Database\Traits\Sortable;
public $table = 'acme_blog_repeater_items';
protected $expandoPassthru = ['parent_id', 'sort_order'];
public $attachMany = [
'photos' => \System\Models\File::class,
];
}最后,中继器项可以指定为一个表单字段,附带表单字段定义,包括使用模型关系的字段。
extra_information:
type: repeater
form:
fields:
title:
label: title
is_enabled:
label: Enabled
type: switch
photos:
label: Photos
type: fileupload
mode: image