117 lines
2.8 KiB
Vue
117 lines
2.8 KiB
Vue
<template>
|
||
<div>
|
||
<div
|
||
style="font-size: 2rem"
|
||
class="flex flex-warp justify-center items-center font-bold"
|
||
>
|
||
<div style="font-size: 1rem">请输入要导入的数据数量:</div>
|
||
<el-input-number v-model="singleFormNum" :min="1" :max="10" @change="createForm" />
|
||
<div v-if="checkNeed"> </div>
|
||
<el-checkbox
|
||
v-if="checkNeed"
|
||
v-model="checked"
|
||
label="为所有表单进行相同操作"
|
||
size="large"
|
||
/>
|
||
</div>
|
||
<el-scrollbar max-height="40rem">
|
||
<div class="mt-2 border-2 border-gray-300" v-for="item in input">
|
||
<div style="font-size: 1.2rem" class="w-full text-center font-bold">
|
||
{{ item.label }}
|
||
</div>
|
||
<SingleAdd ref="singleAddRef" :addData="addData" />
|
||
</div>
|
||
</el-scrollbar>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup>
|
||
import { ref, watch } from "vue";
|
||
import { ElMessage } from "element-plus";
|
||
|
||
import SingleAdd from "./singleAdd.vue";
|
||
|
||
const singleFormNum = ref(1);
|
||
const checked = ref(true);
|
||
const input = ref([{ label: "多选题1" }]);
|
||
const singleAddRef = ref(null);
|
||
const formClone = ref([]);
|
||
|
||
const props = defineProps({
|
||
checkNeed: Boolean,
|
||
addData: Object,
|
||
});
|
||
|
||
const emit = defineEmits(["addMultiple"]);
|
||
|
||
const initData = () => {
|
||
setTimeout(() => {
|
||
singleAddRef.value.forEach((item) => {
|
||
item.ruleForm.role = singleAddRef.value[0].ruleForm.role;
|
||
});
|
||
}, 100);
|
||
};
|
||
|
||
const createForm = () => {
|
||
let difference = Math.abs(singleFormNum.value - input.value.length);
|
||
for (let i = 0; i < difference; i++) {
|
||
if (singleFormNum.value > input.value.length) {
|
||
input.value.push({ label: `多选题${input.value.length + 1}` });
|
||
} else if (singleFormNum.value < input.value.length) {
|
||
input.value.pop();
|
||
}
|
||
}
|
||
if (checked.value) {
|
||
initData();
|
||
}
|
||
};
|
||
|
||
const resetCheckForm = () => {
|
||
if (!singleAddRef) return;
|
||
singleAddRef.value.forEach((element) => {
|
||
element.resetCheckForm();
|
||
});
|
||
singleFormNum.value = 1;
|
||
createForm();
|
||
};
|
||
|
||
const confirm = () => {
|
||
if (!singleAddRef) return;
|
||
let valid = true;
|
||
formClone.value = [];
|
||
singleAddRef.value.forEach((element) => {
|
||
element.validate().then((result) => {
|
||
if (result) {
|
||
formClone.value.push({ ...element.ruleForm });
|
||
} else {
|
||
valid = result;
|
||
}
|
||
});
|
||
});
|
||
setTimeout(() => {
|
||
if (valid) {
|
||
emit("addMultiple", formClone.value);
|
||
singleAddRef.value.forEach((item) => {
|
||
item.resetCheckForm();
|
||
});
|
||
ElMessage({
|
||
message: `提交成功!`,
|
||
type: "success",
|
||
});
|
||
} else {
|
||
ElMessage.error("提交失败!");
|
||
}
|
||
}, 100);
|
||
};
|
||
|
||
defineExpose({ resetCheckForm, confirm, formClone });
|
||
|
||
watch(checked, (value) => {
|
||
if (value) {
|
||
initData();
|
||
}
|
||
});
|
||
</script>
|
||
|
||
<style scoped></style>
|