06-字段控件映射.md 6.0 KB

字段控件映射

用户说「XX 字段」时,按下表直接落成控件 + 校验 + 列渲染,不用再问。


一、类型对照表

用户说的类型 表单控件 必传属性 列表列
文本 / 名称 / 编码 <a-input> :maxlength="50"、autocomplete="off"、placeholder ellipsis: true
长文本 / 备注 / 描述 <a-textarea> :rows="3"、full、:maxlength="255" ellipsis: true,或不显示
数字 / 排序 / 数量 <a-input-number> :precision="0"、:min="0"、style="width: 100%" width: 80、align: 'center'
金额 / 价格 <a-input-number> :precision="2"、:min="0"、:step="0.01" width: 120、align: 'right'
百分比 <a-input-number> :precision="2"、:min="0"、:max="100" align: 'right',后缀 %
日期 <a-date-picker> value-format="YYYY-MM-DD"、style="width: 100%" width: 120、align: 'center'
日期时间 <a-date-picker> show-time、value-format="YYYY-MM-DD HH:mm:ss" width: 180
日期区间 <a-range-picker> value-format="YYYY-MM-DD"、style="width: 100%" 拆成两个字段或 ellipsis
时间 <a-time-picker> value-format="HH:mm:ss" width: 100
字典下拉 <a-select> + mapByDictTypes :allowClear、:showSearch、:filterOption="$utils.filterSelectOption" template: true,插槽里显示 label
固定枚举(2~4 个) <a-select> + 手写 <a-select-option> 同上 template: true,配 a-tag
单选 / 是否 <a-radio-group> 选项 ≤ 4 个时用它,不用下拉 template: true,显示文字
开关 / 启用状态 <a-switch> v-model:checked,提交时转 1 / 0 template: true,配 a-tag
多选 <a-select mode="multiple"> 或 <a-checkbox-group> :maxTagCount="3" 数组 join('、')
组织 / 校区 / 单位 <s-data-range-tree-select> :treeCheckable="false"、:maxTagCount="1";带数据权限加 :isDataScopeFilter="true" ellipsis: true
人员(单选) <s-user-select> 必须 :multiple="false"(默认多选) 显示姓名
人员(多选) <s-user-select> :multiple="true" 姓名 join('、')
人员(按钮式) <s-user-select-button> buttonText、:multiple —
岗位 <s-office-select> 必须显式传 :multiple 显示岗位名
自定义树(分类 / 地区) <s-tree-select> :recursionChildren、:props 显示名称
附件 / 文件 <s-file-upload> path(业务表名)、:maxFileCount、accept 显示「N 个附件」或图标
图片 / 头像 <s-file-upload> accept="image/*"、listType="card"、:maxFileCount="1" 缩略图插槽
富文本 / 正文 <s-editor> v-model、:config="{ minHeight: 300 }"、full 不显示或截断
级联(省市区等) <s-cascader> :options ellipsis: true
密码 <a-input-password> :maxlength="32"、autocomplete="new-password" 不显示

二、校验规则模板

类型 rules
必填文本 { required: true, message: '请输入名称', trigger: 'blur' }
限长 { max: 50, message: '最多 50 个字符', trigger: 'blur' }
必选(下拉 / 日期 / 树 / 选人) { required: true, message: '请选择状态', trigger: 'change' }
手机号 { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
邮箱 { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
正整数 自定义 validator,见 03-组件文档-表单与弹窗.md
数字范围 { type: 'number', min: 0, max: 999999, message: '超出范围', trigger: 'blur' }

文案规则:输入类 请输入XX,选择类 请选择XX,与 label 保持一致。


三、列配置速查

const columns = ref([
  { title: '名称', dataIndex: 'name', ellipsis: true, template: true },  // 需要自定义渲染才加 template
  { title: '编码', dataIndex: 'code', ellipsis: true },
  { title: '状态', dataIndex: 'status', width: 100, align: 'center', template: true },
  { title: '创建时间', dataIndex: 'createTime', width: 180, align: 'center' },
  { title: '操作', dataIndex: 'action', width: 160, align: 'center', fixed: 'right' }
])
  • template: true → 必须配同名插槽 <template #列dataIndex="{ record }">,插槽名就是 dataIndex。
  • 操作列固定 width: 160 起(每多一个按钮加 60),fixed: 'right'。
  • 长文本列一律 ellipsis: true,不加就直接撑破表格。
  • 时间列统一 width: 180,日期 120,状态 100,排序 80。

四、查询区怎么排

条件 放哪
名称 / 编码类模糊查询 #filter,a-input + @pressEnter="paramChange"
状态 / 类型下拉 #filter,a-select + @change="paramChange"
日期区间 #filterMore(放更多里,别占主行)
组织 / 分类 主查询区用 s-data-range-tree-select,或做成左侧树
是否包含下级 #filterMore,a-checkbox + :isNormal="false"
超过 4 个条件 前 3~4 个放 #filter,其余全放 #filterMore

每个查询控件都要绑 @change(输入框用 @pressEnter)触发 paramChange,它内部调 reload()。 重置时要把 queryParam 整体 Object.assign 回初始值,再 reload()。


五、字段命名与提交转换

场景 规则
组织树返回值 组件返回数组,表单里存 officeIds,提交前转标量 officeId = officeIds[0]
开关 表单用布尔 status: true,提交转 1 / 0;回显 !!res.status
选人 只存 id 不够,展示用的姓名另外存 xxxName 字段(@change 里一并赋值)
多选 提交数组,不要 join(','),除非后端明确要字符串
附件 提交 `fileList.map(i => i.imageUrl
后端返回前缀 is isEnable 类似字段按布尔处理,!! 转换