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>
相关推荐
非优秀程序员11 小时前
第04章 邮箱大战,163 与 QQ 的暗战|《互联网产品恩仇录》
java·vue.js·架构
tryCbest18 小时前
Vue系列——第 15 篇:内置组件全家桶——Transition、Teleport、Suspense、KeepAlive
前端·vue.js
2601_9679801121 小时前
Python自动化办公:模块、库与样式调整教程
python·excel·自动化办公·openpyxl·样式调整
徐小夕1 天前
开源Vector-Graph-RAG:向量+图谱双引擎,打破纯向量RAG的效果天花板
前端·vue.js·github
用户0332126663671 天前
使用 Python 轻松加密或解密 Excel 文档
python·excel
FungLeo1 天前
成为全栈·Vue 管理后台篇·Vue3 + Vite + Naive UI:后台工程基座怎么搭
前端·vue.js·ui·typescript·vite·前端工程化·biome
FungLeo1 天前
成为全栈·Vue管理后台篇·<scriptsetup>:把页面拆成可读、可测的 Vue 组件
前端·javascript·vue.js·typescript·前端组件化·composition api·vue sfc
软件技术NINI2 天前
基于SpringBoot和Vue的新能源汽车销售推荐系统
java·vue.js·spring boot·后端·汽车·毕业设计·毕设
招风的黑耳2 天前
SpringBoot+Vue+MySQL 民宿在线预定平台 完整可运行版(源码+文档)
vue.js·spring boot·mysql
总裁余(余登武)2 天前
【python项目】:1操作EXCEl自动化开发
python·自动化·excel