反应表中的导出到 CSV 按钮

2023-09-06前端开发问题
2

本文介绍了反应表中的导出到 CSV 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

寻找一种方法将导出到 CSV"按钮添加到作为 npmjs 包的 react-table (https://www.npmjs.com/package/react-table).

我需要添加一个自定义按钮,用于将表格数据导出为 csv 或 xls 格式的 excel 工作表?

解决方案

下面是集成的样子

从'react'导入反应;导入'react-dropdown/style.css'导入反应表/反应表.css"从react-table"导入 ReactTable;从react-csv"导入 {CSVLink};常量列 = [{标题:'名称',accessor: 'name',//基于字符串的值访问器!},{标题:'年龄',访问者:'年龄',}]类 AllPostPage 扩展 React.Component {构造函数(道具){超级(道具);this.download = this.download.bind(this);这个.state = {表属性:{所有数据:[{姓名":ramesh",年龄":12"},{姓名":账单",年龄":13"},{姓名":阿伦",年龄":9"},{姓名":凯西",年龄":21"}]},数据下载:[]};}下载(事件){const currentRecords = this.reactTable.getResolvedState().sortedData;var data_to_download = []for (var index = 0; index < currentRecords.length; index++) {让 record_to_download = {}for(var colIndex = 0; colIndex < columns.length ; colIndex ++) {record_to_download[columns[colIndex].Header] = currentRecords[index][columns[colIndex].accessor]}data_to_download.push(record_to_download)}this.setState({ dataToDownload: data_to_download }, () => {//点击 CSVLink 组件触发 CSV 下载this.csvLink.link.click()})}使成为() {返回 

<button onClick={this.download}>下载</按钮></div>

<ReactTable ref={(r)=>this.reactTable = r}数据={this.state.tableproperties.allData} 列={columns} 可过滤defaultFilterMethod={(过滤器, 行) =>String(row[filter.id]).toLowerCase().includes(filter.value.toLowerCase())}/></div></div>}}导出默认的 AllPostPage;

这也适用于过滤器.

Looking for a way to add an "Export to CSV" button to a react-table which is an npmjs package (https://www.npmjs.com/package/react-table).

I need to add a custom button for exporting the table data to an excel sheet in the csv or xls format?

解决方案

Here is how integration will look like

import React from 'react';
import 'react-dropdown/style.css'
import 'react-table/react-table.css'
import ReactTable from "react-table";
import {CSVLink} from "react-csv";

const columns = [
   {
       Header: 'name',
       accessor: 'name', // String-based value accessors!
   },
   {
       Header: 'age',
       accessor: 'age',

  }]
class AllPostPage extends React.Component {
    constructor(props) {
       super(props);
       this.download = this.download.bind(this);
       this.state = {
          tableproperties: {
             allData: [
                {"name": "ramesh","age": "12"},
                {"name": "bill","age": "13"},
                {"name": "arun","age": "9"},
                {"name": "kathy","age": "21"}
             ]
          },
          dataToDownload: []
       };
    }

   download(event) {
      const currentRecords = this.reactTable.getResolvedState().sortedData;
      var data_to_download = []
      for (var index = 0; index < currentRecords.length; index++) {
         let record_to_download = {}
         for(var colIndex = 0; colIndex < columns.length ; colIndex ++) {
            record_to_download[columns[colIndex].Header] = currentRecords[index][columns[colIndex].accessor]
         }
         data_to_download.push(record_to_download)
      }
      this.setState({ dataToDownload: data_to_download }, () => {
         // click the CSVLink component to trigger the CSV download
         this.csvLink.link.click()
      })
    } 

    render() {
       return <div>
                 <div>
                    <button onClick={this.download}>
                        Download
                    </button>
                 </div>
                 <div>
                    <CSVLink
                        data={this.state.dataToDownload}
                        filename="data.csv"
                        className="hidden"
                        ref={(r) => this.csvLink = r}
                        target="_blank"/>

                 </div>
                 <div>
                    <ReactTable ref={(r) => this.reactTable = r}
                                data={this.state.tableproperties.allData} columns={columns} filterable
                                defaultFilterMethod={(filter, row) =>
                                    String(row[filter.id]).toLowerCase().includes(filter.value.toLowerCase())}
                    />
                 </div>
              </div>
    }
}

export default AllPostPage;

This will work with filters as well.

这篇关于反应表中的导出到 CSV 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Fatal error: Call to a member function fetch_assoc() on a no
业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass...
2024-12-13 前端开发问题
136

layui实现laydate日历控件控制之前日期不可选择
具体实现代码如下: laydate.render({ elem: '#start_time', min:0, //,type: 'date' //默认,可不填}); 只要加一个min参数,就可以控制了。0表示之前的日期不可...
2024-11-29 前端开发问题
133

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

layui树状组件tree怎么默认勾选?
在layui树状组件tree中,勾选问题可以通过以下方法解决: 通过tree的oncheck事件来监听勾选操作,然后根据勾选状态进行相应的处理。例如: tree.on('check', function(obj) { // 获取勾选状态 var isChecked = obj.checked; // 获取当前节点数据 var data =...
2024-11-09 前端开发问题
372

layui tree树组件怎么自定义添加图标
经常用到layui的朋友都知道,layui tree默认是不能自定义图标的,那么我们要自定义的话要怎么操作呢? 首先用编辑器软件(修改时候用编辑器记得编码),打开layui.js。搜索: i class="layui-icon layui-icon-file" 改为如下代码: i class="'+ (i.icon || "l...
2024-10-26 前端开发问题
493