2

嗨,我是十月 cms 的新手。我在我的模型类中定义了下面显示的方法。该方法还用于在后端表单中显示选择选项。此方法返回一个数组,其中键是类似于 db 中的字段值的值。我已将该方法定义为静态方法,因为它也建议在前端获取函数并使用 db 记录对其进行处理并对其进行迭代以显示与键匹配的数组的值。一切正常。事情在我的 columns.yaml 文件中,我如何列出与 db 记录匹配的方法的数组值,就像我在前端所做的那样。

public static function getSampleOptions()
{
    return[
          '1'=>'Sample1',
          '2'=>'Sample2'
          ];
}
4

2 回答 2

3

您好朋友,我在 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
于 2016-03-18T05:27:08.620 回答
0
break a for loop in october cms template if condition not satisfied?

{% for key, pTest in pack.products %}
                                {{loop.length}}
                                <li>{{pTest.productable.name}} {{ key }}</li>
                                {% if key == 2 %}
                                <li class="more">...</li>
                                {% endif %}
                                {% endfor %}
于 2016-03-21T05:38:15.607 回答