zachgilbert / vue-multiselectable
支持PHP的vue-multiselect
Requires
- illuminate/database: ^6.0|^7.0|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
Requires (Dev)
- mockery/mockery: ^1.4.4
- phpunit/phpunit: ^9.5.10
This package is auto-updated.
Last update: 2024-09-30 02:16:01 UTC
README
本包提供方法,方便地将PHP类转换为适合在Vue组件(2.4.1版)前端Vue组件上使用的前端数组数据,Vue Multiselect。
请注意,此实用程序包独立于https://vue-multiselect.js.org/,并且没有得到其认可。
目录
- 安装
- 1.1. Laravel
Multiselectable
特性- Eloquent 集合方法
- 3.1.
multiselect()
- 3.2.
disableOptionsBy()
- 3.1.
- Eloquent 查询构建器方法
- 4.1.
multiselect()
- 4.1.
# 1. 安装
使用Composer安装
$ composer require zachgilbert/vue-multiselect
将 Multiselectable
特性应用到任何你希望转换为 <vue-multiselect />
Vue组件选项的PHP对象(如Laravel中的Eloquent模型)
<?php use ZachGilbert\VueMultiselectable\Traits\Multiselectable; class User { use Multiselectable; // ... }
# 1.1. Laravel
要在Laravel中扩展Eloquent集合,请确保在 config/app.php
中注册以下提供者
<?php return [ // ... 'providers' => [ // ... \ZachGilbert\VueMultiselectable\Laravel\Providers\MultiselectProvider::class, ], ];
# 2. Multiselectable
特性
# 2.1. toMultiselectOption()
toMultiselectOption()
方法将任何给定的类实例转换为适合在 <vue-multiselect />
Vue组件上使用的选项格式。
toMultiselectOption()
接受五个参数:$trackByAttribute
、$labelAttribute
、$trackByKey
、$labelKey
和 $isDisabled
。
$trackByAttribute
和$labelAttribute
是类上用于多选选项的值和标签的属性<?php $user->toMultiselectOption('id', 'name'); // ['value' => 1, 'name' => 'John McEnroe']
请注意,$trackByAttribute
是唯一必需的参数。如果单独提供,->toMultiselectOption()
将仅返回属性值<?php $user->toMultiselectOption('name'); // 'John McEnroe'
$trackByKey
和$labelKey
设置属性应该如何键入<?php $user->toMultiselectOption('id', 'name', 'user_id', 'user_name'); // ['user_id' => 1, 'user_name' => 'John McEnroe']
$isDisabled
,如果提供,将设置一个具有布尔值的键,以确定选项是否应该禁用<?php $user->toMultiselectOption('id', 'name', 'value', 'label', true); // ['value' => 1, 'name' => 'John McEnroe', '$isDisabled' => true]
# 3. Eloquent 集合方法
在Laravel中,使用 Multiselectable
特性的PHP对象集合可以轻松转换为JSON序列化的选项数组。
确保在
config/app.php
中注册提供者MultiselectProvider
。
# 3.1. multiselect()
multiselect()
方法将集合中的每个项目转换为选项数组。
multiselect()
接受四个参数:$trackByAttribute
、$labelAttribute
、$trackByKey
和 $labelKey
$trackByAttribute
和$labelAttribute
是集合中每个项目上用于多选选项的值和标签的属性<?php $collection->multiselect('id', 'name')->toJson(); // '[{"value": 1, "name": "John McEnroe"}, {"value": 2, "name": "Dr Eggman"}]''
请注意,如果仅提供第一个参数,结果集合将仅是该属性值的数组<?php $collection->multiselect('name')->toJson(); // '["John McEnroe", "Dr Eggman"]'
- 默认情况下,Vue多选组件假设每个选项的值和标签将通过
'value'
和'label'
键来标识。使用$trackByKey
和$labelKey
可以设置属性应该如何键入。<?php $collection->multiselect('id', 'name', 'user_id', 'user_name')->toJson(); // '[{"user_id": 1, "user_name": "John McEnroe"}, {"user_id": 2, "user_name": "Dr Eggman"}]'
# 3.2. disbaleOptionsBy()
disableOptionsBy()
方法根据一个回调(其返回值被评估为true
或false
)禁用多选选项。任何通过真值测试的项目将设置$isDisabled = true
;任何未通过的项目将设置$isDisabled = false
。
<?php $options = $collection->disableOptionsBy(function ($item, $key) { return strpos($item->name, 'John') === 0; }); $options->multiselect('id', 'name')->toJson(); // '[{"value": 1, "label": "John McEnroe", "$isDisabled": true}, {"value": 2, "label": "Dr Eggman"}]'
请注意,禁用多选选项仅适用于可以转换为JSON对象的选项(即数组)。
# 4. Eloquent查询构建器方法
使用Laravel的Eloquent查询构建器,我们可以以多选组件准备好的格式获取数据库记录。注意,这不需要使用Multiselectable
特征的任何类。
# 4.1. multiselect()
multiselect()
方法选择两个带有别名的列,准备好用于在多选组件中使用。它接受四个参数:$trackBySelect
、$labelSelect
、$trackByAlias
和$labelAlias
。
$trackBySelect
和$labelSelect
是要选择的原始SQL列/语句,用于多选中的值和标签。<?php $users = User::query() ->multiselect('users.id', 'CONCAT(users.name, " - ", companies.name)') ->join('companies', 'users.company_id', '=', 'companies.id'); // SELECT `users`.`id` AS `value`, CONCAT(`users`.name`, " - ", `companies`.`name`) AS `label` // ... $users->get()->toJson(); // '[{"value": "1", "label": "John McEnroe - Tennis Co."}]'
$trackByAlias
和$labelAlias
设置了选择列的列名。<?php $users = User::query() ->multiselect( 'users.id', 'CONCAT(users.name, " - ", companies.name)', 'employee_id', 'employee_name' ) ->join('companies', 'users.company_id', '=', 'companies.id'); // SELECT `users`.`id` AS `employee_id`, CONCAT(`users`.name`, " - ", `companies`.`name`) AS `employee_name` // ... $users->get()->toJson(); // '[{"employee_id": 1, "employee_name": "John McEnroe - Tennis Co."}]'