通常,您可能会遇到 options,optionsMethod 或 optionsPreset 属性,本文将更详细地介绍如何配置选项。
options 属性应直接指定选项作为定义的一部分,以键值对的形式,其中值和标签是独立指定的。
options:
draft: Draft
published: Published
archived: Archived键可以是带有其标签的整数。
options:
1: Simple
2: Complex除了简单的数组,一些字段例如单选列表支持指定描述作为其 options 值的一部分。 此时值被定义为另一个采用 key: [label, description] 语法的数组。
options:
all: [All, Guests and customers will be able to access this page.]
registered: [Registered only, Only logged in member will be able to access this page.]
guests: [Guests only, Only guest users will be able to access this page.]其他字段,例如 下拉字段 支持指定图标、图片或颜色作为其 options 值的一部分。如果数组中的第二项以 # 开头,则它被视为颜色,并且如果该值包含 .,则它被视为图片,否则它被视为图标类。
options:
red: [Color, '#ff0000']
icon: [Icon, 'oc-icon-calendar']
image: [Image, '/path/to/image.png']optionsPreset 属性指定一个预设代码,该代码可用于请求可用选项。
optionsPreset: icons以下预设可用:
| Preset | Description |
|---|---|
| icons | Lists available icon names (eg: icon-calendar) |
| phosphorIcons | Lists available icon names (eg: ph ph-calendar) |
| locales | Lists available locales (eg: en-au) |
| flags | Lists locales with their icons as flags (eg: [en-au, flag-au]) |
| timezones | Lists available timezones (eg: Australia/Sydney) |
optionsMethod 属性指定一个可用于请求可用选项的可调用 PHP 方法。通常,该方法名称将指向关联模型中的一个本地方法。
optionsMethod: getMyOptionsFromModel方法名也可以是任何对象上的静态方法。
optionsMethod: MyAuthor\MyPlugin\Helpers\FormHelper::getMyStaticMethodOptions在方法内部, 可以使用详细定义来指定更高级的选项, 例如为每个选项设置单独的属性. 详细定义通过其关联的数组结构来识别.
public function getDetailedFieldOptions()
{
return [
1 => [
'label' => 'Option 1',
'comment' => 'This is option one'
],
2 => [
'label' => 'Option 2',
'comment' => 'This is option two',
'disabled' => true
]
];
}以下属性在可能的情况下受支持:
| Property | Description |
|---|---|
| label | a name when displaying the option to the user. |
| comment | places a descriptive comment below the option label. |
| readOnly | specifies if the option is read-only or not. |
| disabled | specifies if the option is disabled or not. |
| hidden | defines the option without ever displaying it. |
| color | defines a status indicator color for the option as a hex color (dropdown) |
| icon | specifies an icon name for this option (dropdown) |
| image | specifies an image URL for this option (dropdown) |
| optgroup | set to true if the children should belong to an option group structure, default: false (dropdown) |
| children | specifies child options as another array for a nested structure (checkbox list) |
使用 children 属性如果选项定义支持嵌套。通常,这将为复选框列表显示一个结构并为下拉菜单实现一个选项组。
public function getDetailedFieldOptions()
{
return [
1 => [
'label' => 'Option 1',
'comment' => 'This is option one',
'children' => [
2 => [
'label' => 'Option 2',
'comment' => 'This is option two',
],
// ...
]
],
];
}