如何通过下拉列表中的选定选项显示componentProperties选项?

问题描述 投票:0回答:1

我确信这很简单,但现在证明它有点超出我的范围。

我已经制作了一个插件,我想用它来显示工作正常的画廊。但是,尝试添加我在组件中创建的库的选项证明是困难的。

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

任何帮助将不胜感激!

我确信这很简单,但现在证明它有点超出我的范围。

我已经制作了一个插件,我想用它来显示工作正常的画廊。但是,尝试添加我在组件中创建的库的选项证明是困难的。

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

任何帮助将不胜感激!

组件/ 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 %}

See screenshot

plugins components octobercms
1个回答
0
投票

我通过创建一个函数来解决这个问题,该函数返回“name”并使用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();
    }

enter image description here

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

{{ d(__SELF__.getList) }}

enter image description here

此外,如果我做{{ d(__SELF__.property('getList')) }}它显示我的值是"5"

© www.soinside.com 2019 - 2024. All rights reserved.