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();