多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 上图: 个人、推广员、商业用户都是在用户表里面的一个type字段,当前订单表通过user_id关联了用户表。 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210116121036694.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210116121010526.png)![在这里插入图片描述](https://img-blog.csdnimg.cn/2021011612110368.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/2021011612113176.png) ### 第一步: 在`_initialize`方法里面加上 `$this->view->assign("newList", $this->model->getNewList());` `getNewList` 方法如下,其实就是自定义的 ```php public function getNewList(){ return ['person' => __('个人'), 'genera' => __('推广员'), 'business' => __('商业用户')]; } ``` 然后再对应的Html下面加上这个 ```html <div class="panel-heading"> {:build_heading(null,FALSE)} <ul class="nav nav-tabs" data-field="user_id"> <li class="active"><a href="#t-all" data-value="" data-toggle="tab">{:__('All')}</a></li> {foreach name="newList" item="vo"} <li><a href="#t-{$key}" data-value="{$key}" data-toggle="tab">{$vo}</a></li> {/foreach} </ul> </div> ``` ### 第二步 在对应的js里面的index方法里面加上这个: ```javascript // 绑定TAB事件 $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) { var field = $(this).closest("ul").data("field"); var value = $(this).data("value"); var options = table.bootstrapTable('getOptions'); options.pageNumber = 1; options.queryParams = function (params) { var filter = {}; if (value !== '') { filter[field] = value; } params.filter = JSON.stringify(filter); return params; }; table.bootstrapTable('refresh', {}); return false; }); // 为表格绑定事件 Table.api.bindevent(table); ``` ### 第三步就是接收数据处理数据了 `buildparams`方法主要是过滤掉了 `filter `字段,改为:`$filter = '{}';`,不去接收get参数 ```php $c=null; $t=$this->request->param(); if(isset($t['filter'])){ if($t['filter']!='{}'){ $c=$t['filter']; } } ``` 上面这段代码主要是为了提前接收到传值 `filter`,并且做好保存,加上`$where2`查询条件作为新条件的处理。 ```php $where2=array(); if($c){ $type=(array)json_decode($c,true)['user_id']; $user=new \app\admin\model\User(); $ids=$user->where(['type'=>$type[0]])->column('id'); $where2=['user_id'=>['in',$ids]]; } ``` 上面这段代码核心思想 `where in` ,想必到这里基本上就能解决问题了,代码再骚,问题解决才是关键。 下面贴出来完整的index方法跟buildparams两个方法,讲解在上面: ```php /** * 查看 */ public function index() { // $this->relationSearch=true; //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage $c=null; $t=$this->request->param(); if(isset($t['filter'])){ if($t['filter']!='{}'){ $c=$t['filter']; } } if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $where2=array(); if($c){ $type=(array)json_decode($c,true)['user_id']; $user=new \app\admin\model\User(); $ids=$user->where(['type'=>$type[0]])->column('id'); $where2=['user_id'=>['in',$ids]]; } $total = $this->model ->with(['user','maindata']) ->where($where) ->where($where2) ->order($sort, $order) ->count(); $list = $this->model ->with(['user','maindata']) ->where($where) ->where($where2) ->order($sort, $order) ->limit($offset, $limit) ->select(); $list = collection($list)->toArray(); $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } public function buildparams($searchfields = null, $relationSearch = null) { $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields; $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch; $search = $this->request->get("search", ''); $filter = '{}'; $op = $this->request->get("op", '', 'trim'); $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id'); $order = $this->request->get("order", "DESC"); $offset = $this->request->get("offset/d", 0); $limit = $this->request->get("limit/d", 999999); //新增自动计算页码 $page = $limit ? intval($offset / $limit) + 1 : 1; if ($this->request->has("page")) { $page = $this->request->get("page/d", 1); } $this->request->get([config('paginate.var_page') => $page]); $filter = (array)json_decode($filter, true); $op = (array)json_decode($op, true); $filter = $filter ? $filter : []; $where = []; $alias = []; $bind = []; $name = ''; $aliasName = ''; if (!empty($this->model) && $this->relationSearch) { $name = $this->model->getTable(); $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model)))); $aliasName = $alias[$name] . '.'; } $sortArr = explode(',', $sort); foreach ($sortArr as $index => & $item) { $item = stripos($item, ".") === false ? $aliasName . trim($item) : $item; } unset($item); $sort = implode(',', $sortArr); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $where[] = [$aliasName . $this->dataLimitField, 'in', $adminIds]; } if ($search) { $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields); foreach ($searcharr as $k => &$v) { $v = stripos($v, ".") === false ? $aliasName . $v : $v; } unset($v); $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"]; } $index = 0; foreach ($filter as $k => $v) { if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) { continue; } $sym = isset($op[$k]) ? $op[$k] : '='; if (stripos($k, ".") === false) { $k = $aliasName . $k; } $v = !is_array($v) ? trim($v) : $v; $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym); //null和空字符串特殊处理 if (!is_array($v)) { if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) { $sym = strtoupper($v); } if (in_array($v, ['""', "''"])) { $v = ''; $sym = '='; } } switch ($sym) { case '=': case '<>': $where[] = [$k, $sym, (string)$v]; break; case 'LIKE': case 'NOT LIKE': case 'LIKE %...%': case 'NOT LIKE %...%': $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"]; break; case '>': case '>=': case '<': case '<=': $where[] = [$k, $sym, intval($v)]; break; case 'FINDIN': case 'FINDINSET': case 'FIND_IN_SET': $v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v)); $findArr = array_values($v); foreach ($findArr as $idx => $item) { $bindName = "item_" . $index . "_" . $idx; $bind[$bindName] = $item; $where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)"; } break; case 'IN': case 'IN(...)': case 'NOT IN': case 'NOT IN(...)': $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)]; break; case 'BETWEEN': case 'NOT BETWEEN': $arr = array_slice(explode(',', $v), 0, 2); if (stripos($v, ',') === false || !array_filter($arr)) { continue 2; } //当出现一边为空时改变操作符 if ($arr[0] === '') { $sym = $sym == 'BETWEEN' ? '<=' : '>'; $arr = $arr[1]; } elseif ($arr[1] === '') { $sym = $sym == 'BETWEEN' ? '>=' : '<'; $arr = $arr[0]; } $where[] = [$k, $sym, $arr]; break; case 'RANGE': case 'NOT RANGE': $v = str_replace(' - ', ',', $v); $arr = array_slice(explode(',', $v), 0, 2); if (stripos($v, ',') === false || !array_filter($arr)) { continue 2; } //当出现一边为空时改变操作符 if ($arr[0] === '') { $sym = $sym == 'RANGE' ? '<=' : '>'; $arr = $arr[1]; } elseif ($arr[1] === '') { $sym = $sym == 'RANGE' ? '>=' : '<'; $arr = $arr[0]; } $tableArr = explode('.', $k); if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) { //修复关联模型下时间无法搜索的BUG $relation = Loader::parseName($tableArr[0], 1, false); $alias[$this->model->$relation()->getTable()] = $tableArr[0]; } $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr]; break; case 'NULL': case 'IS NULL': case 'NOT NULL': case 'IS NOT NULL': $where[] = [$k, strtolower(str_replace('IS ', '', $sym))]; break; default: break; } $index++; } if (!empty($this->model)) { $this->model->alias($alias); } $model = $this->model; $where = function ($query) use ($where, $alias, $bind, &$model) { if (!empty($model)) { $model->alias($alias); $model->bind($bind); } foreach ($where as $k => $v) { if (is_array($v)) { call_user_func_array([$query, 'where'], $v); } else { $query->where($v); } } }; return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind]; } ```