Get 2024 Templates Mega Bundle!14 Bootstrap, Vue & React Templates + 3 Vector Sets
Get for 99$

SELECT2 Livewire



<select class="form-select form-select-solid" data-control="select2"
data-close-on-select="false"
data-placeholder="Select an option"
data-allow-clear="true"
data-hide-search="true"
wire:model="lead"
>
@foreach($this->users as $user)
<option value="{{ $user->name }}">{{ $user->name }}</option>
@endforeach
</select>



I'm having this issue with Select2 where the selected value is not being sent to the attribute in the component, even though I have the `wire:model` set. How can I make it work?


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (1)


hi Alex,

In Livewire, using wire:model with Select2 doesn't work out-of-the-box due to the way Select2 initializes. It creates a separate element for the Select2 UI, while hiding the original select dropdown. However, you can implement a workaround to ensure the change event is passed to the Livewire model variable. Here's how to do it:


<select class="form-select form-select-solid" data-control="select2"
data-close-on-select="false"
data-placeholder="Select an option"
data-allow-clear="true"
data-hide-search="true"
>
@foreach($this->users as $user)
<option value="{{ $user->id }}">{{ $user->name }}</option>
@endforeach
</select>

@script
<script>
$("#select2_item").on("change", function(e) {
var data = $(this).select2("data");
Livewire.emit("change.item", data);
});
</script>
@endscript


In your Livewire component:

protected $listeners = [
"change.item" => "updateItem"
];

public $lead;

public function updateItem($property)
{
$this->lead = $property["id"];
}


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(