kayacekovic/nova-multiselect-field

Laravel Nova 的多选字段。

资助包维护!
optimistdigital

安装次数: 4

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 118

语言:Vue

3.0.0 2021-02-05 13:28 UTC

README

Latest Version on Packagist Total Downloads

Laravel Nova 包为 Nova 的字段工具箱添加了多选功能。

要求

  • php: >=7.2
  • laravel/nova: ^3.0

特性

  • 带搜索的多选和单选
  • 异步搜索
  • 拖拽排序功能
  • 依赖其他多选实例

截图

Detail View

Form View

Index View

Reorder GIF

安装

通过 Composer 在 Laravel Nova 项目中安装此包

composer require optimistdigital/nova-multiselect-field

使用方法

该字段的使用方式与 Nova 的原生选择字段类似。数据库中的字段类型应为基于文本的(例如 stringtextvarchar),选中的值以字符串化的 JSON 数组的形式存储。

use OptimistDigital\MultiselectField\Multiselect;

public function fields(Request $request)
{
    return [
      Multiselect::make('Football teams')
        ->options([
          'liverpool' => 'Liverpool FC',
          'tottenham' => 'Tottenham Hotspur',
        ])

        // Optional:
        ->placeholder('Choose football teams') // Placeholder text
        ->max(4) // Maximum number of items the user can choose
        ->saveAsJSON() // Saves value as JSON if the database column is of JSON type
        ->optionsLimit(5) // How many items to display at once
        ->reorderable() // Allows reordering functionality
        ->singleSelect() // If you want a searchable single select field

        // Async model querying
        Multiselect::make('Artists')
          ->asyncResource(Artist::class),

          // If you want a custom search, create your own endpoint:
          ->api('/api/multiselect/artists?something=false', Artist::class),
    ];
}

选项组

支持选项组。它们的语法与 Laravel 的选项组语法 相同。

在这个示例(来自 Nova 文档)中,所有值都通过 group 键进行分组

->options([
    'MS' => ['label' => 'Small', 'group' => 'Men Sizes'],
    'MM' => ['label' => 'Medium', 'group' => 'Men Sizes'],
    'WS' => ['label' => 'Small', 'group' => 'Women Sizes'],
    'WM' => ['label' => 'Medium', 'group' => 'Women Sizes'],
])

依赖

您可以通过使用 dependsOn 使多选依赖另一个字段。这也需要指定 ->dependsOnOptions()。依赖的多选字段的值必须是选项的键,值必须是选项的键值字典,如常规操作。

使用方法

Multiselect::make('Country', 'country')
    ->options([
        'IT' => 'Italy',
        'SG' => 'Singapore',
    ]),

Multiselect::make('Language', 'language')
    ->dependsOn('country')
    ->dependsOnOptions([
        'IT' => [
            'it' => 'Italian',
        ],
        'SG' => [
            'en' => 'English',
            'ms' => 'Malay',
            'zh' => 'Chinese',
        ]
    ]),

    // Optionally define max number of values
    ->dependsOnMax([
        'IT' => 1,
        'SG' => 3,
    ])

多对多关系

您可以使用此字段进行 BelongsToMany 关系选择。

// Add your BelongsToMany relationship to your model:
public function categories()
{
    return $this->belongsToMany(\App\Models\Category::class);
}

// Add the field to your Resource:
Multiselect::make('Categories', 'categories')
  ->belongsToMany(\App\Nova\Resources\Category::class),

选项

您可以传递给字段的可能选项,使用选项名称作为函数,例如 ->placeholder('选择花生')

本地化

可以使用以下发布命令发布翻译文件

php artisan vendor:publish --provider="OptimistDigital\MultiselectField\FieldServiceProvider" --tag="translations"

然后您可以根据需要编辑字符串。

覆盖详情字段

您可以覆盖详情视图值组件以按需自定义它。

NovaMultiselectDetailFieldValue 创建一个新的组件并将其注册到您的 app.js 中。该组件接收两个属性:fieldvaluesvalues 属性是一个包含所选标签的数组。

// in NovaMultiselectDetailFieldValue.vue

<template>
  <div class="relative rounded-lg bg-white shadow border border-60" v-if="values">
    <div class="overflow-hidden rounded-b-lg rounded-t-lg">
      <div class="border-b border-50 cursor-text font-mono text-sm py-2 px-4" v-for="(value, i) of values" :key="i">
        {{ value }}
      </div>
    </div>
  </div>

  <div v-else></div>
</template>

<script>
export default {
  props: ['field', 'values'],
};
</script>
// in app.js

import NovaMultiselectDetailFieldValue from './NovaMultiselectDetailFieldValue';

Nova.booting((Vue, router, store) => {
  Vue.component('nova-multiselect-detail-field-value', NovaMultiselectDetailFieldValue);
});

鸣谢

许可

本项目是开源软件,受 MIT 许可证 许可。