ElementPlus下的form也有新增表單 如果你寫H5等沒找到合適的 自己也可以進行封裝
實現(xiàn)3個代碼講解:1:ElementPlus的代碼 2:自己書寫的代碼 3:自己把2的代碼進行封裝
1:ElementPlus的運行效果
點擊提交
1:ElementPlus的代碼
<template>
<el-form
ref="formRef"
:model="dynamicValidateForm"
label-width="120px"
class="demo-dynamic"
>
<el-form-item
prop="email"
label="Email"
:rules="[
{
required: true,
message: 'Please input email address',
trigger: 'blur',
},
{
type: 'email',
message: 'Please input correct email address',
trigger: ['blur', 'change'],
},
]"
>
<el-input v-model="dynamicValidateForm.email" />
</el-form-item>
<el-form-item
v-for="(domain, index) in dynamicValidateForm.domains"
:key="domain.key"
:label="'Domain' + index"
:prop="'domains.' + index + '.value'"
:rules="{
required: true,
message: 'domain can not be null',
trigger: 'blur',
}"
>
<el-input v-model="domain.value" />
<el-button class="mt-2" @click.prevent="removeDomain(domain)"
>Delete</el-button
>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm(formRef)">提交</el-button>
<el-button @click="addDomain">新增</el-button>
<el-button @click="resetForm(formRef)">重置</el-button>
</el-form-item>
</el-form>
</template>
<script lang="ts" setup>
import { reactive, ref } from "vue";
import type { FormInstance } from "element-plus";
const formRef = ref<FormInstance>();
const dynamicValidateForm = reactive<{
domains: DomainItem[];
email: string;
}>({
domains: [
{
key: 1,
value: "",
},
],
email: "",
});
interface DomainItem {
key: number;
value: string;
}
const removeDomain = (item: DomainItem) => {
const index = dynamicValidateForm.domains.indexOf(item);
if (index !== -1) {
dynamicValidateForm.domains.splice(index, 1);
}
};
const addDomain = () => {
dynamicValidateForm.domains.push({
key: Date.now(),
value: "",
});
};
const submitForm = (formEl: FormInstance | undefined) => {
if (!formEl) return;
formEl.validate((valid) => {
if (valid) {
console.log("submit!");
console.log(dynamicValidateForm, "dynamicValidateForm");
} else {
console.log("error submit!");
return false;
}
});
};
const resetForm = (formEl: FormInstance | undefined) => {
if (!formEl) return;
formEl.resetFields();
};
</script>
2:自己書寫的代碼
點擊提交的打印效果
<template>
<div class="box">
<template v-for="(item, index) in dataForm" :key="item.id">
<div
class="box-b bg-ff"
style="display: flex; justify-content: space-between"
>
<div>
<div style="display: flex">
<div class="">區(qū)域位置</div>
<input v-model="item.ipchange" type="text" />
</div>
<div style="display: flex; margin-top: 10px">
<div class="">危險有害因素</div>
<input type="text" v-model="item.errorText" />
</div>
</div>
<div>
<el-button type="primary" @click="deleteClick(item.id)"
>刪除</el-button
>
</div>
</div>
</template>
<div>
<el-button type="primary" @click="handlerClick">新增</el-button>
<el-button type="primary" @click="subMitClick">提交</el-button>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
const dataForm = reactive([
{ id: 1, ipchange: "", errorText: "" },
{ id: 2, ipchange: "", errorText: "" },
]);
const handlerClick = () => {
dataForm.push({ id: Date.now(), ipchange: "", errorText: "" });
};
const deleteClick = (id: number) => {
dataForm.forEach((item, index) => {
item.id == id ? dataForm.splice(index, 1) : "";
});
};
const subMitClick = () => {
console.log(dataForm, "dataForm");
};
</script>
<style scoped>
.bg-ff {
background: #fff;
}
.box {
width: 30vw;
margin: auto;
background: #fff;
}
.box-b {
padding: 10px;
width: 100%;
overflow: hidden;
box-sizing: border-box;
border-radius: 10px;
border: 1px solid #000;
margin-top: 10px;
}
</style>
2的代碼量太多 我們都會進行封裝
3:自己把2的代碼進行封裝
components/insertForm.vue
<template>
<div>
<template v-for="(item, index) in NewdataForm" :key="item.id">
<div
class="box-b bg-ff"
style="display: flex; justify-content: space-between"
>
<div>
<div style="display: flex">
<div class="">區(qū)域位置</div>
<input v-model="item.ipchange" type="text" />
</div>
<div style="display: flex; margin-top: 10px">
<div class="">危險有害因素</div>
<input type="text" v-model="item.errorText" />
</div>
</div>
<div>
<el-button type="primary" @click="deleteClick(item.id)"
>刪除</el-button
>
</div>
</div>
</template>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, watch } from "vue";
const emit = defineEmits(["deleteClick"]);
const define = withDefaults(defineProps<{ dataForm: any[] }>(), {
dataForm: () => [
{ id: 1, ipchange: "", errorText: "" },
{ id: 2, ipchange: "", errorText: "" },
],
});
//1: 這樣不可以的; 下面的watch監(jiān)聽進行賦值的時候 NewdataForm=newvalue不可整體的賦值這個
// reactive 只能賦值到它下的某個屬性下
//1: let errors = reactive( [...define.dataForm] );
// 2:但是使用這個 太麻煩
// 2:let NewdataForm = reactive({ dataForm: [...define.dataForm] });
// 3:使用 ref這個 可以直接賦值
let NewdataForm = ref([...define.dataForm]);
watch(define.dataForm, (newvalue) => {
// errors=newvalue //錯誤的寫法 如上1:
// NewdataForm.dataForm=newvalue;可以正常運行如上2:
NewdataForm.value = newvalue;
});
const deleteClick = (id: number) => {
emit("deleteClick", id);
};
defineExpose({ NewdataForm });
</script>
<style scoped>
</style>
主文件引入
<template>
<div class="box">
<div>
<insertForm
ref="insertFormRef"
:dataForm="dataForm"
@deleteClick="deletenewClick"
></insertForm>
<el-button type="primary" @click="handlerClick">新增</el-button>
<el-button type="primary" @click="subMitClick">提交</el-button>
</div>
</div>
</template>
<script setup lang="ts">
import insertForm from "@/components/insertForm/index.vue";
import { ref, reactive } from "vue";
const insertFormRef = ref<InstanceType<typeof insertForm>>();
var dataForm = ref([
{ id: 1, ipchange: "", errorText: "" },
{ id: 2, ipchange: "", errorText: "" },
]);
const handlerClick = () => {
dataForm.value.push({ id: Date.now(), ipchange: "", errorText: "" });
};
const deletenewClick = (id: number) => {
dataForm.value.forEach((item, index) => {
item.id == id ? dataForm.value.splice(index, 1) : "";
});
};
const subMitClick = () => {
dataForm.value = insertFormRef.value.NewdataForm;
console.log(dataForm.value, "dataForm");
};
</script>
<style >
.bg-ff {
background: #fff;
}
.box {
width: 30vw;
margin: auto;
background: #fff;
}
.box-b {
padding: 10px;
width: 100%;
overflow: hidden;
box-sizing: border-box;
border-radius: 10px;
border: 1px solid #000;
margin-top: 10px;
}
</style>
效果一樣文章來源:http://www.zghlxwxcb.cn/news/detail-561783.html
*文章來源地址http://www.zghlxwxcb.cn/news/detail-561783.html
大多數(shù)人的疑問 我新增的input的vmodel綁定的值 和之前的表單一樣的 ,都是ipchange: “”, errorText: “” 就是這樣的 發(fā)給后端后端自己判斷即可
到了這里,關(guān)于vue3,elementPlus和自己封裝,點擊 新增添加表單,刪除表單,提交數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!