Vue实现Excel表格中按钮增加小数位数,减少小数位数功能,多用于处理金融数据

效果图

javascript 复制代码
<template>
  <div>
    <el-button @click="increaseDecimals">A按钮</el-button>
    <el-button @click="roundNumber">B按钮</el-button>
    <el-table :data="tableData" border>
      <el-table-column v-for="(item, index) in tableHeader" :key="index" :prop="`col${index}`" label="表头">
        <template slot-scope="scope">
          <span>{{ formatNumber(scope.row[`col${index}`]) }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data() {
    return {
      tableHeader: [], // 表格表头
      tableData: [] // 表格数据
    };
  },
  mounted() {
    this.generateTableHeader(); // 生成随机表头
    this.generateTableData(); // 生成表格数据
  },
  methods: {
    generateTableHeader() {
      const minColumns = 10; // 最低列数
      const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
      this.tableHeader = Array.from({ length: minColumns }, (_, index) => letters[index % letters.length].repeat(4));
    },
    generateTableData() {
      const numRows = 50; // 行数
      const numColumns = this.tableHeader.length; // 列数
      const maxNumber = 1000; // 数字的最大值
      this.tableData = Array.from({ length: numRows }, () => {
        const rowData = {};
        for (let i = 0; i < numColumns; i++) {
          rowData[`col${i}`] = Math.random() * 3;
        }
        return rowData;
      });
    },
    increaseDecimals() {
      const arr = this.tableData
      for (let row of arr) {
        for (let key in row) {
          let value = row[key];
          if (typeof value === 'number') {
            console.log('进来了');
            if (value % 1 !== 0) {
              row[key] = parseFloat(value.toString() + '0');
            }
          }
        }
        return this.tableData = arr
      }
    },
    roundNumber() {
      for (let row of this.tableData) {
        for (let key in row) {
          let value = row[key];
          if (Number.isFinite(value) && Number.isInteger(value) === false) {
            row[key] = Math.round(value * 100) / 100;
          }
        }
      }
    },
    formatNumber(number) {
      return Number(number).toFixed(4);
    }
  }
};
</script>
相关推荐
慧一居士34 分钟前
Vite 中配置环境变量方法及完整示例
前端·vue.js
yyt3630458413 小时前
TypeScript { [key: string]: unknown } 索引签名写法和 Record 替代
前端·javascript·vue.js·typescript·ecmascript·es6
Web3VentureView3 小时前
SYNBO 协议亮相 ChainThink “Meme 回归” AMA:市场奖励机制深度剖析
网络·金融·web3·区块链·加密货币
getapi4 小时前
在宝塔面板中部署 Vue 项目打包后的 dist 文件作为前端
前端·javascript·vue.js
xkxnq8 小时前
第二阶段:Vue 组件化开发(第 28天)
前端·javascript·vue.js
hongyucai8 小时前
智能体技术架构的深度解析:从模型层到应用层的金融实践
金融·架构
zhengxianyi5158 小时前
vue-cli build, vite build 生产部署刷新或弹窗404,页面空白修复方法
前端·javascript·vue.js·nginx·生产部署
恋爱绝缘体18 小时前
Vue.js 组件 - 自定义事件【1】
前端·javascript·vue.js
不坑老师9 小时前
小工具显出大才能——不坑盒子为教育数字化转型贡献“新方案”
microsoft·word·excel·ppt·office
暴风游侠9 小时前
金融经济学笔记
笔记·金融