10 月 CMS 后端列表的列获取数组键值


October CMS backend list's column get array key value

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

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

你好朋友们,我在十月CMS帮助/支持的帮助下找到了答案http://octobercms.com/index.php/forum/post/dropdown-shows-its-value-than-key-name-in-list-controller并提到了拉拉维尔的几个概念。

模型类方法

public static function getSampleOptions()
{
    return[
      '1'=>'Mobile App',
      '2'=>'Web  App'
      ];
} 

列.Yaml 文件

sample:
    label: Sample Column
    type: dropdown

再次回到模型中,声明属性对象并将归档名称作为空值的键包含

public $attributes = ['sample'=>''];

定义 getfield_nameAttribute() 函数,为列中的相应键设置关联值

public function getSampleAttribute()
{
    $result = $this->attributes['sample'];
    $options = $this->getSampleOptions();
    foreach($options as $key=>$value)
    {
        if($key == $result)
        {
            return $value;
        }
    }
}

更新在编辑记录时纠正问题的解决方案很简单。创建部分并修改字段。亚姆

_sample_options.htm(部分)//文件名应以 with_(下划线)开头

<?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
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 %}