Ver código fonte

fix:小数位数同步

ldong 2 meses atrás
pai
commit
796f4331b3

+ 7 - 0
src/views/datasheet_manage/common/customTable.js

@@ -324,6 +324,13 @@ export function isNumberVal(value) {
    return reg.test(value);
 }
 
+/* 判断值是否是一个数字,不包含数字字符串 */
+export function isNumberValue(value) {
+  let reg = /^[-]?(?:\d*\.?\d+|\d+%|\d*\.\d)$/;
+  
+  return reg.test(value);
+}
+
 /* 增加减少小数点位数 */
 export function transDecimalPlace(str,{pn,nt}) {
 

+ 15 - 12
src/views/datasheet_manage/components/BalanceTable.vue

@@ -137,11 +137,7 @@
                           :data-datarindex="index"
                           :data-datacindex="cell_index"
                           :data-key="cell.Uid"
-                          >{{
-                            cell.ShowStyle
-                              ? cell.ShowFormatValue
-                              : cell.ShowValue
-                          }}</span
+                          >{{ isShowDecimal(cell)?showDecimalValue(cell):isShowFormat(cell.ShowStyle)?cell.ShowFormatValue:cell.ShowValue }}</span
                         >
                       </el-popover>
 
@@ -154,7 +150,7 @@
                         :data-datacindex="cell_index"
                         :data-key="cell.Uid"
                       >
-                        {{isShowDecimal(cell)?showDecimalValue(cell):isShowFormat(cell.ShowStyle)?cell.ShowFormatValue:cell.DataTime?cell.ShowValue:[7,6,5].includes(cell.DataType)?cell.ShowValue:cell.Value}}
+                        {{isShowDecimal(cell)?showDecimalValue(cell):isShowFormat(cell.ShowStyle)?cell.ShowFormatValue:cell.DataTime?cell.ShowValue:[8,7,6,5,1].includes(cell.DataType)?cell.ShowValue:cell.Value}}
                       </span>
 
                       <span
@@ -417,7 +413,9 @@ import {
   splitString,
   toUpperCase,
   findCellKeyByFactor,
-  isNumberVal,
+  isNumberValue,
+  transNumPercentType,
+  getDecimalPlaces,
   transDecimalPlace,
   resetStyle
 } from "../common/customTable";
@@ -1214,11 +1212,16 @@ export default {
         }
       }
 
-      /* 不是数字类型,清除原来设置的格式 */
-      if (!isNumberVal(value)) {
-        cell.ShowStyle = '';
-        cell.ShowStyle = '';
-      };
+       /* 不是数字类型,清除原来设置的格式 */
+      let cellValueStyle = cell.ShowStyle?JSON.parse(cell.ShowStyle):{}
+      if(!isNumberValue(value)){
+        cell.ShowStyle = JSON.stringify({...cellValueStyle,pn:0,nt:'',last:'',decimal:null});
+      }else{
+        cell.ShowFormatValue = cellValueStyle.nt?transNumPercentType(value,cellValueStyle.nt):''
+        const num = getDecimalPlaces(cell.ShowFormatValue?cell.ShowFormatValue+'':value+'')
+        //last 记录最先操作方式
+        cell.ShowStyle = JSON.stringify({...cellValueStyle,pn:0,last:'nt',decimal:num})
+      }
 
       //判断是否是有插入值的依赖单元格 更新值或重置关系
       this.checkCellRelation(cell)

+ 14 - 7
src/views/datasheet_manage/components/MixedTable.vue

@@ -113,7 +113,7 @@
                   :data-datarindex="index"
                   :data-datacindex="cell_index"
                   :data-key="cell.Uid"
-                >{{ cell.ShowStyle?cell.ShowFormatValue:cell.ShowValue }}</span>
+                >{{ isShowDecimal(cell)?showDecimalValue(cell):isShowFormat(cell.ShowStyle)?cell.ShowFormatValue:cell.ShowValue }}</span>
               </el-popover>
 
               <!-- 数字格式化显示 -->
@@ -125,7 +125,7 @@
                 :data-datacindex="cell_index"
                 :data-key="cell.Uid"
               >
-                {{isShowDecimal(cell)?showDecimalValue(cell):isShowFormat(cell.ShowStyle)?cell.ShowFormatValue:cell.DataTime?cell.ShowValue:[7,6,5].includes(cell.DataType)?cell.ShowValue:cell.Value}}
+                {{isShowDecimal(cell)?showDecimalValue(cell):isShowFormat(cell.ShowStyle)?cell.ShowFormatValue:cell.DataTime?cell.ShowValue:[8,7,6,5,1].includes(cell.DataType)?cell.ShowValue:cell.Value}}
               </span>
 
               <span
@@ -271,7 +271,9 @@ import {
   splitString,
   toUpperCase,
   findCellKeyByFactor,
-  isNumberVal,
+  isNumberValue,
+  transNumPercentType,
+  getDecimalPlaces,
   transDecimalPlace,
   resetStyle
 } from "../common/customTable";
@@ -656,10 +658,15 @@ export default {
       }
 
       /* 不是数字类型,清除原来设置的格式 */
-      if(!isNumberVal(value)){
-        cell.ShowStyle = '';
-        cell.ShowStyle = '';
-      } ;
+      let cellValueStyle = cell.ShowStyle?JSON.parse(cell.ShowStyle):{}
+      if(!isNumberValue(value)){
+        cell.ShowStyle = JSON.stringify({...cellValueStyle,pn:0,nt:'',last:'',decimal:null});
+      }else{
+        cell.ShowFormatValue = cellValueStyle.nt?transNumPercentType(value,cellValueStyle.nt):''
+        const num = getDecimalPlaces(cell.ShowFormatValue?cell.ShowFormatValue+'':value+'')
+        //last 记录最先操作方式
+        cell.ShowStyle = JSON.stringify({...cellValueStyle,pn:0,last:'nt',decimal:num})
+      }
 
       //判断是否是有插入值的依赖单元格 更新值或重置关系
       this.checkCellRelation(cell)

+ 23 - 25
src/views/datasheet_manage/components/toolBarSection.vue

@@ -66,7 +66,7 @@
   </div>
 </template>
 <script>
