主页

fastadmin的表格数据逗号分隔的id关联展示数据名称

清盏涂墨衣   IP属地: 青海
0 点赞 0阅读 1983
使用fastadmin的扩展函数

附加关联列表数据 addtion

问题:把关联的逗号分隔数字ID关联表展示名称

代码示例

public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['admin','store'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
                    $fields = [
                        [
                        'field'=>'store_ids',//关联数据字段
                        'primary'=>'id',//关联表主键
                        'display'=>'house_type_config_name',//附加字段名称
                        'column'=>'name',//关联表中读取需要显示的字段,显示所有用"*"
                        // 'model'=>\app\common\model\store'',//关联模型
                        //  'name'=>'Houseconfig',//模型名称,比table字段少一个前缀
                        'table'=>'h_store',//关联表,关联表和关联模型二选一
                        // 'table'=>'h_house_config',//关联表,关联表和关联模型二选一
                        // 'type'=>'hasOne',//关联类型
                        // 'foreignKey'=>'house_config_id',//关联外键
                        // 'fields'=>['name']
                        ]
                    ];
                    // 关联查询逗号
                    addtion($list->items(), $fields);

            foreach ($list as $row) {
                
                $row->getRelation('admin')->visible(['id','username','nickname']);
				$row->getRelation('store')->visible(['id','name','cover']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

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

效果:

按上面的代码新增了一个house_type_config_name字段,在js添加这个字段显示即可

© 著作权归作者所有,转载或内容合作请联系作者