您好朋友,我在 10 月 CMS 帮助/支持
http://octobercms.com/index.php/forum/post/dropdown-shows-its-value-than-key-name-in-list-controller的帮助下找到了答案
并提到了一些 laravel 的概念。
模型类方法
public static function getSampleOptions()
{
return[
'1'=>'Mobile App',
'2'=>'Web App'
];
}
列.Yaml 文件
sample:
label: Sample Column
type: dropdown
再次回到模型中,声明属性对象并将文件名包含为具有空值的键
public $attributes = ['sample'=>''];
定义 get field_name Attribute() 函数为列中的相应键设置关联值
public function getSampleAttribute()
{
$result = $this->attributes['sample'];
$options = $this->getSampleOptions();
foreach($options as $key=>$value)
{
if($key == $result)
{
return $value;
}
}
}
更新在编辑记录时纠正问题的解决方案很简单。创建部分并修改字段。yaml
_sample_options.htm (partial) // 文件名应该以_(下划线)开头
<?php
$fieldOptions = $model->getSampleOptions();
$sample = $model->attributes['sample'];
?>
<select id="<?= $field->getId() ?>" name="<?= $field->getName() ?>" class="form-control custom-select" <?= $field->getAttributes() ?>>
<?php foreach($fieldOptions as $key=>$label)
{
?>
<option value="<?= $key ?>" <?php echo ($sample == $key)?"selected":''; ?>><?= $label ?></option>
<?php
} ?>
</select>
这里的$model和$field是用于访问预期模型的方法和属性的部分变量。文档:https ://octobercms.com/docs/backend/forms#field-partial
字段.Yaml 文件
sample:
label: Sample Field
type: partial
path: $/october/demo/controllers/sample/_sample_options.htm //path where the partial is located in the controller view