当前位置: 首页 > news >正文

fastadmin表格数据逗号分隔的id关联展示数据名称并实现搜索

记录:fastadmin表格数据逗号分隔的id关联展示数据名称并实现搜索

1,在查询后增加显示代码


$fields = [['field'=>'tags_ids', //关联数据字段'display'=>'tags_name', //附加的字段名称'primary'=>'id', //关联表主键'column'=>'tags_name', //关联表中读取需要显示的字段'model'=>'', //关联模型'table'=>'fa_litestore_tags' //关联表,关联表和关联模型二选一]
];
addtion($list,$fields); //list是数组
// addtion($list->items(),$fields); // list是对象

2,增加搜索功能代码

// 特殊搜索处理
$wheres = [];
$filter = json_decode($this->request->get('filter'), true);
$op = json_decode($this->request->get('op'), true);
if(!empty($filter['tags_name'])){$keyword = $filter['tags_name'];$tagsIds = implode(',', Db::name('litestore_tags')->where(['tags_name'=>['like',"%$keyword%"]])->column('id'));$rex = explode(',', $tagsIds);foreach ($rex as $v){$wheres[] = ['exp',Db::raw("FIND_IN_SET('$v', tags_ids)")];}unset($filter['tags_name']);unset($op['tags_name']);
}
$this->request->get(['filter'=>json_encode($filter)]);
$this->request->get(['op'=>json_encode($op)]);

3,完整示例

/*** 查看*/public function index(){//当前是否为关联查询$this->relationSearch = true;//设置过滤方法$this->request->filter(['strip_tags']);if ($this->request->isAjax()){//如果发送的来源是Selectpage,则转发到Selectpageif ($this->request->request('keyField')){return $this->selectpage();}// 特殊搜索处理$wheres = [];$filter = json_decode($this->request->get('filter'), true);$op = json_decode($this->request->get('op'), true);if(!empty($filter['tags_name'])){$keyword = $filter['tags_name'];$tagsIds = implode(',', Db::name('litestore_tags')->where(['tags_name'=>['like',"%$keyword%"]])->column('id'));$rex = explode(',', $tagsIds);foreach ($rex as $v){$wheres[] = ['exp',Db::raw("FIND_IN_SET('$v', tags_ids)")];}unset($filter['tags_name']);unset($op['tags_name']);}$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(['category'])    ->where($where)->where($wheres)->where(['sign'=>'10'])->where(['is_delete'=>0])->order($sort, $order)->count();$list = $this->model->with(['category'])    ->where($where)->where($wheres)->where(['sign'=>'10'])->where(['is_delete'=>0])->order($sort, $order)->limit($offset, $limit)->select();$fields = [['field'=>'tags_ids', //关联数据字段'display'=>'tags_name', //附加的字段名称'primary'=>'id', //关联表主键'column'=>'tags_name', //关联表中读取需要显示的字段'model'=>'', //关联模型'table'=>'fa_litestore_tags' //关联表,关联表和关联模型二选一]];addtion($list,$fields); //list是数组// addtion($list->items(),$fields); // list是对象foreach ($list as &$row) {$row->getRelation('category')->visible(['name']);}$list = collection($list)->toArray();$result = array("total" => $total, "rows" => $list);return json($result);}return $this->view->fetch();}

图示:
在这里插入图片描述

相关文章:

  • 基于Versoria函数优化协方差更新的改进扩展卡尔曼滤波(MVC-EKF)与经典EKF的对比,附matlab源代码|订阅专栏后可查看完整代码
  • ERP知识有价值,更有温度!
  • react-sequence-diagram时序图组件
  • Java大厂面试实录:从Spring Boot到AI微服务架构的全栈挑战
  • 系统性能优化-7 TCP 四次挥手
  • 【请关注】制造企业机械加工数据脱敏解决方案
  • QGIS导出Shape文件
  • matplotlib 绘制热力图
  • uniapp中表格固定列(Vue)
  • 《游戏元素创世法则:从原子到虚拟生命的全链路解析》—— 网格/刚体/纹理/材质/骨骼/蒙皮/光照/渲染的深度关联指南
  • 广东广电U点-创维E900-S-海思MV310芯片-海兔线刷烧录固件包
  • 带标签的 Docker 镜像打包为 tar 文件
  • 策略模式与工厂模式的黄金组合:从设计到实战
  • C++并发编程-4.unique_lock,共享锁和递归锁
  • 关于Kotlin与Java的思考
  • 通过ETL从MySQL同步到GaussDB
  • Linux工作常用命令记录
  • Android开发获取视图组件的findViewById,kotlin-android-extensions,ViewBinding三种详解
  • PyWavelets
  • 分布式系统ID生成方案深度解析:雪花算法 vs UUID vs 其他主流方案