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

select2 livewire



<div class="fv-row mb-7" wire:ignore>
<!--begin::Label-->
<label class="required fw-semibold fs-6 mb-2">Category</label>
<!--end::Label-->
<!--begin::Input-->
<select name="category_id" aria-label="Select a Category" data-control="select2" wire:model.defer="category_id"
class="form-select form-select-solid fw-bold category_id">
<option value="">Select a Category...</option>
@foreach($this->categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
@endforeach
</select>

<!--end::Input-->

</div>
<div class="fv-row mb-7" wire:ignore>
<!--begin::Label-->
<label class="required fw-semibold fs-6 mb-2">Sub-Category</label>
<!--end::Label-->
<!--begin::Input-->
<select name="child_category_id" aria-label="Select a Sub-Category" data-control="select2" wire:model.defer="child_category_id"
class="form-select form-select-solid fw-bold child_category_id">
<option value="">Select a Sub-Category...</option>
@foreach($sub_categories as $sub)
<option value="{{$sub->id}}">{{$sub->name}}</option>
@endforeach
</select>

<!--end::Input-->

</div>
@push("scripts")
<script>
$(document).ready(function () {

var $selectElementCategory = $(".category_id");

$selectElementCategory.on("change", function (e) {

var data_category = $selectElementCategory.val();

@this.set("category_id", data_category);
});

var $selectElement = $(".child_category_id");

$selectElement.on("change", function (e) {

var data = $selectElement.val();

@this.set("child_category_id", data);
});
})

</script>


@endpush

public function updatedCategoryId()
{
$this->sub_categories = Category::whereParentId($this->category_id)->get();
}


I have 2 problems the search in select2 not allow me to focus in search input, and another issue when change category_id not change anything in child_category_id but when remove wire:ignore from child_category_id div it works fine and the options show


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 (4)


this is inside modal



Hi,

Please check this link for the updated select2.
https://gist.github.com/faizalmy/243ef00e9d9059429c5b27e10fe67176

Search Input Focus Issue:
The select2() method is called on both the category_id and child_category_id dropdowns during the livewire:load event. This initializes Select2 properly.

Child Category Update Issue:
The wire:ignore directive has been removed from the child_category_id div to allow Livewire to manage the element.
The select2() method is now applied to the child_category_id dropdown, enabling it to work with Select2 and update correctly when category_id changes.

Let us know if any issue. Thanks



nothing changes the same problem, when the select outside modal it works fine but the problem inside the modal, and inside the modal when edit the option chosen not selected just marked as true



You can try initializing Select2 within the Bootstrap modal event. Here's a revised approach:

Initialize Select2 within the Bootstrap modal event for when the modal is fully loaded.


$("#your-modal").on("shown.bs.modal", function () {
$("#your-select-element").select2();
});


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