【问题标题】:How to display the componentProperties options by the selected option in the dropdown?如何通过下拉列表中的选定选项显示 componentProperties 选项?
【发布时间】:2019-09-13 02:04:48
【问题描述】:

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

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

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

任何帮助将不胜感激!

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

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

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

任何帮助将不胜感激!

组件/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】:

    我自己解决了这个问题,方法是创建一个返回“名称”并使用 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-&gt;getList();

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

    之后,您可以继续在函数中执行 Lists::where('id', $this-&gt;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"

    【讨论】:

    • 感谢您的建议。我已经尝试了您的建议,但最终出现以下错误:October\Rain\Database\Builder 类的对象无法转换为字符串。有什么想法吗?
    • @MartinJuniorS 您能否按照我的建议发布修改后的代码。你在哪里得到错误?在后端查看 CMS 页面还是在前端查看网页本身?我修改了我的答案,以向您展示更多我的样子。在你的 Component.php 中不要害怕用 dd(); . die 和 dump ( dd() ) 可用于跟踪 php 执行错误发生的位置。
    • 好的,我已经更新了代码,您应该看到它与您发送过来的代码有些相同。我将变量和函数名称保持不变以避免混淆。正如您从屏幕截图中看到的那样,+attributes 现在确实显示了正确的画廊,当我将组件更改为“测试 2”时,它也会更新,但画廊没有被输出。
    • 哦,另外,当我执行 {{ d(SELF.property('galleryName')) }} 时,我得到的值为 18
    • 如果你想获得一个特定的画廊并在你的组件 default.htm 中使用组件更改 {% set gallerys = __SELF__.gallery %}{% set gallerys = __SELF__.getList %} 来指定它。
    猜你喜欢
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多