Appearance
Form 表单
form表单作为组件容器,输入框, 下拉选择, 多选框,Table表格 等用户输入的组件等都作为 form 子组件。
基本表单
<template>
<dn-page>
<dn-form ref="formRef" v-bind="formOptions"></dn-form>
</dn-page>
</template>
<script lang="jsx">
import {ref,defineComponent} from "vue";
import { useForm } from "@sdevnet/devnet-form";
import {dn} from "@sdevnet/devnet-form";
const formRef = ref();
const formOptions = ref();
function createFormOptions() {
const { buildFormOptions } = useForm();
return buildFormOptions({
columns: {
fn1: {
title: "文本框",
type: "text",
form: {
component:{
placeholder:'文本框'
},
rules: [{ required: true, message: "此项必填1" }],
col:{span:24},
tooltip:'Tooltip输入提示'
}
},
fn2: {
title: "数字框3",
type: "number",
form: {
col:{span:24},
component:{
precision:2,
step:0.01
},
props:{
defaultValue:'123'
}
}
},
fnhr:{
type:'split',
form:{
props:{
defaultValue:'分割线'
}
}
},
fn3:{
title:'多行文本',
type:'textarea',
form:{
col:{span:24},
component:{
rows:3
},
props:{
defaultValue:'多行文本框'
}
}
},
fn4: {
title: "复选框",
type: "checkboxlist",
form: {
rules: [{ required: true, message: "必选项" }],
col:{span:24},
tooltip:{
text:'必须勾选一项',
iconColor:'red',
position:'label'
},
props:{
options:[
{
text:'复选框1',
id:'1'
},
{
text:'复选框2',
id:'2'
},
{
text:'复选框3',
id:'3'
}
],
events:{
onChange:(scope)=>{
console.log('out',scope);
}
}
}
}
},
fn5: {
title: "下拉框",
type: "select",
form: {
col:{span:24},
props:{
//selectIndex:1, //默认选择第二项
options:[
{
text:'下拉框1',
id:'1',
name:'abc'
},
{
text:'下拉框2',
id:'2'
},
{
text:'下拉框3',
id:'3'
}
],
events:{
onChange:(scope)=>{
console.log('out',scope);
}
}
}
}
}
},
form: {
labelWidth: "120px",
beforeSubmit(formScope){
console.log(formScope)
},
submit({ form }) {
console.log(form);
dn.notification.success("自定义表单提交:" + JSON.stringify(form));
},
onValueChange:(e)=>{
console.log(e);
},
toolbar:{
show:true,
position:'bottom',
buttons:{
add:{
show:false
},
save:{
text:'提交',
onClick:(e)=>{
formSubmit();
}
},
reset:{
text:'重置',
onClick:(e)=>{
formReset();
}
}
}
}
}
});
}
function formSubmit() {
formRef?.value?.submit();
}
function formReset() {
dn.notification.success("重置...");
formRef?.value?.reset();
}
export default defineComponent({
setup() {
const initFormOptions = () => {
formOptions.value = createFormOptions();
formOptions.value.initData = { fid:1,fn1: "初始值",fn4:'1,3',fn5:'2' };
return {
formOptions,
formRef,
formSubmit,
formReset,
};
}
return {
...initFormOptions()
};
}
});
</script>Form API
| PropsName | Description | Type | Default |
|---|---|---|---|
| columns | 表单字段 | json | |
└ key | 字段名称表单字段类型 | json | |
| form | 表单配置 | json | |
└ key | 原生el-form组件属性,参考原生form组件 | ||
| └ row | row 配置 | json | |
| └ col | 列配置 | object | |
| └ watch | watch监听数据 | ||
| └ beforeValidate | 点击保存按钮,表单校验前执行操作(async) | ||
| └ validateError | 验证失败 | ||
| └ beforeSubmit | 表单校验完完成后,提交前处理 | ||
| └ submit | 点击保存按钮时执行操作 | ||
| └ afterSubmit | 表单提交后处理 | ||
| └ autoRender | 自动布局,false时可以自定义模板布局 | boolean | true |
| └ slots | 插槽内容 | ||
| └ toolbar/bottombar | 表单工具栏 | json | |
| └└ show | 是否显示 | boolean | false |
| └└ title | 标题 | string | |
| └└ subTitle | 子标题 | string | |
| └└ subTitleClass | 子标题class | string | |
| └└ position | 工具栏位置 top / bottom | string | |
| └└ buttons | 按钮 | json | |
└└└ add | 新增 | json | onClick需要自定义实现 |
| └└└└└ show | 是否显示 | boolean | false |
└└└ delete | 删除 | json | onClick需要自定义实现 |
| └└└└ show | 是否显示 | boolean | false |
└└└ save | 保存 | json | 默认实现 |
| └└└└ show | 是否显示 | boolean | false |
└└└ close | 关闭 | json | 默认实现 |
| └└└└ show | 是否显示 | boolean | false |
└└└ key | 按钮key | json | |
└└└└ key | 按钮配置,参考button | json | |
| └└└└ debounce | 按钮防抖属性 | boolean | false |
| └└└└ delay | 按钮防抖延迟 | number | 2000 |
| └ dataBind | 数据绑定 | any | |
| └└ sortStr | 排序 | string | |
| └└ where | 条件 | WhereCompare[] | |
| └└ dataSource | 数据源(预留) | string | |
| └ group | 组件分组 | object | |
| └└ groupType | 分组类型 | tabs card collapse layout | |
└└ key | 分组类型原生组件属性 | ||
| └└ groups | 分组项 | json | |
└└└ key | 组key | json | |
| └└└└ label | 组标题 | string | |
| └└└└ value | 组value,同key | string | |
| └└└└ columns | 组内字段名称 ["fn1","fn2"] | ||
| └└└└ toolbar | 组内顶部工具栏 | json | |
| └└└└└ title | 组内顶部标题 | string | |
| └└└└└ subTitle | 组内顶部子标题 | string | |
| └└└└└ subTitleClass | 组内顶部子标题class | string | |
| └└└└└ buttons | 组内顶部按钮 | json | |
└└└└└└ key | 组内顶部按钮key | json | |
└└└└└└└ key | 组内顶部按钮配置,参考原生el-button属性 | json | |
| └└└└└└└ debounce | 按钮防抖属性 | boolean | false |
| └└└└└└└ delay | 按钮防抖延迟 | number | 500 |
Form 事件
| Event name | Description |
|---|---|
| success | 提交成功后 |
| value-change | 更改字段值 |
Form Slots
| Name | Description |
|---|---|
| formHeader | 表单顶部 |
| formFooter | 表单底部 |
Form Exposes
| Name | Description | Type |
|---|---|---|
| formRef | 原生表单 form 引用 | |
| getComponentRef | 获取子组件引用 | func |
| getFormData | 获取表单数据 | true:包含子表数据,并已进行 submitValueConvert 函数转换 |
| getValue | 获取字段数据 | func |
| setValue | 设置字段数据 | func |
| children | 子表formExpose(暂表格和树) | |
| isChanged | 是否更改 | |
| saveData | 保存表单数据 |