129 Star 902 Fork 190

剑走偏锋 / dcat-admin

 / 详情

列表页筛选条件记忆功能

Backlog
Opened this issue  
2021-04-06 12:32

有个需求是在列表页筛选条件有记忆功能,搜索后下打开该页自动把之前筛选的条件填充进去。

Comments (3)

wskeee created任务
wskeee set related repository to 剑走偏锋/dcat-admin
wskeee changed title
wskeee changed title
Expand operation logs

参考

<?php

namespace App\Admin;

use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Support\Helper;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;

class RememberFilterInputs
{
    protected $resetKey = '__reset__';

    protected $grid;

    protected $user;

    // 缓存10天
    protected $ttl = 864000;

    protected $name;

    public function __construct(Grid $grid, $name = null)
    {
        $this->grid = $grid;
        $this->user = Admin::user();
        $this->name = $name ?: request()->path();
    }

    public function handle()
    {
        if ($this->except()) {
            return;
        }

        $this->addScript();

        $inputs = $this->getInputs();

        if ($this->canReset() && ! $inputs) {
            $this->reset();

            return;
        }

        $inputs && $this->replace($inputs);

        $this->save($inputs);

        $this->grid->rows(function () {
            $this->grid->filter()->expand(false);
        });

    }

    protected function except()
    {
        return Helper::isAjaxRequest() || request($this->grid->getName().'_export_');
    }

    /**
     * 替换筛选条件
     *
     * @param array $inputs
     */
    protected function replace(array $inputs)
    {
        request()->merge(
            collect($inputs)->mapWithKeys(function ($v, $k) {
                $k = $this->getFilterKeyPrefix().$k;

                return [$k => $v];
            })->toArray()
        );
    }

    /**
     * 重置
     */
    protected function reset()
    {
        request()->merge([$this->resetKey => null]);

        Cache::forget($this->getCacheKey());
    }

    /**
     * 保存筛选条件
     *
     * @param array $inputs
     */
    protected function save(array $inputs)
    {
        Cache::put($this->getCacheKey(), $inputs, $this->ttl);
    }

    /**
     * 判断是否是重置操作
     *
     * @return bool
     */
    protected function canReset()
    {
        return (bool) request($this->resetKey);
    }

    /**
     * 获取过滤器所有值,如果没有则从缓存拉取
     *
     * @return array
     */
    protected function getInputs()
    {
        if ($inputs = $this->all()) {
            return $inputs;
        }

        return (array) Cache::get($this->getCacheKey());
    }

    protected function ignoreFields()
    {
        return [
            '_pjax',
            'page',
            'parent_id',
            '_columns_',
            $this->grid->getName().'_scope_',
            $this->grid->getName().'_search_',
            $this->grid->model()->getSortName(),
            '_campaign_id_',
            '_status_',
        ];
    }

    /**
     * 获取过滤器所有值
     *
     * @return array
     */
    protected function all()
    {
        $all = request()->all();

        Arr::forget($all, $this->ignoreFields());

        if ($this->getFilterKeyPrefix()) {
            $all = array_filter($all, function ($v, $key) {
                return Str::startsWith($key, $this->getFilterKeyPrefix());
            }, ARRAY_FILTER_USE_BOTH);
        }

        return array_filter($all, function ($v) {
            return $v !== null && $v !== '';
        });
    }

    /**
     * 替换重置按钮
     */
    protected function addScript()
    {
        Admin::script(
            <<<JS
$('.filter-button-group .btn').click(function () {
    var key = '{$this->resetKey}';
    
   setTimeout(function () {
       var reset = $('.slider-panel a.reset'), url = reset.attr('href');
       
       if (url.indexOf(key) !== -1) {
           return;
       }
       
       if (url.indexOf('?') === -1) {
           url += '?';
       }
       
       url += '&' + key + '=1';
       
       reset.attr('href', url);
   }, 10);
});
JS
        );
    }

    /**
     * 获取过滤器前缀
     *
     * @return string|void
     */
    protected function getFilterKeyPrefix()
    {
        return $this->grid->getNamePrefix();
    }

    protected function getCacheKey()
    {
        return "rem6-{$this->name}-{$this->user->getKey()}";
    }

    public static function make(Grid $grid)
    {
        return (new static($grid))->handle();
    }
}

用法

RememberFilterInputs::make($grid);

不过发现个小问题,重置按钮不起效,重置不了

亲测可用,厉害!

Sign in to comment

Status
Assignees
Milestones
Pull Requests
Successfully merging a pull request will close this issue.
Branches
Planed to start   -   Planed to end
-
Top level
Priority
参与者(2)
2004009 jqhph 1596708294
PHP
1
https://gitee.com/jqhph/dcat-admin.git
git@gitee.com:jqhph/dcat-admin.git
jqhph
dcat-admin
dcat-admin

Search

141041 ab9339c7 1850385 141043 25c028d5 1850385