vue使用computed和watch监听实现表单控件的联动效果

vue: 直接使用iview的相关控件

1、template:提前安装iview,主要使用iview相关控件

<template>
  <div class="test">
    <Button @click="add">增加</Button>
    <Modal v-model="Model"
           :closable="false"
           :mask-closable="false">
      <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
        <FormItem label="报销金额" prop="reimburseAmount">
          <Input v-model="formValidate.reimburseAmount" style="width: 320px" placeholder="请输入报销金额..."></Input>
        </FormItem>
        <FormItem label="税率 ( % )" prop="taxRate" :rules="{ required: true, message: '税率不能为空,请输入'}">
          <InputNumber
            :max="100"
            :min="1"
            v-model="formValidate.taxRate"
            style="width: 320px;"
            placeholder="请输入税率..."
            :formatter="value => `${value}%`"
            :parser="value => value.replace('%', '')">
          </InputNumber>
        </FormItem>
        <FormItem label="税额" prop="taxAmount" :rules="{ required: true, message: '税额不能为空且小数点后只能保留2位,请输入'}">
          <InputNumber
            v-model="formValidate.taxAmount"
            style="width: 320px"
            placeholder="请输入税额...">
          </InputNumber>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button  @click="Cancel">取消</Button>
        <Button type="primary" @click="Submit('formValidate')">确定</Button>
      </div>
    </Modal>
  </div>
</template>

2、data:主要定义需要用到的一些属性

// 验证报销金额
    const validateReimburseAmount = (rule, value, callback) => {
      let reg = /^\d+(?:\.\d{1,2})?$/
      if (!reg.test(value)) {
        callback(new Error('报销金额只能保留两位小数,请重新输入'))
      } else {
        callback()
      }
    }
    return {
      Model: false,
      formValidate: {
        reimburseAmount: 0,
        taxRate: 0,
        taxAmount: 0
      },
      ruleValidate: {
        reimburseAmount: [
          { required: true, message: '报销金额不能为空,请输入', trigger: 'blur' },
          {validator: validateReimburseAmount, trigger: 'blur'}
        ]
      }
    }

3、computed :计算当前控件的值

   reimburseAmounts () {
      return this.formValidate.reimburseAmount
    },
    taxRates () {
      return this.formValidate.taxRate
    },
    taxAmounts () {
      return this.formValidate.taxAmount
    }

4、watch : 监听报销金额和税率变化的时候,得到的税额

   reimburseAmounts (n, o) {
      // console.log("reimburseAmounts", n, o)
      this.formValidate.taxAmount = n * (this.formValidate.taxRate / 100)
      this.formValidate.taxAmount = parseFloat(this.formValidate.taxAmount.toFixed(2))
    },
    taxRates (n, o) {
      // console.log("taxRates", n, o)
      this.formValidate.taxAmount = this.formValidate.reimburseAmount * (n / 100)
      this.formValidate.taxAmount = parseFloat(this.formValidate.taxAmount.toFixed(2))
    },
    taxAmounts (n, o) {
      // console.log("taxAmounts", n, o)
      this.formValidate.taxAmount = parseFloat(this.formValidate.taxAmount.toFixed(2))
    }

5、methods : 一些操作方法

 add: function () {
      let self = this
      self.Model = true
    },
    Cancel: function () {
      let self = this
      self.Model = false
    },
    Submit: function (name) {
      let self = this
      console.log(self.formValidate.taxAmount)
    }

具体操作可自行测试。

最后,谢谢我最亲爱的后援。MR LU

转载于:https://my.oschina.net/yj1993/blog/1821795

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值