一.普通下拉框

{ label: '状态', prop: 'status', render: 'tag', replaceValue: { '0': '正常', '1': '已停用', '2':'已注销'} }
- 3.将状态封装为配置 (必须加上render)
- 创建目录
web\src\config\status
- 添加文件
empUserStatus.ts
import empUserStatus from '/@/config/status/empUserStatus'
{ label: t('empUser.status'), prop: 'status', render: 'tag', replaceValue: empUserStatus }
二.远程下拉
- 1.CURD代码生成

1.前端代码 (重点)
- 1.1 首页 :
show:false 关闭筛选字段在表格列的显示, operator: false关闭表格列在筛选中的显示
import { goodsCategory } from '/@/api/controllerUrls'
{ label: '分类', prop: 'category_id', comSearchRender: 'remoteSelect', show:false, remote: {
pk: 'id',
field: 'title',
remoteUrl: goodsCategory +'index'
}},
{ label: t('goods.category__title'), prop: 'category.title', operator: false },
- 1.2 表单(表单验证暂时取消掉, 这里也可以导入URL)
<FormItem
:label="t('goods.category_id')"
type="remoteSelect"
v-model="baTable.form.items!.category_id"
prop="category_id"
:input-attr="{ pk: 'ba_goods_category.id', field: 'title', 'remote-url': '/admin/GoodsCategory/index' }"
/>
以下部分其实是关联表数据(获取category__title,也就是分类名), 而非真正的远程下拉, 真正的远程下拉是上面前端部分的代码
2.1 后端控制器
namespace app\admin\controller;
use Throwable;
use app\common\controller\Backend;
class Goods extends Backend
{
protected object $model;
protected array|string $preExcludeFields = ['id'];
protected array $withJoinTable = ['category', 'seller'];
protected string|array $quickSearchField = ['id', 'sn'];
public function initialize(): void
{
parent::initialize();
$this->model = new \app\admin\model\Goods;
}
public function index(): void
{
if ($this->request->param('select')) {
$this->select();
}
list($where, $alias, $limit, $order) = $this->queryBuilder();
$res = $this->model
->withJoin($this->withJoinTable, $this->withJoinType)
->alias($alias)
->where($where)
->order($order)
->paginate($limit);
$res->visible(['category' => ['title'], 'seller' => ['username']]);
$this->success('', [
'list' => $res->items(),
'total' => $res->total(),
'remark' => get_route_remark(),
]);
}
}
2.2 后端模型 (使用关联模型实现, 查询数据)
<?php
namespace app\admin\model;
use think\Model;
class Goods extends Model
{
protected $name = 'goods';
protected $autoWriteTimestamp = false;
public function category(): \think\model\relation\BelongsTo
{
return $this->belongsTo(\app\admin\model\GoodsCategory::class, 'category_id', 'id');
}
public function seller(): \think\model\relation\BelongsTo
{
return $this->belongsTo(\app\admin\model\Admin::class, 'seller_id', 'id');
}
}
{
"code": 1,
"data": {
"list": [
{
"id": 12345,
"sn": "DEMO00001",
"status": 1,
"type": 1,
"title": "iPhone 15 pro max",
"new_product_id": 123,
"category": {
"title": "手机"
},
"seller": null
}
],
"total": 1,
}
}
三.其他
- 1.开关
- 值为0=>关闭, 1=>开启
- 页面上点击切换开关, 使用的是edit接口
- 对应的筛选是下拉
{ label: t('goods.is_open'), prop: 'is_open', render:'switch' },