lara-pack/livewire-select2 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lara-pack/livewire-select2

Composer 安装命令:

composer require lara-pack/livewire-select2

包简介

Livewire Component : Select2

README 文档

README

A custom Livewire V3/V4 component wrapper for Select2. This component simplifies the usage of Select2 in your Livewire projects, supporting both static options arrays and AJAX-based remote data loading, along with multiple selections capabilities.

Requirements

  • PHP 8.1+
  • Laravel 10+ / 11+
  • Livewire ^3.0|^4.0
  • jQuery
  • Select2 (JS & CSS)

Installation

You can install the package via composer:

composer require lara-pack/livewire-select2

Note: Ensure you have already included jQuery and Select2 assets in your layout file/app.

Usage

You can use the component in your Livewire blade views using the <livewire:... /> tag.

1. Basic Usage (Static Options)

Provide an array of arrays containing id and text keys.

<livewire:lara-pack.livewire-select2
  wire:model="selectedCity"
  :options="[
        ['id' => '1', 'text' => 'Jakarta'],
        ['id' => '2', 'text' => 'Bandung'],
        ['id' => '3', 'text' => 'Surabaya']
    ]"
  placeholder="Pilih Kota"
/>

2. AJAX Remote Data (Load from API / URL)

Instead of passing static options, you can provide an endpoint URL. The component will handle the AJAX calls and debounce internally.

The API should return an array of objects structured as [{ "id": 1, "text": "Option 1" }, ...].

<livewire:lara-pack.livewire-select2
  wire:model="selectedUser"
  url="{{ route('api.users') }}"
  :minimumInputLength="3"
  placeholder="Cari user berdasarkan nama..."
/>

3. Multiple Selections

Enable multiple selection mode by passing :multiple="true".

<livewire:lara-pack.livewire-select2
  wire:model="selectedTags"
  url="{{ route('api.tags') }}"
  :multiple="true"
  placeholder="Pilih beberapa tag"
/>

4. Custom "Select All Filtered" for Multiple AJAX

This component comes with a special feature for AJAX Multiple Select. If you pass :multipleSelection="true", an extra option "--- Pilih Semua Yang Tampil ---" will appear at the top. Selecting it will fetch and select all data matching the search term.

<livewire:lara-pack.livewire-select2
  wire:model="selectedProducts"
  url="{{ route('api.products') }}"
  :multiple="true"
  :multipleSelection="true"
/>

Available Properties

Property Type Default Description
wire:model string null Bind the selected value. Must be { "id": ..., "text": ... } for single select, or an array of objects for multiple.
options array [] Static array of options: [['id'=>1, 'text'=>'Opt']].
url string "" The API Endpoint for AJAX data loading.
minimumInputLength int 0 Number of characters required to trigger the search.
placeholder string "" Placeholder text for the input.
allowClear bool true Show a clear button (X) to reset the value.
dropdownParent string "" Appends the dropdown to a specific element. Very useful inside Bootstrap Modals (e.g., '#myModal').
debounceTime int 500 Delay in milliseconds before firing the AJAX request.
class string '' Extra CSS classes injected directly to the <select> element.
theme string '' Select2 theme (e.g., 'bootstrap-5').
multiple bool false Set to true to allow arrays of selections.
multipleSelection bool false When true in AJAX multiple mode, allows selecting all loaded search results at once.
disabled bool false Disables the Select2 input entirely.

Data Binding & Format

Because Livewire components pass data over the wire, the bound value representation is always an object or an array of objects, representing the selected id and text.

For single selection:

// In your Livewire Component Controller
public $selectedCity = ['id' => '1', 'text' => 'Jakarta'];

For multiple selections:

// In your Livewire Component Controller
public $selectedTags = [
    ['id' => '1', 'text' => 'PHP'],
    ['id' => '2', 'text' => 'Laravel']
];

Troubleshooting: Modals

If your Select2 input isn't clickable or doesn't show properly inside a Bootstrap Modal, you need to set the dropdownParent property to target your modal element.

<livewire:lara-pack.livewire-select2 ... dropdownParent="#yourModalId" />

License

This package is distributed under the MIT license.

lara-pack/livewire-select2 适用场景与选型建议

lara-pack/livewire-select2 是一款 基于 Blade 开发的 Composer 扩展包,目前已累计 111 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lara-pack/livewire-select2 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lara-pack/livewire-select2 我们能提供哪些服务?
定制开发 / 二次开发

基于 lara-pack/livewire-select2 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 111
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 18
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: Blade

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-19