网站首页 编程语言 正文
封装组件
<template>
<el-form
class="supervise-detail__form"
ref="baseForm"
:rules="baseRules"
:model="baseForm"
label-position="top"
>
<el-row :gutter="32">
<el-col
v-for="item in baseFormList"
:key="item.key"
:xl="item.xl"
:lg="item.lg"
:md="item.md"
:sm="item.sm"
>
<el-form-item v-if="!item.notShow" :class="item.class" :prop="item.key">
<template slot="label">
{{ item.label }}
<!-- 提示 -->
<el-popover
v-if="item.tipsType"
popper-class="supervise-detail__tips"
placement="bottom"
trigger="hover"
>
<div v-if="item.tipsType === 'text'" style="max-width: 600px">
{{ item.tips }}
</div>
<div v-else-if="item.tipsType === 'table'" style="max-width: 600px">
<el-table
stripe
tooltip-effect="light"
:cell-style="getCellStyle"
:data="baseForm[item.tipsKey]"
style="width: 100%;">
<el-table-column
show-overflow-tooltip
v-for="tableItem in item.tipsHeader"
:key="tableItem.key"
:prop="tableItem.key"
:label="tableItem.label"
:width="tableItem.width || 'auto'"
:align="tableItem.algin || 'left'"
>
<template slot-scope="scope">
{{ scope.row[tableItem.key] }}
</template>
</el-table-column>
</el-table>
</div>
<svg-icon
style="margin-left: 5px;"
slot="reference"
class="icon"
icon-class="infoTips"
/>
</el-popover>
</template>
<!-- {{ item.key }}----
{{ baseForm[item.key] }} -->
<!-- 输入框 -->
<el-input
:disabled="item.disabled ? true : false"
v-emoji
:maxlength="item.maxLength || 500"
v-if="item.type === 'input'"
v-model.trim="baseForm[item.key]"
:placeholder="`请输入${item.label}`"
>
</el-input>
<!-- 下拉选 -->
<el-select
:disabled="item.disabled ? true : false"
v-else-if="item.type === 'select'"
v-model="baseForm[item.key]"
:filterable="item.isFilterable"
:multiple="item.isMultiple"
:placeholder="`请选择${item.label}`"
collapse-tags
@change="(val) => changeSelect(val, item, baseForm)"
>
<el-option
v-for="optionItem in item.optionList"
:key="optionItem.value"
:label="optionItem.label"
:value="optionItem.value"
></el-option>
</el-select>
<!-- 远程选择(员工) -->
<el-select
:disabled="item.disabled ? true : false"
v-else-if="item.type === 'selectRemote'"
v-model="baseForm[item.key]"
:filterable="item.isFilterable"
:remote="item.isRemote"
:multiple="item.isMultiple"
:placeholder="`请输入${item.label}`"
:remote-method="(query) => getRemoteOption(query, item)"
:loading="item.loading"
@focus="getRemoteOption('', item)"
@change="(str) => changeUserSelect(str, item, baseForm)"
>
<div slot="prefix">
<i class="el-icon-search"></i>
</div>
<el-option
v-for="optionItem in item.optionList"
:key="optionItem.loginId"
:label="optionItem.userName"
:value="optionItem.loginId"
>
{{ `${optionItem.userName} - ${optionItem.workCode}` }}
</el-option>
</el-select>
<el-input
v-emoji
disabled
class="addKey"
:maxlength="item.maxLength || 500"
v-if="item.addKey"
v-model.trim="baseForm[item.addKey.label.subLable]"
>
</el-input>
<!-- 时间选择 -->
<el-date-picker
:disabled="item.disabled ? true : false"
v-else-if="item.type === 'date'"
v-model="baseForm[item.key]"
type="date"
:editable="false"
:clearable="false"
value-format="yyyy-MM-dd"
:placeholder="`请选择${item.label}`"
>
</el-date-picker>
<!-- 级联 -->
<el-cascader
:disabled="item.disabled ? true : false"
v-else-if="item.type === 'cascader'"
:props="{
checkStrictly: item.isCheckStrictly,
multiple: item.isMultiple,
}"
collapse-tags
v-model="baseForm[item.key]"
:options="item.optionList"
@change="(val) => changeDepSelect(val, item, baseForm)"
:placeholder="`请选择${item.label}`"
>
</el-cascader>
<!-- 文本域 -->
<el-input
:disabled="item.disabled ? true : false"
:maxlength="item.maxLength || 500"
show-word-limit
v-emoji
v-else-if="item.type === 'textarea'"
type="textarea"
:rows="2"
:autosize="{
minRows: 3,
}"
v-model.trim="baseForm[item.key]"
:placeholder="`请输入${item.label}`"
>
</el-input>
<!-- 附件上传 -->
<el-upload
:class="{hideBtn: item.hideBtn}"
v-else-if="item.type === 'uploadFile'"
class="upload"
accept="*"
:disabled="item.disabled || uploading"
:action="uploadUrl"
:file-list="baseForm[item.key]"
:before-upload="fileBeforeUpload"
:on-preview="downloadFile"
:on-error="uploadError"
:on-progress="uploadProgress"
:on-remove="(file)=>fileRemove(file, item)"
:on-success="(res) => uploadSuccess(res, item)"
>
<el-button type="primary" :disabled="uploading">
上传附件
</el-button>
<div slot="tip" class="el-upload__tip">
仅支持上传以下类型文件: .pdf,.xls,.xlsx,.doc,.docx,.zip,.png,.jpg,.jpeg,.rar,且单个文件不超过50MB
</div>
</el-upload>
</el-form-item>
</el-col>
</el-row>
</el-form>
</template>
<script>
import { mapGetters, mapActions } from 'vuex';
// import { api } from '@/api';
import { flat } from '@/util/validate';
import myForm from '@/mixins/my-form';
export default {
name: 'my-form',
mixins: [myForm],
components: {},
props: {
// 输入框类型
baseFormList: {
type: Array,
default: () => [],
},
// 输入框选中后的传参
baseForm: {
type: Object,
default: () => {},
},
},
watch: {
baseForm: {
handler(val) {
if (val) {
this.updateForm();
}
},
deep: true,
},
baseFormList: {
// eslint-disable-next-line no-unused-vars
handler(val) {
// console.log('myFrom组件的baseFormList组件类型', val);
},
deep: true,
},
},
data() {
return {
// 校验
baseRules: {},
// 扁平化处理下拉选
depOptions: [],
// 附件上传地址
uploadUrl: `${process.env.VUE_APP_BASE_URL}common/file/upload`,
uploading: false,
};
},
created() {
this.initData();
this.initOption();
},
methods: {
...mapActions(['apiGetDepList']),
// 更新数据
updateForm() {
// 关闭数据后更新依然要刷新下拉框
this.initOption();
this.$emit('update:baseForm', this.baseForm);
},
// 取消校验
closeBaseForm() {
this.$refs.baseForm.resetFields();
},
// 校验必填项
checkValidate() {
const that = this;
console.log(this.baseFormList, this.baseForm);
return new Promise((resolve) => {
this.$refs.baseForm.validate((valid) => {
if (!valid) {
resolve(false);
return false;
}
that.updateForm();
resolve(true);
return true;
});
});
},
// 重置数据
resetForm() {
this.$refs.baseForm.resetFields();
},
/**
* 附件相关
*/
// 上传校验
fileBeforeUpload(file) {
const isLt50M = file.size / 1024 / 1024 < 50;
if (!isLt50M) {
this.$message.error('上传文件大小不能超过 50MB!');
return false;
}
const suffix = file.name.split('.').slice(-1)[0].toLowerCase();
const types = ['pdf', 'xls', 'xlsx', 'doc', 'docx', 'zip', 'png', 'jpg', 'jpeg', 'rar'];
const isRightType = types.includes(suffix);
if (!isRightType) {
this.$message.error('仅支持上传以下类型文件:.pdf|.xls|.xlsx|.doc|.docx|.zip|.png|.jpg|.jpeg|.rar');
return false;
}
return isLt50M && isRightType;
},
// 移除文件
fileRemove(file, item) {
this.baseForm[item.key] = this.baseForm[item.key].filter(
(el) => (el.url !== file.url),
);
},
// 上传中
uploadProgress() {
this.uploading = true;
},
// 上传失败
uploadError(res) {
this.$message.error(res.message);
},
// 上传成功
uploadSuccess(res, item) {
this.uploading = false;
if (res.messageCode === '200') {
this.baseForm[item.key].push({
name: res.data.docName,
...res.data,
});
this.$message({
message: '上传成功',
type: 'success',
});
} else {
this.$message({
message: res.message,
type: 'error',
});
}
},
// 下载文件
downloadFile(file) {
if (file.url) {
const link = document.createElement('a');
link.style.display = 'none';
link.href = file.location;
link.setAttribute('download', file.name);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
},
// 数据初始化
initData() {
// console.log(this.baseForm, this.baseFormList);
// 督办立项单
this.baseFormList.forEach((el) => {
if (el.isMultiple && !el.labelKey) {
this.$set(this.baseForm, el.key, []);
} else if (!el.isMultiple && el.labelKey) {
this.$set(this.baseForm, el.key, '');
this.$set(this.baseForm, el.labelKey, '');
} else if (!el.isMultiple && !el.labelKey) {
this.$set(this.baseForm, el.key, '');
}
if (el.defaultValue) {
this.$set(this.baseForm, el.key, el.defaultValue);
}
if (el.defaultLabelValue) {
this.$set(this.baseForm, el.labelKey, el.defaultLabelValue);
}
if (el.notRequired) {
return false;
}
const rules = [];
const inputType = ['input', 'textarea'];
if (inputType.indexOf(el.type) > -1) {
rules.push({
required: true,
message: `请输入${el.label}`,
trigger: ['change', 'blur'],
});
this.$set(this.baseRules, el.key, rules);
} else {
rules.push({
required: true,
message: `请选择${el.label}`,
trigger: ['change'],
});
this.$set(this.baseRules, el.key, rules);
}
return true;
});
},
// 下拉选初始化
async initOption() {
// 部门处理
const res = await this.apiGetDepList();
// 部门扁平化处理,方便获取名称
this.depOptions = flat(res);
// 部门相关下拉选,在这里可以更改下拉框的重置参数
const dep = ['hostUnitId', 'assistanceUnitId'];
this.baseFormList.forEach((el) => {
if (this.getConditionObj[el.key]) {
this.$set(el, 'optionList', this.getConditionObj[el.key]);
}
if (dep.indexOf(el.key) > -1) {
this.$set(el, 'optionList', res[0].children);
}
});
},
},
computed: {
...mapGetters(['getConditionObj', 'getUserInfo']),
},
};
</script>
使用baseFormList传参
const EDIT_LIST = [{
label: '事项1',
key: 'superviseDesc',
type: 'input',
disabled: true,
optionList: [],
class: '',
xl: 12,
lg: 12,
md: 12,
sm: 12,
}, {
label: '任务1',
key: 'taskName',
type: 'input',
disabled: true,
optionList: [],
class: '',
xl: 12,
lg: 12,
md: 12,
sm: 12,
}, {
label: '申请延期原因',
key: 'delayReason',
type: 'textarea',
optionList: [],
class: '',
xl: 24,
lg: 24,
md: 24,
sm: 24,
}, {
label: ' 延期办结时限',
key: 'delayFinishDate',
type: 'date',
optionList: [],
class: '',
xl: 12,
lg: 12,
md: 12,
sm: 12,
}, {
label: '申请人员',
key: 'creatorName',
type: 'input',
disabled: true,
optionList: [],
class: '',
xl: 12,
lg: 12,
md: 12,
sm: 12,
}, {
label: '申请部门',
key: 'deptName',
type: 'input',
disabled: true,
optionList: [],
class: '',
xl: 12,
lg: 12,
md: 12,
sm: 12,
}, {
label: '申请时间',
key: 'applyDate',
type: 'date',
disabled: true,
optionList: [],
class: '',
xl: 12,
lg: 12,
md: 12,
sm: 12,
}{
label: '附件',
key: 'attachmentList',
type: 'uploadFile',
isMultiple: true,
loading: false,
notRequired: true,
optionList: [],
class: '',
xl: 24,
lg: 24,
md: 24,
sm: 24,
}];
上面是常量
data() {
return {
visible: false,
baseFormList: JSON.parse(JSON.stringify(EDIT_LIST)),
baseForm: {
},
};
},
templeent:
<my-form
ref="myForm"
:baseFormList="baseFormList"
:baseForm="baseForm">
</my-form>
baseForm是每个组件的传入不同展示输入框的数据(如input.value),组件间更新是双向数据绑定的
baseFormList是传入的组件类型数据,如input,下拉选,时间选择器等等
用法,使用常量传入设置不同的组件类型,key是要用到baseForm的返回的数据的字符
其他输入框属性可看elemenet的form的表单属性
原文链接:https://blog.csdn.net/m0_64207574/article/details/130790681
- 上一篇:没有了
- 下一篇:没有了
相关推荐
- 2022-09-23 Python文件目录操作常用模块的使用详解_python
- 2023-02-17 Python去除字符串前后空格的三种方法汇总_python
- 2022-07-11 Docker 安装redis
- 2022-05-17 ubuntu E: 无法获得锁 /var/lib/dpkg/lock-frontend - open
- 2022-11-11 Python深度学习之Keras模型转换成ONNX模型流程详解_python
- 2022-11-29 React 中 memo useMemo useCallback 到底该怎么用_React
- 2022-08-01 go中import包的大坑解决方案_Golang
- 2022-11-11 Vant 3.* 底部安全区适配 部分页面不生效
- 栏目分类
-
- 最近更新
-
- window11 系统安装 yarn
- 超详细win安装深度学习环境2025年最新版(
- Linux 中运行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存储小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基础操作-- 运算符,流程控制 Flo
- 1. Int 和Integer 的区别,Jav
- spring @retryable不生效的一种
- Spring Security之认证信息的处理
- Spring Security之认证过滤器
- Spring Security概述快速入门
- Spring Security之配置体系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置权
- redisson分布式锁中waittime的设
- maven:解决release错误:Artif
- restTemplate使用总结
- Spring Security之安全异常处理
- MybatisPlus优雅实现加密?
- Spring ioc容器与Bean的生命周期。
- 【探索SpringCloud】服务发现-Nac
- Spring Security之基于HttpR
- Redis 底层数据结构-简单动态字符串(SD
- arthas操作spring被代理目标对象命令
- Spring中的单例模式应用详解
- 聊聊消息队列,发送消息的4种方式
- bootspring第三方资源配置管理
- GIT同步修改后的远程分支