Answers for "select2 with livewire"

0

laravel livewire select2 multi select

######## INSIDE LIVEWIRE COMPONENT
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:select', (e) => {
    @this.emit('locationUsersSelected', $('#locationUsers').select2('val'));
  });

  $('#locationUsers').val(@this.get('locationUsers')).trigger('change');
});
Posted by: Guest on May-17-2021
1

select2 with livewire

<div class="form-group col-md-3">
              <label class="text-sm"for="division_id">Division</label>
                <select  class="form-control select2 {{ $errors->has('division_id') ? 'is-invalid' : '' }}"
                    wire:model="division_id" id="division_id"  >
                    <option value=''>{{ trans('global.pleaseSelect') }}</option>
                    @foreach($divisions as $id =>  $division) 
                            <option value="{{ $id }}"  {{ old('division_id') == $id ? 'selected' : '' }}> {{ $division->division }} </option>  
                    @endforeach
             </select> 
         </div>


<script>
   document.addEventListener('livewire:load', function () {
    
        $('#division_id').on('select2:select', function (e) {
     
            @this.set('division_id', e.target.value);
        });
        
     });  
</script>
Posted by: Guest on August-18-2021

Browse Popular Code Answers by Language