通常, 您可能会遇到options, optionsMethod 或 optionsPreset 属性, 本文将更详细地描述如何配置选项.
options 属性应将选项作为定义的一部分,以键值对的形式直接指定,其中值和标签是独立指定的。
options:
draft: Draft
published: Published
archived: Archived键可以是带有标签的整数。
options:
1: Simple
2: Complex除了简单的数组,一些字段,例如单选列表,支持在其options值中指定描述。在这种情况下,值被定义为另一个数组,其语法为键: [标签, 描述]。
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',
],
// ...
]
],
];
}