记录:fastadmin 列表页实现一对多关联的搜索
PHP代码
在这里插入图片描述

    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            
            // 一对多关联搜索处理
            $filter = json_decode($this->request->get('filter'), true);
            $op = json_decode($this->request->get('op'), true);
            if(!empty($filter['goods.goods_name'])){
                $keyword = $filter['goods.goods_name'];
                $orderIds = Db::name('litestore_order_goods')->where(['goods_name'=>['like',"%$keyword%"]])->group('order_id')->column('order_id');
                $filter['id'] = $orderIds;
                $op['id'] = 'in';
                unset($filter['goods.goods_name']);
            }
            if(!empty($filter)){
                $this->request->get(['filter'=>json_encode($filter)]);
                $this->request->get(['op'=>json_encode($op)]);
            }
            
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['address','goods'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['address','goods'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            $wxmoney = $this->model->where($where)->where(['paytype'=>2])->sum('pay_money');
            $alimoney = $this->model->where($where)->where(['paytype'=>1])->sum('pay_money');
            $wxnum = $this->model->where($where)->where(['paytype'=>2])->count();
            $alinum = $this->model->where($where)->where(['paytype'=>1])->count();
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list, "extend" => ['wxmoney' => $wxmoney, 'alimoney' => $alimoney,'wxnum' => $wxnum, 'alinum' => $alinum]);

            return json($result);
        }
        return $this->view->fetch();
    }

JS代码
在这里插入图片描述

{field: 'goods.goods_name', title: __('商品'), formatter:function(val,row){
  var tip = '';
    $.each(row.goods,function(index,info){
    // row.goods.forEach(function(info,index){
        if(tip){
            tip +=
                '<hr style="margin-top:0;margin-bottom:0;border-top: 1px solid #6ad4bf;">';
        }
        tip += 
        '<div style="text-align:left">'+
            '<div>'+
                '<span title="'+info.goods_name+'">'+info.goods_name+'</span>';
        if(info.goods_attr){
            tip +=
                '<br>'+
                '<span>规格:'+info.goods_attr+'</span>';
        }
        tip += 
            '</div>'+
        '</div>';
    })
    return tip;
},
cellStyle : function(value, row, index, field){
    return {
        css: {
            "white-space": "nowrap",
            "text-overflow": "ellipsis",
            "overflow": "hidden",
            "max-width":"200px"
        }
    };
}},

效果如下:
在这里插入图片描述

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