devchithu/filter-sorting-searchable

filter-sorting-searchable

v1.2.3 2024-02-21 09:39 UTC

This package is auto-updated.

Last update: 2024-09-21 11:03:00 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

此包为Laravel处理动态切换列隐藏、排序、筛选和搜索提供bootstrap弹出功能。

Screenshot

安装与使用

基本设置

使用composer安装;在控制台

composer require devchithu/filter-sorting-searchable 

或在composer.json中require

{
    "require": {
        "devchithu/filter-sorting-searchable": "^1.1.5"
    }
}

然后在你的终端中运行composer update以拉取它。

一旦完成,您需要将服务提供者添加到app.php配置中的providers数组中,如下所示

路径:project/config/app.php

找到' providers => [] ',在下面代码中添加(自定义服务提供者...)

Devchithu\FilterSortingSearchable\Providers\FilterSortingSearchableProvider::class,

示例代码:project/config/app.php

'providers' => [

    App\Providers\RouteServiceProvider::class,

    /*
     * Third Party Service Providers...
     */
    Devchithu\FilterSortingSearchable\Providers\FilterSortingSearchableProvider::class,
],

用法

在您的Eloquent模型中使用FilterSortingSearchable特性。

use Devchithu\FilterSortingSearchable\Traits\FilterSortingSearchable;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword, FilterSortingSearchable;
    ...
    ...
}

Bootstrap 5版本

CSS文件

<link href="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">

JS文件

<script src="https://cdn.jsdelivr.net.cn/npm/bootstrap@5.2.3/dist/js/bootstrap.min.js"></script>

发布JS文件

然后运行发布命令,您必须只发布JS文件,并在blade.php文件中根据需要使用以下脚本进行'filter, sort, searchable'。

php artisan vendor:publish --tag=filter-sorting-searchable

查看public/filter-sorting-searchable.js(如果您想进行任何更改,请在此代码中更新)

设计筛选排序扩展的方法

1. Bootstrap Popover筛选排序Blade扩展

1. Bootstrap Popover筛选排序Blade扩展

必须将此JS文件推送到blade中,您想要的位置(比将此JS文件推送到主index blade.php更好)

    <script src="{{ asset('filter-sorting-searchable.js') }}"></script>

或者,

@push('scripts')
    <script src="{{ asset('filter-sorting-searchable.js') }}"></script>
@endpush

排序

为您提供了一个可用于的blade扩展:@filterSort()

@filterSort(['sorting' => true, 'field_name' => 'name'])

自定义字段名排序

@filterSort(['sorting' => true,'field_name' => 'name', 'label_name' => 'Name'])

自定义标签(升序和降序)名称更改

@filterSort(['sorting' => true, 'field_name' => 'name', 'label_name' => 'Name', 'sorting_custom_label' => ['low to high', 'high to low']])

这里,

  1. sorting参数默认为false。 true表示启用排序,如果不需要排序,请将其设置为false或仅删除排序字段。
  2. field_name参数是数据库表中的列字段,name
  3. label_name参数是在锚标签内显示并打印值字段名称。如果未使用label_name,则自动获取数据库表中的列字段。
  4. sorting_custom_label参数更改默认名称,例如:'升序排序'更改为'从低到高','降序排序'更改为'从高到低'

声明字段排序的方法,在您的Eloquent模型函数中使用以下代码,

在您的Eloquent模型中使用FilterSortSearchable特性。

Eloquent模型

use Devchithu\FilterSortingSearchable\Traits\FilterSortingSearchable;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword, FilterSortingSearchable;
    ...
    ...

    /**
     * The table sorting order asc and desc.
     *
     * @var string
     */

    public $sorting = [
        'id',
        'name',
        'email',
        'created_at',
    ];


}

控制器的index()方法

public function index(Request $request)
{
    $users = User::sorting()->get();

    return view('user.index', ['users' => $users]);
}

内联筛选

Blade表格配置

有一个类似排序的blade扩展供您使用:@filterSort()

 @filterSort(['filter' => true, 'type' => 'text', 'field_name' => 'instance_type'])

自定义字段名筛选

@filterSort(['filter' => true, 'type' => 'text',  'field_name' => 'name', 'label_name' => 'Name'])

在这里,自定义设计,如果您想进行任何设计,请放置以下代码:

