thiktak/laravel-bootstrap-component-select2
Composer 安装命令:
composer require thiktak/laravel-bootstrap-component-select2
包简介
Laravel Bootstrap Blade components for Select2 & Bootstrap 5.
README 文档
README
Based on the components of https://github.com/bastinald/laravel-bootstrap-components
Installation
Run composer to install the package
composer require thiktak/laravel-bootstrap-component-select2
Include css
Include on your resources/scss/app.scss the line:
@import 'vendor/thiktak/laravel-bootstrap-component-select2/resources/scss/select2';
Include Javascript
Include on your resources/js/app.js the line:
require('../../vendor/thiktak/laravel-bootstrap-component-select2/resources/js/select2');
Run NPM
npm run dev
Use the component on your blades
<x-bs::select2 :label="__('User')" :options="['1' => 'Admin']" what="User" wire:model.defer="user_id" />
This bs::select2 component use the bs:select of https://github.com/bastinald/laravel-bootstrap-components. You can use all the options of this component.
Here, the important part is the what="Model", with Model = App\Models\User or User
Configure your models
- Implement the trait
Select2Searchable - Fill the protected variable $search2Fields with list of columns to be added in the where closure
- Implement the
__toString()model method if you want to have a beautiful output
use Thiktak\LaravelBootstrapComponentSelect2\Models\Search2Proxies\Select2Searchable; class User extends Model { use Select2Searchable; // Will use magic search protected $search2Fields = ['name', 'title']; // [...] }
You can redefine the methods search2 and search2_export if required.
You can also create a proxy and build you own query. The Select2Proxy implement automatically the Select2Searchable trait.
The proxy should be on App\Models\Search2Proxies folder.
namespace App\Models\Search2Proxies; use Thiktak\LaravelBootstrapComponentSelect2\Models\Search2Proxies\Select2Proxy; class UserProxy extends Select2Proxy { /* * protected $search2Fields = ['name', 'title']; */ /** * METHOD getModel *---------------- * Define your own model * Used by magic method select2, if not overwritten */ public function getModel() { return new \App\Models\User; } /** * Method Select2 *---------------- * Will return a query object based on the keyword searched * ... or the ID provided */ public function select2($id, $term) { return $this->getModel() ->query() // Search by ID ->when($id, function($q) use($id) { // no return $q->find($id); // OR $q->where($this->getModel()->getPrimaryKey(), $id) }) ->when(!$id, function($q) use($term) { return $q ->where('name', 'like', '%' . $term . '%') ->orderBy('name'); }); } /** * Method Export_select2 * Will export array data based on Select2 format id/text */ public function export_select2(User $user) { // id => '1' // text => 'Admin (user@example.net)' return [ 'id' => $user->id, 'text' => sprintf('%s (%s)', $user->name, $user->email), ]; } // [...] }
Test the API
You can test the data exported to the select by opening directly /api/select2/search?what=User&q=a
If needed, you can use {{ route('api.select2.search') }}
Parameters
what= the model to be used (i.e.: User or App\Models\User)q= search keyword
Output:
- results: [{id: 1, text: "label"}]
- error: { code: 200, message: null }
Use the error code to know what happens.
TODO
- Use Proxy for Model (App\Models\Search2Proxies<ModelName>Proxy)
Changelogs
- Added select2/autoload values (fetch data via API in order to keep label synchronization)
- Added possibility to fetch by ID or by term
- Added Proxy model
thiktak/laravel-bootstrap-component-select2 适用场景与选型建议
thiktak/laravel-bootstrap-component-select2 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 07 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 thiktak/laravel-bootstrap-component-select2 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 thiktak/laravel-bootstrap-component-select2 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-02