plugins - 如何通过下拉列表中的选定选项显示 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 %}
解决方案
我通过使用 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"
.
推荐阅读
- github-actions - 如何使用 GitHub Actions 维护 Bazel 缓存?
- javascript - PHP CKFinder 应用程序:开始过滤文本更改
- debian - apt update --allow-releaseinfo-change 在不同的 Debian 版本上
- git - 如果我使用 git pull (merge) 并将我的提交压缩到 master 会发生什么?
- gerrit - 在更改中设置状态字段
- python - Understanding Intermediate Values and Pruning in Optuna
- java - 具有每个请求配额的线程池执行器?
- audio - 有没有办法在不增加延迟的情况下同步视频和音频?
- r - 不使用分隔符分隔列
- python - 漏洞?在 M1 - 为每个样本分别计算梯度时优化的 TensorFlow