承接 jackbayliss/livewire-select2 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

jackbayliss/livewire-select2

Composer 安装命令:

composer require jackbayliss/livewire-select2

包简介

Simple to use Livewire component for Select2

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads Laravel Compatibility

Easy to use Livewire component specifically for Select2.

Installation

You can install the package via composer: This currently works on Livewire V3 and V4

composer require jackbayliss/livewire-select2

Initial Setup

First of all, ensure you install jQuery, and select2- for example, the below. You can also install these via npm and import it into your app.js. Main thing is ensuring the scripts are included anywhere you want select2 to work.

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

Usage

The package is quite straight forward, and can be used directly in a LIVEWIRE component, see below the basic usage

<livewire:select-2 :options="$this->vehicles" onchange="triggerMyFunction" name="vehicles" model="Vauxhall" multiple/>

Component parameters

options (required) - array

The select2 component expects an array of options, this can be for example - you must pass this into the options param as per the above.

public $vehicles = ['Ford','Vauxhall','Seat'];

onchange (required) - string

The select2 component expects a string of the listener function to call. For example, triggerMyFunction- you can see how to do this below. The onchange function, expects one param. The param returned from this component is an array of data - which includes the following:

Name - The name param you passed in, this can be used in your custom function if required - this is null if not supplied.
Data - This is the values returned from the select2 change, if you have not set the multiple param, this is a string- otherwise its an array. Please see https://github.com/jackbayliss/livewire-select2?tab=readme-ov-file#base-component if theres any confusion.

Important

Ensure your function includes one parameter, for example triggerMyFunction($select2)

name (not required) - string

You can pass this to your component if you need specific logic based on the name.

model (not required) - string

You can pass an option value in as model, this then auto selects the option- useful in a case where a selection has already been made.

<livewire:select-2 :options="$this->vehicles" onchange="callVehicles" name="vehicles" model="Vauxhall"/>

How to add a custom class

This can apply a custom CSS class to the Livewire Select2 component, see below for an example

<style>
        .example{
            width: 50% !important;
        }
</style>
<livewire:select-2 :options="$this->vehicles" onchange="triggerMyFunction" name="vehicles" model="Vauxhall" multiple :class="'example'"/>

multiple (not required) - blank param

Tip

If this is used, the data will be returned as an array, rather than a string.

This allows you to select multiple options, as per select2 usually does and should be used like the following:

<livewire:select-2 :options="$this->vehicles" onchange="callVehicles" name="vehicles" multiple/>

Creating a Listener

Tip

A listener is required for the onchange param, in order to create a listener, you should do the below. This means, whenever the select2 component is changed - ie an option clicked, it will call the function you defined, and you can do as you wish with the logic.

<livewire:select-2 :options="$this->vehicles" onchange="triggerMyFunction" name="vehicles"/>
    protected $listeners = ['triggerMyFunction'];

    public function triggerMyFunction($data){
        dd($data['name'],$data['data']);
    }

Important

Whatever string you pass to the onchange param, ensure you create a listener and function like the above. Your function must accept one parameter, this is the array of data returned which includes name and data, name being the name you set on the component (if you did set one) and the data ie the value selected.

Example / How it works in practice

Base component

I have an initial component, which I am calling as livewire:test in my blade. The component is below:

<?php

namespace App\Livewire;

use Livewire\Component;
class Test extends Component
{

    public string $selectedVehicle = 'N/A';
    public array $vehicles = ['Ford','Vauxhall','Seat'];

    protected $listeners = ['callVehicles'];
    
    // DO YOUR OWN LOGIC...
    public function callVehicles($output){
            if(in_array($output['data'],$this->vehicles)){
                $this->selectedVehicle = $output['data'];
            }
    }
    public function render()
    {
        return view('livewire.test');
    }
}

The view, is basic and looks like the below - main thing to take away is how the component is used:

<div>
<livewire:select-2 :options="$this->vehicles" onchange="callVehicles" name="vehicles"/>
{{ $this->selectedVehicle }}
</div>

You then get the below...

test-component

Stuck?

I have an example project setup below.. you can simply download it and composer install.

https://github.com/jackbayliss/livewire-select-2-example-proj

Testing WIP

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

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

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

它主要适用于以下技术方向: 「laravel」 「livewire-select2」 「JackBayliss」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 3
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-08