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

How to display the componentProperties options by the selected option in the dropdown?

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

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

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

如有任何帮助,我们将不胜感激!

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

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

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

如有任何帮助,我们将不胜感激!

Components/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();
}

}

Components/gallery/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

我自己解决了这个问题,方法是创建一个 returns "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();
    }

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

{{ d(__SELF__.getList) }}

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