189 8069 5689

利用element-ui编写一个表单金额输入框-创新互联

这篇文章给大家介绍利用element-ui编写一个表单金额输入框,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

我们提供的服务有:成都网站建设、做网站、微信公众号开发、网站优化、网站认证、开福ssl等。为上千多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的开福网站制作公司

组件代码如下:


  },
  rules: {
   type: Object,
   default: () => { },
  },
 },
 data () {
  return {
   showFormatPrice: false, // 是否显示遮罩
  }
 },
 computed: {
  formaterPrice () {
   if (
    this.form.deceivedAmount !== '' &&
    this.form.deceivedAmount !== null
   ) {
    // 去掉前面的0
    const integer = this.form.deceivedAmount.split('.')[0]
    const decimal = this.form.deceivedAmount.split('.')[1]
     ? `.${this.form.deceivedAmount.split('.')[1]}`
     : ''
    return `${integer
     .toString()
     .replace(/(?=(?!^)(\d{3})+$)/g, ',')}${decimal}`
   } else {
    return ''
   }
  },
 },
 methods: {
  // 聚焦金额输入框
  focusInput () {
   this.showFormatPrice = false
   this.$refs.input.focus()
  },
  // 失焦金额输入框
  blurInput () {
   if (this.form.deceivedAmount !== '') {
    // 去掉前面的0
    const integer = Number(this.form.deceivedAmount.split('.')[0])
    const decimal = this.form.deceivedAmount.split('.')[1]
     ? `.${this.form.deceivedAmount.split('.')[1]}`
     : ''
    this.form.deceivedAmount = isNaN(`${integer}${decimal}`)
     ? this.form.deceivedAmount
     : `${integer}${decimal}`
    if (typeof this.rules[this.prop][0].pattern !== 'object') {
     throw `请确保 rules[${this.prop}][0].pattern 为正则表达式`
     return
    }
    this.showFormatPrice = this.rules[this.prop][0].pattern.test(
     this.form.deceivedAmount,
    )
   }
  },
 },
}



.price-mask {
 position: absolute;
 z-index: 2;
 top: 1px;
 left: 125px;
 background: white;
 width: 110px;
 overflow: auto;
 font-size: 13px;
}

在表单中的使用方法其实和你直接写一个el-form-item 的效果是一样的,直接引入即可。

// 使用方法:

el-form(:model="form" ref="form" label="180px" :label-suffix="':'" :rules="rules")
  priceInput(:form.sync = "form" :width = "150" label = "金额" prop = "deceivedAmount" :rules = "rules")


关于利用element-ui编写一个表单金额输入框就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


名称栏目:利用element-ui编写一个表单金额输入框-创新互联
分享网址:http://cdxtjz.cn/article/dicsih.html

其他资讯