简体   繁体   English

如何将 select2 多选与 livewire 一起使用

[英]How to use select2 multiselect with livewire

What I am trying to achieve is, there are two models A and B with a relationship of One to many, where A has many B. So if the records for A and B have already been created, A can be assigned to many B. So I started this current project with livewire, and now I am at a loss.我想要实现的是,有两个模型 A 和 B 具有一对多的关系,其中 A 有很多 B。因此,如果 A 和 B 的记录已经创建,则可以将 A 分配给多个 B。所以我用livewire开始了这个当前的项目,现在我很茫然。 Normal select with multiple works just fine and populates the array in the backend of livewire.正常的 select 与多个作品就好了,并在 livewire 的后端填充数组。 But when I use select2 nothing is stored in the array, and when I dd it, it shows an empty array.但是当我使用 select2 时,数组中没有存储任何内容,当我 dd 时,它显示一个空数组。 Even though livewire provides wonderful set of tools, I am starting to realize that there is a lot of gray areas where things are missing or lacks external support, like select2 here.尽管 livewire 提供了很棒的工具集,但我开始意识到有很多灰色区域缺少一些东西或缺乏外部支持,比如这里的 select2。

Here is what I have done so far:这是我到目前为止所做的:

<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

And the backend:和后端:

<?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){

            }
        }
    }
}

Hopefully this helps.希望这会有所帮助。

######## INSIDE LIVEWIRE COMPONENT ######## 内部 LIVEWIRE 组件

public array $locationUsers = [];
protected $listeners = ['locationUsersSelected'];

public function locationUsersSelected($locationUsersValues)
{
  $this->locationUsers = $locationUsersValues;
}

######## INSIDE LIVEWIRE BLADE ######## 内部带电刀片

<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>

######## INSIDE LIVEWIRE SCRIPTS ######## 活线脚本内

document.addEventListener('livewire:load', function () {
  $('#locationUsers').on('select2:close', (e) => {
    @this.emit('locationUsersSelected', $('#locationUsers').select2('val'));
  });

  $('#locationUsers').val(@this.get('locationUsers')).trigger('change');
});
    <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>

Now JS现在 JS

 $('#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'));
});

This will directly store your selection in variable in livewire and wont get deselected on render.这将直接将您的选择存储在 livewire 中的变量中,并且不会在渲染时取消选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM