Преглед на файлове

fix:修改金额组件去除value参数,返回数字类型

liuzl преди 2 години
родител
ревизия
ec3573187f
променени са 3 файла, в които са добавени 36 реда и са изтрити 32 реда
  1. 33 17
      src/components/currency/index.vue
  2. 2 9
      src/views/contract/components/Edit.vue
  3. 1 6
      src/views/contract/components/ProductTable.vue

+ 33 - 17
src/components/currency/index.vue

@@ -2,16 +2,15 @@
  * @Author: liuzhenlin 461480418@qq.ocm
  * @Date: 2023-01-10 16:18:28
  * @LastEditors: liuzhenlin
- * @LastEditTime: 2023-01-10 17:49:42
+ * @LastEditTime: 2023-01-17 17:44:24
  * @Description: file content
- * @FilePath: \订单全流程管理系统\src\components\amountInput\index.vue
+ * @FilePath: \订单全流程管理系统\src\components\currency\index.vue
 -->
 <template>
   <div>
     <el-input
       v-bind="$attrs"
       v-model.trim="num"
-      oninput="value=value.replace(/[^0-9.]/g,'')"
       @blur="inputMoney($event)"
       @change="handleChange"
       @focus="uninputMoney($event)"
@@ -33,23 +32,19 @@
         num: '',
       }
     },
-    watch: {
-      value(val) {
-        this.num = this.formatPrice(val)
-      },
-    },
-
     mounted() {
       this.num = this.formatPrice(this.value)
     },
 
     methods: {
       returnNum() {
-        this.$emit('input', this.delcommafy(this.num))
+        if (this.num) {
+          this.$emit('input', this.CurrencyFomatNumber(this.num))
+        }
       },
       handleChange(val) {
         this.$nextTick(() => {
-          this.$emit('change', this.delcommafy(val))
+          this.$emit('change', this.CurrencyFomatNumber(val))
         })
       },
       // 失焦显示数字类型
@@ -60,17 +55,38 @@
       // 获得焦点金额去掉格式
       uninputMoney(el) {
         if (el.target.value) {
-          this.num = this.delcommafy(el.target.value)
+          this.num = this.CurrencyFomatNumber(el.target.value)
         } else {
           this.num = null
         }
       },
       //去除千分位中的‘,’
-      delcommafy(num) {
-        if (!num) return num
-        num = num.toString().replace('¥', '').replace(/,/gi, '')
-        if (num.indexOf('.00') > 0) num = parseInt(num)
-        return num
+      CurrencyFomatNumber(num, n = 2) {
+        let number = num.replace('¥', '')
+        if (number != null && number != '' && number != undefined) {
+          number = number.replace(/,/g, '') //去除千分位的','
+          if (isNaN(number)) {
+            //判断是否是数字
+            number = '0'
+          } else {
+            number = Math.round(number * Math.pow(10, n)) / Math.pow(10, n) //n幂
+            number = number.toString()
+          }
+        } else {
+          number = '0'
+        }
+        //a.indexOf(x,y);返回x值在a字符串值中从y位置开始检索首次出现的位置
+        var numLength = number.indexOf('.')
+        //判断传递的值是整数增加小数点再补"0"
+        if (numLength < 0) {
+          numLength = number.length
+          number += '.'
+        }
+        //不足n位小数的,循环补"0"
+        while (number.length <= numLength + n) {
+          number += '0'
+        }
+        return parseFloat(number)
       },
     },
   }

+ 2 - 9
src/views/contract/components/Edit.vue

@@ -435,7 +435,7 @@
           prodNum: item.count,
           maintTerm: 1,
           sugSalesPrice: item.guidPrice,
-          tranPrice: this.delcommafy(item.price),
+          tranPrice: item.price,
           remark: '',
         }))
         let params = Object.assign({ ...this.editForm }, { product })
@@ -454,7 +454,7 @@
           prodNum: item.count,
           maintTerm: 1,
           sugSalesPrice: item.guidPrice,
-          tranPrice: this.delcommafy(item.price),
+          tranPrice: item.price,
           remark: '',
         }))
         let params = Object.assign({ ...this.editForm }, { product })
@@ -489,13 +489,6 @@
         this.businessUserQueryParams = {}
         this.$refs.editForm.resetFields()
       },
-      delcommafy(num) {
-        if (!num) return 0
-        num = num.toString()
-        num = num.replace(/,/gi, '')
-        if (num.indexOf('.00') > 0) num = parseInt(num)
-        return Number(num)
-      },
     },
   }
 </script>

+ 1 - 6
src/views/contract/components/ProductTable.vue

@@ -18,12 +18,7 @@
         show-overflow-tooltip>
         <template #default="{ row }">
           <span v-if="item.prop == 'price'">
-            <amount-input
-              v-model.trim="row.price"
-              placeholder="请输入金额"
-              size="mini"
-              :value="row.price"
-              @change="handleChange(row)" />
+            <amount-input v-model.trim="row.price" placeholder="请输入金额" size="mini" @change="handleChange(row)" />
           </span>
           <span v-else-if="item.prop == 'count'">
             <el-input