iview、element 添加动态表单列,可以删除、重置

需求背景

在有些需求背景下,我们需要动态增加多字段列,满足现有数据格式,并且可以对数据列进行删除,形成数组对象集合。请参考下图!

在这里插入图片描述
废话不多说,上干货 code ,自行补充自己所需的逻辑及拓展

<template>
    <Form ref="formDynamic" :model="formDynamic" :label-width="80" >
      <div  v-for="(item, index) in formDynamic.items" :key="index">
            <Row>
              <Col span="6">
                <FormItem  label="字段名:" :prop="'items.' + index + '.value'">
                  <Input type="text" v-model="item.value" ></Input>
                </FormItem>
              </Col>
              <Col span="6">
                <FormItem  label="查询类型:" :prop="'items.' + index + '.value'">
                  <Input type="text" v-model="item.value" ></Input>
                </FormItem>
              </Col>
              <Col span="6">
                <FormItem  label="类型值:" :prop="'items.' + index + '.value'">
                  <Input type="text" v-model="item.value" ></Input>
                </FormItem>
              </Col>
                <Col span="6" >
                    <Button style="margin-left:10px" @click="handleRemove(index)" v-if="index>0">删除</Button>
                </Col>
            </Row>
      </div>
        <FormItem>
            <Row>
                <Col span="4">
                    <Button type="dashed" long @click="handleAdd" icon="md-add">添加</Button>
                </Col>
            </Row>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formDynamic')">查询</Button>
            <Button @click="handleReset('formDynamic')" style="margin-left: 8px">重置</Button>
        </FormItem>
    </Form>
</template>
<script>
    export default {
      data () {
        return {
          index: 1,
          formDynamic: {
            items: [
              {
                value: '',
                index: 1
              }
            ]
          }
        }
      },
      methods: {
        handleSubmit (name) {
          console.log(this.formDynamic, '提交的数据')
          this.$refs[name].validate((valid) => {
            if (valid) {
              this.$Message.success('Success!')
            } else {
              this.$Message.error('Fail!')
            }
          })
        },
        handleReset (name) {
          this.$refs[name].resetFields()
        },
        handleAdd () {
          if (this.formDynamic.items.length > 2) {
            this.$Message.info('添加上限')
          } else {
            this.index++
            this.formDynamic.items.push({
              value: '',
              index: this.index
            })
          }
        },
        handleRemove (index) {
          if (index !== -1) {
            this.formDynamic.items.splice(index, 1)
          }
        }
      }
    }
</script>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值