You need to enable JavaScript to run this app. 如何在axios请求返回后配置的表格列中添加下载按钮?
优惠活动
大模型
产品
解决方案
定价
更多

如何在axios请求返回后配置的表格列中添加下载按钮?

问题修复与下载按钮实现

你当前使用的是Ant Design Table组件的配置逻辑,原有代码存在两处基础错误:

  • 字符串方法拼写错误:col.toLoweSrCase() 应为 col.toLowerCase()
  • 列配置中添加按钮不能直接声明button属性,需要使用Table内置的render渲染函数实现自定义内容

方案1:为每行添加独立下载按钮

在遍历后端返回的列之后,单独新增一个「操作」列配置,插入到columns数组中即可:

axios.post("/notas",data)
    .then(response => {
        const cols = [];
        const rows = [];
        // 遍历生成原有数据列
        response.data.columns.forEach((col,index)=>{
            cols.push({
                title: col,
                dataIndex: col.toLowerCase(),
                key: col.toLowerCase()
            })
        });
        // 新增操作列,加入下载按钮
        cols.push({
            title: '操作',
            key: 'action',
            render: (text, record) => (
                <button onClick={() => handleDownload(record)}>
                    下载当前行数据
                </button>
            )
        })
        // 行数据生成逻辑保持不变
        response.data.data.forEach((row,index)=>{
            let newRow = {key:index+1};
            row.forEach((elem,index)=> {
                newRow[cols[index].key] = elem;
            });
            rows.push(newRow);
        });
        this.setState({
            loading:false,
            notas:{
                columns:cols,
                dataSource:rows
            },
            deleted: false
        })
    })
    .catch(err => {
        this.setState({loading:false});
        console.log(err);
    });

对应的下载函数示例(以导出当前行数据为CSV为例):

// 类组件中定义的下载方法
handleDownload = (record) => {
    // 把行对象转成CSV格式
    const csvContent = "data:text/csv;charset=utf-8," 
        + Object.keys(record).join(",") + "\n" 
        + Object.values(record).join(",");
    const encodedUri = encodeURI(csvContent);
    const link = document.createElement("a");
    link.setAttribute("href", encodedUri);
    link.setAttribute("download", `行数据_${record.key}.csv`);
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

方案2:添加全局下载按钮,导出整个表格数据

不需要修改columns配置,直接在Table组件上方加按钮即可,点击时读取state中存储的notas.dataSource全量数据导出:

{/* 类组件render方法中示例 */}
<button onClick={this.handleDownloadAll}>下载全部数据</button>
<Table 
    columns={this.state.notas.columns} 
    dataSource={this.state.notas.dataSource} 
    loading={this.state.loading}
/>

全量下载方法示例:

handleDownloadAll = () => {
    const { dataSource, columns } = this.state.notas;
    // 生成表头
    const header = columns.map(col => col.title).join(",");
    // 生成每行内容
    const rows = dataSource.map(row => {
        return columns.map(col => row[col.key]).join(",")
    }).join("\n");
    const csvContent = "data:text/csv;charset=utf-8," + header + "\n" + rows;
    const encodedUri = encodeURI(csvContent);
    const link = document.createElement("a");
    link.setAttribute("href", encodedUri);
    link.setAttribute("download", "全量数据.csv");
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

内容的提问来源于stack exchange,提问作者Master

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 00:57:04