iview中如何动态增减表单项

这是一个使用Vue.js编写的组件,展示了一个服务选择和管理的模态对话框。用户可以添加、删除服务,并为每个服务选择名称和设置单价。当用户点击确定时,会进行表单验证,确保所有字段完整。组件中使用了Less进行样式定义,以及Element UI的Select和Input组件。
摘要由CSDN通过智能技术生成
<style lang="less" scoped>
.text {
  font-size: 16px;
  font-weight: 700;
}
.icon{
  margin-left: 10px;
  cursor: pointer;
}
</style>

<template>
  <div>
    <Modal v-model="showCare" footer-hide width="600">
      <p class="text">选择服务</p>
      <Form :label-width="100" ref="form" :model="formDynamic">
        <Row style="margin-bottom:20px;">
          <Col :offset="18">
            <Button type="primary" ghost @click="choose">添加</Button>
          </Col>
        </Row>
        <Row v-for="(item,key) in formDynamic.service" :key="key" v-if="item.status">
          <Col span="11">
            <FormItem label="服务名称" :rules="{required: true, message: '必填项', trigger: 'change'}" :prop="'service.'+key+'.serviceName'">
              <Select v-model="item.serviceName" clearable filterable>
                <Option v-for="(item,index) in serviceNameList" :value="item" :key="index">{{item}}</Option>
              </Select>
            </FormItem>
          </Col>
          <Col span="11">
            <FormItem label="单价" :rules="{required: true,type:'number', message: '必填项', trigger: 'blur'}" :prop="'service.'+key+'.unitPrice'">
              <Input v-model="item.unitPrice" clearable type="number" number></Input>
            </FormItem>
          </Col>
          <Col span="1">
            <Icon type="md-close-circle" size="30" class="icon" @click="remove(key)"></Icon>
          </Col>
        </Row>
        <Row>
          <Col :offset="15">
            <Button type="primary" ghost @click="cancel">取消</Button>
            <Button type="primary" style="margin-left:30px;" @click="ascertain">确定</Button>
          </Col>
        </Row>
      </Form>
    </Modal>
  </div>
</template>

<script>
export default {
  data(){
    return {
      
      showCare:false,
      serviceNameList:["文章","门诊","问诊","住院","视频讲座","现场讲座"],
      formDynamic:{
        service:[
          {
            serviceName:"",
            unitPrice:"",
            status:1,
          }
        ],
      }
    }
  },
  methods:{
    cancel(){
      this.showCare = false
      this.formDynamic.service = []
    },

    ascertain(){
      this.$refs.form.validate(valid=>{
        if(valid){

        }else {
          this.$Message.error({
            background:true,
            content:"请填写完整!"
          })
        }
      })
    },

    remove(index){
      this.formDynamic.service.splice(index,1)  
    },

    choose(){
      let obj = {
        serviceName:"",
        unitPrice:"",
        status:1,
      }
      this.formDynamic.service.push(obj)
    }
  }
}
</script>

效果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值