• fastadmin列表页展示分类名称通用搜索按分类名称搜索


    test控制器中添加

    1. /**
    2. * 查看
    3. *
    4. * @return string|Json
    5. * @throws \think\Exception
    6. * @throws DbException
    7. */
    8. public function index()
    9. {
    10. $this->relationSearch = true;
    11. //设置过滤方法
    12. $this->request->filter(['strip_tags', 'trim']);
    13. if (false === $this->request->isAjax()) {
    14. return $this->view->fetch();
    15. }
    16. //如果发送的来源是 Selectpage,则转发到 Selectpage
    17. if ($this->request->request('keyField')) {
    18. return $this->selectpage();
    19. }
    20. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
    21. $list = $this->model
    22. ->with("category")
    23. ->field('test.*,category.name as category_id')
    24. ->where($where)
    25. ->order($sort, $order)
    26. ->paginate($limit);
    27. $result = ['total' => $list->total(), 'rows' => $list->items()];
    28. return json($result);
    29. }
    30. public function searchlist()
    31. {
    32. $categoryList = collection(\app\common\model\Category::where(['status'=>'normal'])->select())->toArray();
    33. \fast\Tree::instance()->init($categoryList);
    34. $searchlist = [];
    35. $result = \fast\Tree::instance()->getTreeList(\fast\Tree::instance()->getTreeArray(0));
    36. foreach ($result as $k => $v) {
    37. $searchlist[] = ['id' => $v['id'], 'name' => $v['name']];
    38. }
    39. return json($searchlist);
    40. }

    test的model中添加

    1. public function category()
    2. {
    3. return $this->belongsTo('\app\admin\model\Category', 'category_id')->setEagerlyType(0);
    4. }

    在model中添加分类模型 Category.php

    1. namespace app\admin\model;
    2. use think\Model;
    3. class Category extends Model
    4. {
    5. // 表名
    6. protected $name = 'category';
    7. // 自动写入时间戳字段
    8. protected $autoWriteTimestamp = 'int';
    9. // 定义时间戳字段名
    10. protected $createTime = 'createtime';
    11. protected $updateTime = 'updatetime';
    12. // 追加属性
    13. protected $append = [
    14. 'status_text'
    15. ];
    16. public function getStatusList()
    17. {
    18. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    19. }
    20. public function getStatusTextAttr($value, $data)
    21. {
    22. $value = $value ? $value : $data['status'];
    23. $list = $this->getStatusList();
    24. return isset($list[$value]) ? $list[$value] : '';
    25. }
    26. }

    test的js中添加

    {field: 'category_id', title: __('Category_id'),searchList:$.getJSON("/JLqcEmZpeT.php/test/test/searchlist")},

    展示效果

     

  • 相关阅读:
    GBase 8c 管理平台(一)-部署配置
    编译后的go程序无法在alpine基础镜像创建的容器运行问题
    SpringCloud之注册中心
    关于Java CyclicBarrier reset的理解
    linux硬盘分区和挂载
    记录工作中常用的 JS 数组相关操作
    纸牌博弈问题
    卷发棒上亚马逊美国站UL检测怎么办理
    图像运算和图像增强七
    计算机网络——数据链路层の选择题整理
  • 原文地址:https://blog.csdn.net/withoutfear/article/details/126836925