首页 > 解决方案 > 如何通过下拉列表中的选定选项显示 componentProperties 选项?

问题描述

我敢肯定这很简单,但目前证明它有点超出我的能力。

我制作了一个插件,我想用它来显示运行良好的画廊。但是,尝试添加我在组件中创建的画廊的选项被证明是困难的。

当我将组件添加到页面时,我现在可以选择我创建的所有画廊,但根据我选择的画廊来显示画廊是我没有成功的做法。

任何帮助将不胜感激!

我敢肯定这很简单,但目前证明它有点超出我的能力。

我制作了一个插件,我想用它来显示运行良好的画廊。但是,尝试添加我在组件中创建的画廊的选项被证明是困难的。

当我将组件添加到页面时,我现在可以选择我创建的所有画廊,但根据我选择的画廊来显示画廊是我没有成功的做法。

任何帮助将不胜感激!

组件/Gallery.php:

use Cms\Classes\ComponentBase;
use MartinSmith\Gallerys\Models\Gallery as GalleryModel;

class gallerys extends ComponentBase
{
public $gallery;

public function componentDetails(){
    return [
        'name' => 'Frontend Gallery',
        'description' => 'A gallery for you webpage'
    ];
}

public function defineProperties() {
    $lists = $this->getLists();
    return [
        'galleryName' => [
            'title' => 'Gallery',
            'type' => 'dropdown',
            'placeholder' => 'Select Gallery',
            'options' => $lists
        ]
    ];
}

public function getLists() {
    $agreements = GalleryModel::all()->pluck('name', 'id');
    return $agreements->toArray();
}

public function getList() {
    $agreement = GalleryModel::where('id', $this->property('galleryName'))->get();
    return $agreement->first();
}

}

组件/图库/default.htm:

{% set gallerys = __SELF__.gallery %}

{% for gallery in gallerys %}

<div class="container-fluid px-0">

    <div class="gallery">

        <div class="row">

            {% for image in gallery.fullImage %}

            <div class="col-md-4 px-0 home-galleryImg">

                <a href="{{ image.path }}">

                    <div class="gallery-imgOverlay">
                        <p>{{ image.title }}</p>
                        <h5>{{ image.description }}</h5>
                    </div>

                    <img class="img-fluid" src="{{ image.thumb(650,auto) }}" alt="{{ thumbnail.description }}">

                </a>

            </div>

            {% endfor %}

        </div>

    </div>

</div>

{% endfor %}

看截图

标签: pluginscomponentsoctobercms

解决方案


我通过使用 laravel pluck 方法创建一个返回“名称”并由“id”索引的函数为自己解决了这个问题。pluck('name', 'id')第一个参数选择要用作值的列,第二个参数选择要用作键的列。注意*toArray()我不认为选项字段可以收集的方法。

public function getLists() {
    $agreements = Agreements::all()->pluck('agrnum', 'id');
    return $agreements->toArray();
}
//returns
array:3 [▼
  2 => "DLE-2"
  4 => "DLE-1"
  5 => "DLE-3"
]

现在在我的属性区域中,我调用了该函数$list = $this->getList();

public function defineProperties() {
$lists = $this->getLists();
return [
    'getList' => [
        'title' => 'List',
        'type' => 'dropdown',
        'placeholder' => 'Select List',
        'options'     => $lists
       ]
    ];
}

之后,您可以继续Lists::where('id', $this->property('getList'));在函数中执行某项或某项操作以显示所选列表或在您的案例库中。

我的结果: 来自组件的 CMS 页面后端

    public function defineProperties() {
    $lists = $this->getLists();
    return [
        'getList' => [
            'title' => 'List',
            'type' => 'dropdown',
            'placeholder' => 'Select List',
            'options'     => $lists
           ]
        ];
    }

    public function getLists() {
    $agreements = Agreements::all()->pluck('agrnum', 'id');
    return $agreements->toArray();
    }

    public function getList() {
        $agreement = Agreements::where('id', $this->property('getList'))->get();
        return $agreement->first();
    }

在此处输入图像描述

来自组件模板文件夹中 default.htm 的网页

{{ d(__SELF__.getList) }}

在此处输入图像描述

另外,如果我这样做{{ d(__SELF__.property('getList')) }},它会告诉我价值是"5".


推荐阅读