[英]Using Livewire with multi Select2 multiple input
in one page I have to list the name of the all rooms in a loop and assign employees to the rooms.在一页中,我必须循环列出所有房间的名称并将员工分配到房间。 Some employees uses more than one room.
一些员工使用多个房间。 I decided use Livewire for the first time.
我决定第一次使用 Livewire。 So I don't have experience with Livewire.
所以我没有使用 Livewire 的经验。 I'm using Select2 to choose employees.
我正在使用 Select2 选择员工。
My structure is this:我的结构是这样的:
Livewire View Livewire 视图
<div>
@foreach(\App\Models\Room::all() as $room)
<div class="row">
<div class="col-2">
<div class="fw-bold">{{$room->room_code}}</div>
<div>
<p>{{$room->name}}</p>
</div>
</div>
<div class="col-8">
<div class="row">
<div class="col-10">
<select class="multiple-select" wire:model="employee.employee" data-placeholder="Choose employee" multiple="multiple">
@foreach(\App\Models\Employee::where('status', 1)->get() as $employee)
<option value="{{$employee->id}}">{{$employee->first_name." ".$employee->last_name}}</option>
@endforeach
</select>
</div>
<div class="col-1">
<button class="btn btn-danger" wire:click="assignSave({{$room->id}})"><i class="fa-solid fa-floppy-disk icon-center"></i></button>
</div>
<div class="col-1 text-success font-22">
<i class="fa-solid fa-check icon-center"></i>
</div>
</div>
</div>
</div>
@endforeach
</div>
Livewire Controller Livewire 控制器
<?php
namespace App\Http\Livewire;
use Livewire\Component;
class RoomAssign extends Component
{
public $employee = [];
public function render()
{
return view('livewire.room-assign');
}
public function assignSave($room){
dd($this->employee);
}
}
This is the blade这是刀片
@section('sub-page-content')
<h6 class="mb-0 text-uppercase">Room Assign</h6>
<hr/>
<div class="card">
<div class="card-body">
<livewire:room-assign />
</div>
</div>
@endsection
@section('customjs')
<script src="{{asset('assets/plugins/select2/js/select24.min.js')}}"></script>
<script src="{{asset('assets/js/form-select2.js')}}"></script>
<script>
$('.multiple-select').on('change', function (){
alert('asdasd');
})
</script>
@endsection
Idea is simple.想法很简单。 Take room id and employee id and save to a pivot table both information.
获取房间 ID 和员工 ID,并将这两个信息保存到数据透视表中。 But I can't evet take employees array.
但我不能 evet 员工阵列。 In every loop I have a save button for that room to save records and I want to inform user is process is successful.
在每个循环中,我都有那个房间的保存按钮来保存记录,我想通知用户过程是否成功。 For information I left a div to show a simple "green tick".
有关信息,我留下了一个 div 来显示一个简单的“绿色勾号”。 Can you help me about taking the employee ids and notify the user?
你能帮我获取员工 ID 并通知用户吗?
Need to add wire:ignore
for select2.需要为 select2 添加
wire:ignore
。 I would also change the $employee
variable in your foreach
since you're using it in your Livewire component as well and is more than likely causing a conflict.我还会更改您的
foreach
中的$employee
变量,因为您也在 Livewire 组件中使用它并且很可能导致冲突。 Use something like $employeeInfo
instead for the foreach
对
foreach
使用类似$employeeInfo
的东西
<div class="col-10" wire:ignore>
<select class="multiple-select" wire:model="employee" id="employee" data-placeholder="Choose employee" multiple="multiple">
@foreach(\App\Models\Employee::where('status', 1)->get() as $employeeInfo)
<option value="{{$employeeInfo->id}}">{{$employeeInfo->first_name." ".$employeeInfo->last_name}}</option>
@endforeach
</select>
</div>
When using Livewire with select2 you need to setup a listener for the select将 Livewire 与 select2 一起使用时,您需要为选择设置一个侦听器
public $employee = [];
protected $listeners = [
'employee',
];
public function employee($value)
{
$this->employee = $value;
}
And in your script tag:在你的脚本标签中:
$(document).ready(function() {
window.initSelectDrop=()=>{
$('#employee').select2({
placeholder: '{{ __('locale.Select ....') }}',
allowClear: true});
}
initSelectDrop();
$('#employee').on('change', function (e) {
livewire.emit('employee', e.target.value)
});
});
I had this same issue, when Livewire re-renders the component it will break select2 drop downs.我遇到了同样的问题,当 Livewire 重新渲染组件时,它会破坏 select2 下拉菜单。 Using this approach select2 will act as expected.
使用这种方法 select2 将按预期运行。
public function storeRoomEmployees(Room $room){
$room->employees()->attach($this->employees)
}
/* some suggestions on Your code
1. instead of this @foreach(\App\Models\Room::all() as $room) in your
blade, define a variable in Livewire Controller public $rooms.
2. add mount function
public function mount(){
$this->rooms = Room::all();
}
// mount function runs before the content load
@foreach(\App\Models\Room::all() as $room) -> @foreach($rooms as $room)
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.