本文介绍了反应表中的导出到 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
相关推荐
业务场景:使用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
具体实现代码如下: laydate.render({ elem: '#start_time', min:0, //,type: 'date' //默认,可不填}); 只要加一个min参数,就可以控制了。0表示之前的日期不可...
2024-11-29
前端开发问题
133
在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
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09
前端开发问题
313
在layui树状组件tree中,勾选问题可以通过以下方法解决: 通过tree的oncheck事件来监听勾选操作,然后根据勾选状态进行相应的处理。例如: tree.on('check', function(obj) { // 获取勾选状态 var isChecked = obj.checked; // 获取当前节点数据 var data =...
2024-11-09
前端开发问题
372
经常用到layui的朋友都知道,layui tree默认是不能自定义图标的,那么我们要自定义的话要怎么操作呢? 首先用编辑器软件(修改时候用编辑器记得编码),打开layui.js。搜索: i class="layui-icon layui-icon-file" 改为如下代码: i class="'+ (i.icon || "l...
2024-10-26
前端开发问题
493
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)