vue3 封装表格导出组件

在components组件中新建 tableReport.vue 文件

<template>
    <!-- 导出表格 -->
    <el-dropdown class="export-btns">
        <el-button type="success">数据导出</el-button>
        <template #dropdown>
            <el-dropdown-menu>
                <el-dropdown-item @click="handleExport('current')"><el-icon class="el-icon--left">
                        <document />
                    </el-icon>导出当前页</el-dropdown-item>
                <el-dropdown-item @click="handleExport('all')"><el-icon class="el-icon--left">
                        <document />
                    </el-icon>导出所有</el-dropdown-item>
            </el-dropdown-menu>
        </template>
    </el-dropdown>
</template>

<script setup lang="ts">
import CsvExport from "@/utils/CsvExport";
import { onMounted } from 'vue'

// 接受父组件传递的数据
const props = defineProps({
    // 表格所有数据
    allTable: {
        type: Array,
        default: []
    },
    // 表格当页数据
    viewTable: {
        type: Array,
        default: []
    },
    // 对应表头数据内容
    tableDate: {
        type: Array,
        default: []
    }
})

let tableItem: any = []
let allTableDate: any = []
let viewTableDate: any = []
onMounted(() => {
    setTimeout(() => {
        getTableItem()
        allTableDate = props.allTable
        viewTableDate = props.viewTable
    }, 500)
})

const getTableItem = () => {
    tableItem = props.tableDate.map((item: any) => {
        return {
            prop: item.prop,
            label: item.label
        }
    })
}

const handleExport = (target: any) => {
    const timestamp: any = new Date().valueOf();
    const exportData =
        target === "current" ? viewTableDate : allTableDate;
    CsvExport(
        exportData,
        tableItem
        ,
        `ecom-export-users-${target === "current" ? "current" : "all"}-${timestamp}`
    );
};

</script>

<style scoped></style>

CsvExport 文件

const { Parser } = require("json2csv");

interface IColumn {
  prop: string;
  label?: string;
  formatter?: (row: any, column: IColumn, cellValue: any) => any;
}

function GetRow(row: any, columns: IColumn[]): any {
  let obj: any = {};

  columns.forEach((col) => {
    let val = row[col.prop];

    if (col.formatter) {
      val = col.formatter(row, col, val);
    }

    obj[col.prop] = val;
  });
  return obj;
}

export default function ExportCsv(
  data: any[],
  columns: IColumn[],
  fileName: string = "file"
) {
  const rows = data.map((t) => GetRow(t, columns));
  const fields = columns.map((t) => {
    return { value: t.prop, label: t.label };
  });
  try {
    const json2csvParser = new Parser({ fields });
    const result = json2csvParser.parse(rows);
    const csvContent = "data:text/csv;charset=GBK,\uFEFF" + result;
    const link = document.createElement("a");
    link.href = encodeURI(csvContent);
    link.download = `${fileName}.csv`;
    document.body.appendChild(link); // Required for FF
    link.click(); // This will download the data file named 'my_data.csv'.
    document.body.removeChild(link); // Required for FF
  } catch (err) {
    // Errors are thrown for bad options, or if the data is empty and no fields are provided.
    // Be sure to provide fields if it is possible that your data array will be empty.
    console.error(err);
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A_ugust__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值