-import { transDecimalPlace,isNumberVal,getDecimalPlaces,transNumPercentType } from '../common/customTable';
+import { isNumberValue,getDecimalPlaces,transNumPercentType } from '../common/customTable';
 export default {
   props: {
     cell: {
@@ -94,15 +94,13 @@ export default {
         if(nval) {
           console.log(nval)
           this.cellArray = Array.isArray(nval)?nval:[nval]
-          this.option = this.cellArray.length?this.cellArray[0].ShowStyle?{
+          this.option = this.cellArray.length&&this.cellArray[0].ShowStyle?{
             ...JSON.parse(this.cellArray[0].ShowStyle)
           }:{
             nt: "",//numberType
             pn: 0,//ponitNum
-          }:{
-            nt: "",//numberType
-            pn: 0,//ponitNum,
           }
+          this.option.decimal=!isNaN(parseFloat(this.option.decimal)) && isFinite(this.option.decimal)?this.option.decimal:undefined
         }else{
           this.cellArray=[]
           this.option ={
@@ -334,6 +332,7 @@ export default {
       this.cellArray.map(item =>{
         if(!item.merData || item.merData.type=='merge'){
           if(key==='decimal'){
+            console.log(222)
             this.changeCellDecimal(item)
           }else{
             this.changeCellPoint(key,item)
@@ -344,26 +343,14 @@ export default {
     /* 处理小数点位数 */
     changeCellPoint(key,cell) {
       //单元格不是数字就不用转了
-      if( !isNumberVal(cell.ShowValue)) return
+      if( !isNumberValue(cell.ShowValue)) return
       let cellValueStyle = cell.ShowStyle?JSON.parse(cell.ShowStyle):{
         nt: "",//numberType
         pn: 0,//ponitNum
       }
-      key==='add-point' ? cellValueStyle.pn++ : cellValueStyle.pn--;
-      
-      //判断小数点后尾数 整数最小pn为0 小数最小为负位数
-      if(cellValueStyle.pn <= parseInt(`-${getDecimalPlaces(cell.ShowValue)}`)) {
-        cellValueStyle.pn = parseInt(`-${getDecimalPlaces(cell.ShowValue)}`)
-      }
-
-      //百分比格式的话 pn最小为位数-2
-      if(cellValueStyle.nt==='percent' && cellValueStyle.pn <= parseInt(`-${getDecimalPlaces(cell.ShowValue)-2}`)) {
-        cellValueStyle.pn = parseInt(`-${getDecimalPlaces(cell.ShowValue)-2}`)
-      }
-
-      cell.ShowFormatValue = transDecimalPlace(cell.ShowValue,{...cellValueStyle,pn:cellValueStyle.pn})
-      cell.ShowStyle = JSON.stringify({...cellValueStyle,pn:cellValueStyle.pn})
-
+      let xsNum=!isNaN(parseFloat(cellValueStyle.decimal)) && isFinite(cellValueStyle.decimal)?cellValueStyle.decimal:getDecimalPlaces(cell.ShowValue)
+      // console.log(xsNum)
+      cell.ShowStyle = JSON.stringify({...cellValueStyle,pn:0,decimal:key==='add-point'?xsNum+1:xsNum-1<0?0:xsNum-1})
     },
     changeCellsType(){
       this.cellArray.map(item =>{
@@ -373,19 +360,30 @@ export default {
       })
     },
     changeCellDecimal(item) {
-      if(!isNumberVal(item.ShowValue)) return
+      if(!isNumberValue(item.ShowValue)) return
+      console.log(this.option,getDecimalPlaces(item.ShowFormatValue))
+      if(this.option.nt=='percent'&& this.option.decimal==undefined){
+        this.$set(this.option,'decimal',getDecimalPlaces(item.ShowFormatValue)+'')
+        return
+      }
+      if(this.option.nt!='percent'&&this.option.decimal==undefined){
+        item.ShowFormatValue=''
+      }
+      // console.log(this.option)
+      this.option.pn = 0
       const styleCss = item.ShowStyle ? JSON.parse(item.ShowStyle) : {};
       item.ShowStyle = JSON.stringify({...this.option,last:!isNaN(parseFloat(this.option.decimal)) && isFinite(this.option.decimal)?styleCss.last?styleCss.last:'decimal':styleCss.last=='decimal'?this.option.nt=='percent'?'nt':'':styleCss.last})
     },
     /* 处理百分位或数字格式 */
     changeCellType(item) {
-      if(!isNumberVal(item.ShowValue)) return
+      if(!isNumberValue(item.ShowValue)) return
       this.option.pn = 0
 
       item.ShowFormatValue = transNumPercentType(item.ShowValue,this.option.nt)
+      const num = getDecimalPlaces(item.ShowFormatValue?item.ShowFormatValue:item.ShowValue)
       const styleCss = item.ShowStyle ? JSON.parse(item.ShowStyle) : {};
-      item.ShowStyle = JSON.stringify({...this.option,last:this.option.nt!='percent'?styleCss.last=='nt'?!isNaN(parseFloat(this.option.decimal)) && isFinite(this.option.decimal)?'decimal':'':styleCss.last:styleCss.last?styleCss.last:'nt'})
-
+      //last 记录最先操作方式
+      item.ShowStyle = JSON.stringify({...this.option,last:'nt',decimal:num})
     },
     cellMergeHandle(){
       this.$emit('cellMerge')

+ 9 - 4
src/views/datasheet_manage/mixins/conditionTableMixin.js

@@ -1,3 +1,4 @@
+import { transDecimalPlace, getDecimalPlaces } from '../common/customTable';
 export default {
     methods: {
         // 判断时间
@@ -253,12 +254,16 @@ export default {
                 if (styleCss.last == 'nt') {
                     Value = this.commonDecimalValue(cell.ShowValue * 100, styleCss.decimal) + '%'
                 } else {
-                    Value = (parseInt(this.commonDecimalValue(cell.ShowValue, styleCss.decimal) * 100)) + '%'
+                    Value = (parseFloat(this.commonDecimalValue(cell.ShowValue, styleCss.decimal) * 100)) + '%'
                 }
-
             } else {
-                Value = this.commonDecimalValue(cell.ShowValue, styleCss.decimal)
+                Value = parseFloat(this.commonDecimalValue(cell.ShowValue, styleCss.decimal)).toFixed(styleCss.decimal)
+            }
+            if (getDecimalPlaces(cell.ShowValue) < styleCss.decimal || styleCss.nt == 'percent') {
+                Value = transDecimalPlace(cell.ShowValue + '', { ...styleCss, pn: styleCss.decimal - getDecimalPlaces(cell.ShowValue) + (styleCss.nt == 'percent'&&getDecimalPlaces(cell.ShowValue)!=0 ? 2 : 0) })
             }
+            // console.log(cell)
+            this.$set(this.config.data[cell.rIndex][cell.cIndex],'ShowFormatValue',Value)
             return Value
         },
         commonDecimalValue(values, decimal) {
@@ -277,7 +282,7 @@ export default {
             const styleCss = style ? JSON.parse(style) : {};
             let tag =
                 type === "css"
-                    ? styleCss.pn > 0 || ["percent"].includes(styleCss.nt)
+                    ? styleCss.pn > 0 || styleCss.pn < 0 || ["percent"].includes(styleCss.nt)
                     : style.EdbInfoId;
             return tag;
         },