如何将 select2 多选与 livewire 一起使用
Posted
技术标签:
【中文标题】如何将 select2 多选与 livewire 一起使用【英文标题】:How to use select2 multiselect with livewire 【发布时间】:2021-06-07 06:54:40 【问题描述】:我想要实现的是,有两个模型 A 和 B 具有一对多的关系,其中 A 有很多 B。因此,如果 A 和 B 的记录已经创建,则可以将 A 分配给许多B。所以我用livewire开始了这个当前的项目,现在我很茫然。具有多个工作的正常选择就可以了,并在 livewire 的后端填充数组。但是当我使用 select2 时,数组中没有存储任何内容,当我 dd 时,它显示一个空数组。 尽管 livewire 提供了很棒的工具集,但我开始意识到有很多灰色区域缺少一些东西或缺乏外部支持,比如这里的 select2。
这是我到目前为止所做的:
<div>
<div class="tab-pane fade show active" id="card1" role="tabpanel" aria-labelledby="card1-tab">
<div class="pt-3"></div>
<div class="row" >
<div class="form-group col">
<label for="cards">Enter Cards</label>
<select class="form-control select2" id="cards" multiple="multiple" onchange="this.dispatchEvent(new InputEvent('input'))>
<option disabled="">Select Cards</option>
@foreach($cardsUnassigned as $card)
<option value=" $card->id "> $card->imei_number </option>
@endforeach
</select>
</div>
<div class="form-group col">
<label for="technicians" class="">Technicians</label>
<select class="form-control select2" id="technicians" wire:model='techies.'>
<option disabled="">Select Technicians</option>
@foreach($technicians as $tech)
<option value=" $tech->id "> $tech->first_name </option>
@endforeach
</select>
</div>
</div>
@foreach($cardSelect as $key => $value)
$value
@endforeach
<button wire:click="assignCardToTech()" class="btn btn-primary">Submit</button>
</div>
</div>
@push('scripts')
<script>
$('#cards').select2(
placeholder: 'Select Cards to assign'
);
//send data to livewire
$('#technicians').select2(
placeholder: 'Select Technicians'
);
</script>
@endpush
还有后端:
<?php
namespace App\Http\Livewire\Stock\Assigns;
use App\Models\Card;
use App\Models\Sim;
use App\Models\Technician;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Support\Facades\Auth;
class Assigns extends Component
public $sortBy_card = 'imei_number';
public $sortBy_sim = 'ip';
public $sortDirection = 'asc';
public $search = '';
public $perPage = 10;
public $cardSelect = [];
public $techies;
public function render()
$simsUnassigned = Sim::query()
->whereNull('technician_id')
->searchUnassigned($this->search)
->get();
$cardsUnassigned = Card::query()
->whereNull('technician_id')
->searchUnassignedCard($this->search)
->get();
// dd($cardsUnassigned);
$simsAssigned = Sim::query()
->searchAssigned($this->search)
->orderBy($this->sortBy_sim, $this->sortDirection)
->paginate($this->perPage);
$cardsAssigned = Card::query()
->searchAssignedCard($this->search)
->orderBy($this->sortBy_card, $this->sortDirection)
->paginate($this->perPage);
$technicians = Technician::query()->get();
return view('livewire.stock.assigns.assigns',compact('simsUnassigned','cardsUnassigned','simsAssigned','cardsAssigned','technicians'))
->extends('layouts.base')
->section('content');
public function assignCardToTech()
dd($this->cardSelect);
if($this->cards)
foreach($this->cards as $card)
希望这会有所帮助。
【问题讨论】:
【参考方案1】:######## 在 LIVEWIRE 组件内
public array $locationUsers = [];
protected $listeners = ['locationUsersSelected'];
public function locationUsersSelected($locationUsersValues)
$this->locationUsers = $locationUsersValues;
######## 活线刀片内部
<div class="col-md-12 mb-3" wire:ignore>
<label for="locationUsers">Select Users</label>
<select id="locationUsers" class="form-control select2" multiple="multiple">
<option value="">--select--</option>
@foreach($this->users as $id => $name)
<option value=" $id "> $name </option>
@endforeach
</select>
</div>
######## 现场直播脚本
document.addEventListener('livewire:load', function ()
$('#locationUsers').on('select2:close', (e) =>
@this.emit('locationUsersSelected', $('#locationUsers').select2('val'));
);
$('#locationUsers').val(@this.get('locationUsers')).trigger('change');
);
【讨论】:
已经有一段时间了,我会测试一下并告诉你。感谢您提供此代码!【参考方案2】:您可以尝试将其添加到您的 html 中
<div class="form-group col-md-6" wire:ignore>
<label for="manager" class="mb-0 h5">Assign Managers:</label>
<select wire:model="reporting_managers" id="manager" class="select-2 multiple='multiple' data-placeholder="Assign Managers">
@foreach ($managers as $manager)
<option value=" $manager->id " data-name=" $manager->name "> $manager->name </option>
@endforeach
</select>
</div>
然后在您的 javascript 部分中
$('#manager').on('select2:select', function (e)
@this.set('reporting_managers', $('#manager').select2('val'));
);
$('#manager').on('select2:unselect', function (e)
@this.set('reporting_managers', $('#manager').select2('val'));
);
这会将您的选择直接存储在 livewire 中的变量中,并且不会在渲染时取消选择。
【讨论】:
以上是关于如何将 select2 多选与 livewire 一起使用的主要内容,如果未能解决你的问题,请参考以下文章
Laravel Livewire 上的 select2 不起作用
如何使用 Select2.js 对从 Ajax 调用返回的数据进行多选结果?