定制 israr22/searchable-select 二次开发

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

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

israr22/searchable-select

最新稳定版本:v1.0.0

Composer 安装命令:

composer require israr22/searchable-select

包简介

A Laravel Nova Searchable Dependent Select filter.

README 文档

README

This package provides filters what depends of another filters.

  1. Installation
  2. Usage
    1. Declaration
    2. Class declaration
    3. Static dependencies
    4. Dynamic dependencies
    5. Hiding empty filters
    6. Default filter value
    7. Other stuffs
  3. Thanks

Installation

You can install the package in to a Laravel app that uses Nova via composer:

composer require israr22/searchable-select

Usage

Declaration

You can declare filters in you filters method directly:

function filters(Request $request)
{
    return [
        (new SearchableSelect('State'))
            ->withOptions([
                'all' => 'All orders',
                'dragt' => 'Draft',
                'outstanding' => 'Outstanding',
                'past_due' => 'Past due',
                'paid' => 'Paid',
            ]),
    ];
}

Also you can use SearchableSelect::make() instead new SearchableSelect().

For queries you need to use callback declaration:

function filters(Request $request)
{
    return [
        SearchableSelect::make('Category', 'category_id'))
            ->withOptions(function (Request $request) {
                return Category::pluck('title', 'id');
            }),
    ];
}

Note: In difference with Nova filters filter's value need pass as array key and label as array value.

Class declaration

As is Nova filters you can create filter's class:

class CategoryFilter extends SearchableSelect
{
    /**
     * Name of filter.
     *
     * @var string
     */
    public $name = 'Category';
    
    /**
     * Attribute name of filter. Also it is key of filter.
     *
     * @var string
     */
    public $attribute = 'ctaegory_uid';
    
    public function options(Request $request, array $filters = [])
    {
        return Category::pluck('title', 'id');
    } 
}

Note: The fresh method is identical with the callback for declaring options.

function filters(Request $request)
{
    return [
        CategoryFilter::make(),
    ];
}

Static dependencies

For creating dependent filter you need to specify dependent filters values at which the option will be shown:

function filters(Request $request)
{
    return [
        CategoryFilter::make(),
        
        SubCategory::make('Subcategory', 'subcategory_id')
            ->withOptions(function (Request $request) {
                return SubCategory::all()->map(function ($subcategory) {
                    return [
                        'value' => $subcategory->id,
                        'label' => $subcategory->title.
                        'depends' => [
                            'category_id' => $subcategory->category_id, //Also you can set array of values
                        ],
                    ];
                });
            }),
    ];
}

Note. Instead of an attribute or class name, you must specify the key of the filter.

Dynamic dependencies

For big collection of data you can use dynamic updating of the filter.

function filters(Request $request) 
{
    return [
        StateFilter::make('State', 'state_id'),
        
        SearchableSelect::make('City', 'city_id')
            ->dependentOf('state_id')
            ->withOptions(function (Request $request, $filters) {
                return City::where('state_id', $filters['state_id'])
                    ->pluck('title', 'id');
            }),
    ];
}

In class declaration you also need to set $dependentOf property:

class CityFilter extends SearchableSelect
{
    public $dependentOf = ['state_id'];
    
    function options(Request $request, $filters = [])
    {
        return City::where('state_id', $filters['state_id'])
            ->pluck('title', 'id');
    }
}

If you want to show options only when main filter is selected you can use when for check it:

function options(Request $request, $filters = []) {
    return City::when($filters['state_id'], function ($query, $value) {
        $query->where('state_id', $value)
    })->pluck('title', 'id');
}

Hiding empty filters

You can hide filters until they have options.

For it you need set $hideWhenEmpty or call hideWhenEmpty() method:

class CityFilter extends SearchableSelect
{
    public $hideWhenEmpty = true;
    
}
function filters(Request $request) {
    return [
        StateFilter::make('State', 'state_id'),
        
        CityFilter::make('City', 'city_id')->hideWhenEmpty(),
    ];
}

Default filter value

If you want to set default value you need to call withDefault method with value or overload default method in class declaration.

function filters(Request $request) {
    return [
        StateFilter::make('State', 'code')->withDefault('WA'),
    ];
}
class StateFilter extends SearchableSelect
{
    public function default()
    {
        return 'WA';
    }
}

Other stuffs

By default filter checking by equal filed specified in $attribute and filter value. You can overload it like as in Nova filters:

class MyFilter extends SearchableSelect
{
    public function apply(Request $request, $query, $value)
    {
        return $query->where('column', '>=', $value);
    }
}

When you use declare style you can set pass apply callback to withApply method:

function filters(Request $request) {
    return [
        StateFilter::make('State', 'code')->withApply(function ($request, $query, $value) {
            return $query->where('code', '=', $value);
        }),
    ];
}

Also you can specify another filter key over method key.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-11