test控制器中添加
- /**
- * 查看
- *
- * @return string|Json
- * @throws \think\Exception
- * @throws DbException
- */
- public function index()
- {
- $this->relationSearch = true;
-
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if (false === $this->request->isAjax()) {
- return $this->view->fetch();
- }
- //如果发送的来源是 Selectpage,则转发到 Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- [$where, $sort, $order, $offset, $limit] = $this->buildparams();
-
- $list = $this->model
- ->with("category")
- ->field('test.*,category.name as category_id')
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
-
- $result = ['total' => $list->total(), 'rows' => $list->items()];
- return json($result);
- }
-
- public function searchlist()
- {
- $categoryList = collection(\app\common\model\Category::where(['status'=>'normal'])->select())->toArray();
- \fast\Tree::instance()->init($categoryList);
- $searchlist = [];
- $result = \fast\Tree::instance()->getTreeList(\fast\Tree::instance()->getTreeArray(0));
- foreach ($result as $k => $v) {
- $searchlist[] = ['id' => $v['id'], 'name' => $v['name']];
- }
- return json($searchlist);
- }
test的model中添加
- public function category()
- {
- return $this->belongsTo('\app\admin\model\Category', 'category_id')->setEagerlyType(0);
- }
在model中添加分类模型 Category.php
-
- namespace app\admin\model;
-
- use think\Model;
-
- class Category extends Model
- {
-
- // 表名
- protected $name = 'category';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [
- 'status_text'
- ];
-
- public function getStatusList()
- {
- return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
- }
-
- public function getStatusTextAttr($value, $data)
- {
- $value = $value ? $value : $data['status'];
- $list = $this->getStatusList();
- return isset($list[$value]) ? $list[$value] : '';
- }
-
- }
test的js中添加
{field: 'category_id', title: __('Category_id'),searchList:$.getJSON("/JLqcEmZpeT.php/test/test/searchlist")},
展示效果
