Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/arifurdev/multiselect-inputs-with-livewire-3

This guide explains how to implement multiselect inputs with Livewire 3.
https://github.com/arifurdev/multiselect-inputs-with-livewire-3

laravel-livewire livewire3 multiselect multiselect-drodpdown multiselect-livewire select2

Last synced: 4 days ago
JSON representation

This guide explains how to implement multiselect inputs with Livewire 3.

Awesome Lists containing this project

README

        

# Using Multiselect Inputs with Livewire 3

This guide explains how to implement multiselect inputs with Livewire 3.

## Blade Template
### (resources/views/livewire/your-component.blade.php)

Add the following Blade code to your view. The `wire:ignore` directive is used to prevent Livewire from interfering with the jQuery plugin.

```blade


Select Options:

Volvo
Saab
Fiat
Audi

```
# JavaScript
Include the following script in your Blade template to handle the change event of the multiselect input. This script will update the Livewire component's property **selectedOptions** with the selected values.

```js
@script


$(document).ready(function() {
$('#options').on('change', function(e){
// let selectedValues = e.target.value;
let selectedValues = $(this).val();
$wire.$set('selectedOptions', selectedValues);
});
});


@endscript
```

## component
### Livewire Component (app/Http/Livewire/YourComponent.php)
In your Livewire component class, declare the **selectedOptions** property.
```php
namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Attribute;

class YourComponent extends Component
{
public $selectedOptions = [];

public function render()
{
return view('livewire.your-component');
}
}
```

## Note
Make sure you have jQuery and the select2 library included in your project.

```

```
```

$(document).ready(function() {
$('.js-example-basic-multiple').select2();
});

```