elementui el-table表格数据展示千分位

我们注册成全局组件方便全局el-table-column使用

1、在unils文件夹下创建一个thousands.js

// 不带插槽的
export function  stateFormat(row, column) {
  let num = Number(row[column["property"]]);
    let n = 2;
    let symbol = ",";
      if(typeof num!=='number')throw new TypeError('num参数应该是一个number类型');
      if(n<0)throw new Error('参数n不应该小于0');
      var hasDot=parseInt(num)!=num;//这里检测num是否为小数,true表示小数
      var m=(n!=undefined&&n!=null)?n:1;
      num=m==0?num.toFixed(m)+'.':hasDot?(n?num.toFixed(n):num):num.toFixed(m);
      symbol=symbol||',';
      num=num.toString().replace(/(\d)(?=(\d{3})+\.)/g,function(match, p1,p2) {
          return p1 + symbol;
      });
      if(n==0||(!hasDot&&!n)){//如果n为0或者传入的num是整数并且没有指定整数的保留位数,则去掉前面操作中的小数位
          num=num.substring(0,num.indexOf('.'));
      }
      return num;
   }


   //带插槽的
  export function handleScopeFormat(num,n=2,symbol) {
    // 保证为number类型
    num = Number(num)
    if(typeof num!=='number')throw new TypeError('num参数应该是一个number类型');
    if(n<0)throw new Error('参数n不应该小于0');
    var hasDot=parseInt(num)!=num;//这里检测num是否为小数,true表示小数
    var m=(n!=undefined&&n!=null)?n:1;
    num=m==0?num.toFixed(m)+'.':hasDot?(n?num.toFixed(n):num):num.toFixed(m);
    symbol=symbol||',';
    num=num.toString().replace(/(\d)(?=(\d{3})+\.)/g,function(match, p1,p2) {
        return p1 + symbol;
    });
    if(n==0||(!hasDot&&!n)){//如果n为0或者传入的num是整数并且没有指定整数的保留位数,则去掉前面操作中的小数位
        num=num.substring(0,num.indexOf('.'));
    }
    return num;
}

2、在main.js里引入

import {stateFormat,handleScopeFormat} from '@/utils/thousands.js'
Vue.prototype.stateFormat = stateFormat
Vue.prototype.handleScopeFormat = handleScopeFormat

3、快活的使用

//不带插槽的
 <el-table-column prop="inPlaceAmt" align="center" :formatter="stateFormat" label="资金1" show-overflow-tooltip />
 //带插槽的
  <el-table-column align="center"  label="资金2" >
        <template slot-scope="scope">
          {{handleScopeFormat(scope.row.inPlaceAmt - scope.row.allocationAmt)}}
        </template>
      </el-table-column>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值