关于动态的新增输入框和删除

可以根据需要增加输入框和删除输入框
这里写图片描述


<template>
    <div>
        <div style="z-index: 10; display: block">
            <el-form :model="repairWorkHour" ref="repairWorkHour" label-width="100px" class="demo-dynamic">
                <el-col :span="6">
                    <el-form-item
                            v-for="(repairWorker, index) in repairWorkHour.repairWorkers"
                            :label="'域名' + index"
                            :key="repairWorker.id"
                            :prop="'repairWorkers.' + index + '.id'"
                            :rules="{
                              required: true, message: 'id不能为空', trigger: 'blur'
                            }"
                    >
                        <el-input v-model="repairWorker.id"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item
                            v-for="(repairWorker, index) in repairWorkHour.repairWorkers"
                            :label="'域名' + index"
                            :key="repairWorker.id"
                            :prop="'repairWorkers.' + index + '.name'"
                            :rules="{
                              required: true, message: 'name不能为空', trigger: 'blur'
                            }"
                    >
                        <el-input v-model="repairWorker.name"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item
                            v-for="(repairWorker, index) in repairWorkHour.repairWorkers"
                            :label="'域名' + index"
                            :key="repairWorker.id"
                            :prop="'repairWorkers.' + index + '.remark'"
                            :rules="{
                              required: true, message: '备注不能为空', trigger: 'blur'
                            }"
                    >
                        <el-input v-model="repairWorker.remark"></el-input>
                        <el-button @click.prevent="removeDomain(repairWorker)"  icon="delete"></el-button>
                    </el-form-item>
                </el-col>
            </el-form>
        </div>
        <div >
            <el-button type="primary" @click="submitForm('repairWorkHour')">提交</el-button>
            <el-button @click="addDomain" icon="plus"></el-button>
            <el-button @click="resetForm('repairWorkHour')">重置</el-button>
        </div>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                repairWorkHour: {
                    repairWorkers: [
                        {id: '', name: '', remark: '',},
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            removeDomain(item) {
                console.log('---------');
                console.log(item);
                var index = this.repairWorkHour.repairWorkers.indexOf(item)
                if (index !== -1) {
                    this.repairWorkHour.repairWorkers.splice(index, 1)
                }
            },
            addDomain() {
                this.repairWorkHour.repairWorkers.push({
                    id: '',
                    name: '',
                    remark: '',
                });
                console.log(this.repairWorkHour.repairWorkers);
            }
        }
    }
</script>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值