定制 zachgilbert/vue-multiselectable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

zachgilbert/vue-multiselectable

Composer 安装命令:

composer create-project zachgilbert/vue-multiselectable

包简介

PHP support for vue-multiselect

README 文档

README

This package provides methods to conveniently convert PHP classes to array data prepared for use on the frontend Vue component, Vue Multiselect (2.4.1).

Please note this utility package is independent of and not endorsed by https://vue-multiselect.js.org/.

Table of Contents:

  1. Installation
  2. The Multiselectable Trait
  3. Eloquent Collection Methods
  4. Eloquent Query Builder Methods

# 1. Installation

Install using Composer:

$ composer require zachgilbert/vue-multiselect

Apply the trait Multiselectable to any PHP object (such as an Eloquent model, in Laravel) that you wish to convert to an option for use on the <vue-multiselect /> Vue component:

<?php

use ZachGilbert\VueMultiselectable\Traits\Multiselectable;

class User
{
    use Multiselectable;

    // ...
}

# 1.1. Laravel

To extend Eloquent collections in laravel, be sure to register the following provider in config/app.php:

<?php

return [
    // ...

    'providers' => [
        // ...

		\ZachGilbert\VueMultiselectable\Laravel\Providers\MultiselectProvider::class,
    ],
];

# 2. The Multiselectable Trait

# 2.1. toMultiselectOption()

The toMultiselectOption() method converts any given class instance to an option formatted for use on the <vue-multiselect /> Vue component.

toMultiselectOption() accepts five arguments: $trackByAttribute, $labelAttribute, $trackByKey, $labelKey, and $isDisabled.

  • $trackByAttribute and $labelAttribute are the attributes on the class that should be used for the value and the label on the multiselect option:
     <?php
     $user->toMultiselectOption('id', 'name');
     // ['value' => 1, 'name' => 'John McEnroe']
    Note that $trackByAttribute is the only required argument. Provided on its own, ->toMultiselectOption() will just return the attribute value:
     <?php
     $user->toMultiselectOption('name');
     // 'John McEnroe'
  • $trackByKey and $labelKey set how the attributes should be keyed:
     <?php
     $user->toMultiselectOption('id', 'name', 'user_id', 'user_name');
     // ['user_id' => 1, 'user_name' => 'John McEnroe']
  • $isDisabled, if provided, will set a key with a boolean value for whether the option should be disabled:
     <?php
     $user->toMultiselectOption('id', 'name', 'value', 'label', true);
     // ['value' => 1, 'name' => 'John McEnroe', '$isDisabled' => true]

# 3. Eloquent Collection Methods

In Larvel, collections of PHP objects that use the Multiselectable trait can be easily converted to a JSON-serializable array of options.

Make sure to register the provider MultiselectProvider in config/app.php).

# 3.1. multiselect()

The multiselect() method converts each item in the collection to an array of options.

multiselect() accepts four arguments: $trackByAttribute, $labelAttribute, $trackByKey, and $labelKey:

  • $trackByAttribute and $labelAttribute are the attributes on each item in the collection that should be used for the value and the label on the multiselect option:
     <?php
     $collection->multiselect('id', 'name')->toJson();
     // '[{"value": 1, "name": "John McEnroe"}, {"value": 2, "name": "Dr Eggman"}]''
    Note that if only the first argument is provided, the resultant collection will be an array of that attribute's values:
     <?php
     $collection->multiselect('name')->toJson();
     // '["John McEnroe", "Dr Eggman"]'
  • By default, the Vue multiselect component assumes the value and label on each option will be keyed by 'value' and 'label'. Using $trackByKey and $labelKey can set how the attributes should be keyed:
     <?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()

The disableOptionsBy() method disables options for the multiselect based on a callback (the return value of which is evaluated as true or false). Any items that pass the truth test will have $isDisabled = true set; any items that fail will have $isDisabled = false set.

<?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"}]'

Note that disabling multiselect options is only applicable to options which can be converted to objects in JSON (i.e. arrays).

# 4. Eloquent Query Builder Methods

Using Laravel's Eloquent query builder, we can obtain database records in a multiselect-ready format. Note this does not require any classes using the Multiselectable trait.

# 4.1. multiselect()

The multiselect() method selects two columns, with aliases, ready for use on the multiselect component. It accepts four arguments: $trackBySelect, $labelSelect, $trackByAlias, and $labelAlias:

  • $trackBySelect and $labelSelect are the raw SQL columns/statements to select, that should be used for the value and the label on the multiselect:
     <?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 and $labelAlias set the column names for the selects:
     <?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."}]'

zachgilbert/vue-multiselectable 适用场景与选型建议

zachgilbert/vue-multiselectable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 07 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 zachgilbert/vue-multiselectable 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-03