Skip to content
网游世界
网游世界

吾生有涯,而知无涯。

  • 首页
  • PHP
    • ThinkPHP
    • FastAdmin
    • webman
  • JavaScript
    • jQuery
    • AdminLTE
  • Free Pascal
  • Java
    • JeeSite
    • 若依
    • ruoyi-vue-pro
  • 其它
    • 操作系统
    • 树莓派
    • 前端
    • Null
  • 关于
网游世界

吾生有涯,而知无涯。

FastAdmin 重置搜索条件

3Vshej, 2024年2月26日 周一2024年2月26日 周一

FastAdmin 列表搜索中,默认设置好条件即可实现搜索。

如,

{field: 'cate_id', title: __('Cate_id'), operate: false},

默认会对 cate_id ,进行等于搜索。但现在的需要对,cate_id 子级进行搜索;也就是说,需要对搜索的值进行二次处理。

建立 /application/common/library/Filter.php 文件:

<?php

namespace app\common\library;


/**
 * 搜索过滤
 * <p>用于 FastAdmin 默认搜索条件处理</p>
 */
class Filter
{

    /**
     * 清除搜索
     * <pre>用于置空 filter op 值</pre>
     */
    public static function clearFilter()
    {
        $request = request();
        $filter  = json_encode([]);
        $op      = json_encode([]);
        $request->get(['filter' => $filter, 'op' => $op]);
    }

    /**
     * 重置搜索参数
     * <p>用于 MyOutput 插件,数据导出</p>
     */
    public static function resetFilter()
    {
        $request = request();
        $filter  = $request->get('filter');
        $search  = $request->get('search', '');
        $op      = $request->get("op", '', 'trim');
        $filter  = (array)json_decode($filter, true);
        $op      = (array)json_decode($op, true);
        if (!$filter) {
            $list = $request->post();
            if (isset($list['search'])) {
                $search = $list['search'];
            }
            unset($list['search']);
            foreach ($list as $field => $val) {

                if (strpos($field, '-operate') !== false || strlen($val) == 0 || !isset($list[$field . '-operate'])) {
                    continue;
                }
                $field          = str_replace('-operate', '', $field);
                $filter[$field] = $val;
                $op[$field]     = trim($list[$field . '-operate']);

            }
        }
        $filter    = $filter ? $filter : [];
        $op        = $op ? $op : [];
        $is_export = isset($filter['is_export']) ? $filter['is_export'] : '';
        unset($filter['is_export'], $op['is_export']);

        $filter = json_encode($filter);
        $op     = json_encode($op);
        $request->get(['filter' => $filter, 'op' => $op, 'search' => $search, 'is_export' => $is_export]);
    }

    /**
     * 重置搜索值
     * @param string      $name      名称
     * @param mixed       $val       值
     * @param string|null $condition 条件(默认=)null时移除
     */
    public static function resetFilterVal($name, $val, $condition = '=')
    {

        $request       = request();
        $filter        = $request->get('filter');
        $op            = $request->get("op", '', 'trim');
        $filter        = (array)json_decode($filter, true);
        $op            = (array)json_decode($op, true);
        $filter[$name] = $val;
        $op[$name]     = $condition;
        if ($condition === null) {
            unset($filter[$name]);
            unset($op[$name]);
        }

        $filter = json_encode($filter);
        $op     = json_encode($op);
        $request->get(['filter' => $filter, 'op' => $op]);
    }

    /**
     * 获取搜索条件值
     * <p>优先从搜索中拿,拿不到,从 param 中获取</p>
     * @param string $name 键名
     * @return mixed
     */
    public static function getFilterVal($name)
    {
        $request = request();
        $filter  = $request->get('filter');
        $filter  = (array)json_decode($filter, true);
        $get     = $request->param($name);
        return !isset($filter[$name]) ? ($get ?: '') : $filter[$name];
    }

    /**
     * 获取搜索条件
     * <p>优先从搜索中拿,拿不到,从 param 中获取</p>
     * @param string $name 键名
     * @return mixed
     */
    public static function getFilterOp($name)
    {
        $request = request();
        $op      = $request->get('op');
        $op      = (array)json_decode($op, true);
        $get     = $request->param($name . '-operate');
        return !isset($op[$name]) ? ($get ?: '') : $op[$name];
    }
}

使用示例:

$cateId = Filter::getFilterVal('cate_id');
if ($cateId) {
    $cateIdsArr = (new \app\admin\model\exam\Cate())->where('id|parent_id', $cateId)->column('id') ?: [0];
    Filter::resetFilterVal('cate_id', array_values($cateIdsArr), 'in');
}

可调用方法:

// 获取搜索值,结果,如 4
Filter::getFilterVal('cate_id');

// 获取查询条件,结果,如 =
Filter::getFilterOp('cate_id');

// 重置查询值,如果第3个参数为 null,则移除这个搜索条件
Filter::resetFilterVal('cate_id', '5', '=');

// 清除所有搜索条件和值
Filter::clearFilter();

相关文章:

  1. JeeSite 重置按钮不生效 测试人员在测试列表搜索时,设置搜索条件,点击“查询”按钮功能正常,点击“重置”按钮时,却没反应。...
  2. JeeSite 通过模板添加列表状态筛选 在 JeeSite 流程表单中,列表中可以通过流程状态来筛选不同状态的流程(审批)信息。...
  3. FastAdmin 添加、编辑页复用 FastAdmin 中,大多情况下,添加页与编辑页内容是相同的,区别在于添加页无默认数据,编辑页需要......
  4. FastAdmin Summernote 清除格式 需求是这样的,录入内容时,大多是从 Word 中粘贴过来的,内容很少,但实际代码却很多,存储时,提示......
FastAdmin PHP JeeSite 框架列表搜索

文章导航

Previous post
Next post

近期文章

  • FreePascal 、Lazarus IDE、Lazarus 包镜像
  • Android Studio Gradle 配置国内镜像
  • 为什么重新发明轮子
  • ruoyi-vue-pro 匿名访问
  • VUE 中接收 code 异常

归档

  • 2025 年 5 月
  • 2025 年 4 月
  • 2025 年 3 月
  • 2025 年 2 月
  • 2025 年 1 月
  • 2024 年 12 月
  • 2024 年 11 月
  • 2024 年 10 月
  • 2024 年 9 月
  • 2024 年 8 月
  • 2024 年 7 月
  • 2024 年 6 月
  • 2024 年 5 月
  • 2024 年 4 月
  • 2024 年 3 月
  • 2024 年 2 月
  • 2024 年 1 月
  • 2023 年 12 月
除非特殊说明,本站作品采用知识共享署名 4.0 国际许可协议进行许可。
豫公网安备 41010402002622号 豫ICP备2020029609号-3
©2025 3Vshej