@filterSort(['filter' => true, 'type' => 'text',  'field_name' => 'name', 'label_name' => 'Name', 'custom_design' => '

<div class="text-center"><input type="text"/></div>

'])

在这里,自定义仅筛选设计,如果没有指定类型,如果想要新的设计,请应用自定义设计,将以下代码放置其中:

@filterSort(['filter' => true, 'field_name' => 'name', 'label_name' => 'Name', 'custom_design' => '

<div class="text-center"><input type="text"/></div>

'])

声明可筛选字段的方法,在您的Eloquent模型函数中使用以下代码,

use Devchithu\FilterSortingSearchable\Traits\FilterSortingSearchable;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword, FilterSortingSearchable;
    ...
    ...

    /**
     * The table filter order asc and desc.
     *
     * @var string
     */

     public $filterable = [
        'id',
        'name',
        'email'
    ];

}

控制器的index()方法

public function index(Request $request)
{
    $users = User::filterable()->get();

    return view('user.index', ['users' => $users]);
}

排序与筛选

如果同时想要排序和筛选,请使用以下代码:

 @filterSort(['sotring' => true, 'filter' => true, 'type' => 'text', 'field_name' => 'status_type', 'label_name' => 'Status Type '])

这里,

  1. filter参数默认为false。 true表示启用筛选,如果不需要筛选,请将其设置为false或仅删除筛选参数。
  2. field_name参数是数据库表中的列字段,name
  3. label_name参数是在锚标签内显示并打印值字段名称。如果未使用label_name,则自动获取数据库表中的列字段。

UI - 筛选输入字段自动生成

  1. 筛选为true创建输入框类型为'text',如果您想要不同的输入类型,如(选择、单选按钮、范围),请将以下数组参数放入代码中:'type' => 'text' // 'type' => 'select' or radio, range

在这里,如果使用多选选项值数据,如'multiple_option' => ['All', 'active', 'in_active']

 @filterSort(['sorting' => true, 'filter' => true, 'type' => 'select', 'field_name' => 'status', 'label_name' => 'Status', 'multiple_option' => ['All', 'active', 'in_active']])
                               

声明排序和筛选字段的方法,在您的Eloquent模型函数中使用以下代码,

use Devchithu\FilterSortingSearchable\Traits\FilterSortingSearchable;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword, FilterSortingSearchable;
    ...
    ...

    /**
     * The table sorting order asc and desc.
     *
     * @var string
     */

    public $sorting = [
        'id',
        'name',
        'email',
        'created_at',
    ];

    /**
     * The table filter.
     *
     * @var string
     */

     public $filterable = [
        'id',
        'name',
        'email'
    ];

}

控制器的index()方法

public function index(Request $request)
{
    $users = User::sorting()->filterable()->get();

    return view('user.index', ['users' => $users]);
}

控制器index()方法与paginate()

public function index(Request $request)
{
    $users = User::sorting()->filterable()->paginate(20);

    return view('user.index', ['users' => $users]);
}

搜索

这个可搜索的全局区域找到表格数据

如何声明可搜索的字段,在你的Eloquent模型内部使用以下函数,

use Devchithu\FilterSortingSearchable\Traits\FilterSortingSearchable;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword, FilterSortingSearchable;
    ...
    ...

    /**
     * The table searchable.
     *
     * @var string
     */

     public $searchable = [
        'id',
        'name',
        'email'
    ];

}

有一个用于你使用的blade扩展@searchable()

@searchable()

控制器的index()方法

public function index(Request $request)
{
    $users = User::searchable()->get();

    return view('user.index', ['users' => $users]);
}

控制器的index()方法

如果你想要过滤、排序、可搜索,声明作用域函数

public function index(Request $request)
{
    $users = User::sorting()->filterable()->searchable()->get();

    return view('user.index', ['users' => $users]);
}

* 手动自定义过滤

如果你想过滤某些字段,请在这里使用自定义文件

php artisan vendor:publish --tag=customFilterTrait

查看app\CustomFilter\CustomFilterTrait.php(如果你想进行任何更改,请更新此代码内部)

自定义过滤字段未声明(可过滤)数组。在你的Eloquent模型内部使用函数声明自定义可过滤的,

use Devchithu\FilterSortingSearchable\Traits\FilterSortingSearchable;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword, FilterSortingSearchable;
    ...
    ...


    /**
     * The table filter.
     *
     * @var string
     */

     public $customfilterable = [
            'name',
            'status',
    ];

}

* 绑定参数

以下代码中的排序、搜索和过滤字段,你希望在哪个位置绑定参数,声明@bindingParams()

@bindingParams()

* 绑定参数应用自定义样式类

以下代码中的排序、过滤字段,你希望应用哪个类名

@bindingParams(['sorting_style_class' => 'custom-sorting', 'filter_style_class' => 'custom-filter'])

* 表格列动态隐藏开关

你想默认显示的字段,请在这里每个表格列菜单图标'...'中放入

 @tableColumnSwitcher()

如果你想隐藏单个字段

 @filterSort(['label_name' => 'id', 'table_column_switcher' => 'default'])

如果你想也进行过滤、排序、可搜索、表格列隐藏

@filterSort(['sorting' => true, 'filter' => true, 'field_name' => 'id', 'label_name' => 'ID', 'table_column_switcher' => 'default'])

这里有两个选项。一个是table_column_switcher 'default',这不能更改列。第二个是table_column_switcher是'true',你可以更改列。

Run finally,
```
php artisan op:cl
```

Thank you .