laravel - 如何使用livewire alpinejs Tom select动态添加多选?
问题描述
每个人。我在开发 laravel livewire alpinejs Tom-select / select2 的过程中遇到了一个问题。默认显示一行没有问题。
当我动态添加行数时,select无法正常显示和渲染。我应该怎么办?
这是我的代码:</p>
应用程序\http\livewire\编辑器
public $inputs = [0];
public $i = 1;
protected $listeners = [
'add',
'refresh'=> '$refresh'
];
public function add($i)
{
$i = $i + 1;
$this->i = $i;
array_push($this->inputs ,$i);
$this->emit('addFormRow');
}
public function remove($i)
{
unset($this->inputs[$i]);
}
public function render()
{
return view('livewire.editor');
}
livewire/editor.balde.php
@foreach ($inputs as $key => $value)
<tr>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
{{ $key + 1 }}
</td>
<td class="px-6 py-2 whitespace-nowrap text-sm font-medium text-gray-900">
<select data-placeholder="Select your favorite actors" class="tom-select w-full">
<option value="1" selected>Leonardo DiCaprio</option>
<option value="2">Johnny Deep</option>
<option value="3" selected>Robert Downey, Jr</option>
<option value="4">Samuel L. Jackson</option>
<option value="5">Morgan Freeman</option>
</select>
</td>
<td class="px-6 py-2 whitespace-nowrap text-sm text-gray-500">
<select data-placeholder="Select your favorite actors" class="tom-select w-full" multiple>
<option value="1" selected>Leonardo DiCaprio</option>
<option value="2">Johnny Deep</option>
<option value="3" selected>Robert Downey, Jr</option>
<option value="4">Samuel L. Jackson</option>
<option value="5">Morgan Freeman</option>
</select>
</td>
<td class="px-6 py-2 whitespace-nowrap text-sm text-gray-500">
<button wire:click.prevent="remove({{ $key }})" type="button"
class="inline-flex items-center p-1.5 border border-transparent rounded-full shadow-sm text-white bg-red-600 hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24"
stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</td>
</tr>
@endforeach
like-tome-select.js
import TomSelect from "tom-select";
(function (cash) {
"use strict";
// Tom Select
cash(".tom-select").each(function () {
let options = {
plugins: {
dropdown_input: {},
},
};
if (cash(this).data("placeholder")) {
options.placeholder = cash(this).data("placeholder");
}
if (cash(this).attr("multiple") !== undefined) {
options = {
...options,
plugins: {
...options.plugins,
remove_button: {
title: "Remove this item",
},
},
persist: false,
create: true,
onDelete: function (values) {
return confirm(
values.length > 1
? "Are you sure you want to remove these " +
values.length +
" items?"
: 'Are you sure you want to remove "' +
values[0] +
'"?'
);
},
};
}
if (cash(this).data("header")) {
options = {
...options,
plugins: {
...options.plugins,
dropdown_header: {
title: cash(this).data("header"),
},
},
};
}
new TomSelect(this, options);
});
})(cash);
如果tom-select不能实现,可以用select2来实现吗?谢谢你。
解决方案
推荐阅读
- git - 拆分 git commit 以满足 git push 限制
- production-environment - 使用 ZEIT Now 部署到生产环境时,.env 中的变量为空
- relationship - Laravel Eloquent 查询与关系
- c++ - 从 uint8 到 hex char 的最快转换
- javascript - 你如何编写一个可以在不修改原始函数的情况下扩展的 JavaScript 函数
- java - 无法在 Apache Tomcat WebServer 中访问该站点
- reactjs - React Hooks 和 Firestore -> 无法在未安装的组件上执行 React 状态更新
- python - Django 查询扩展用户模型进行身份验证
- arrays - 为什么我的 python 代码不删除所有等于目标的元素?
- c++14 - 在单独的类方法中使用它会使程序崩溃