使用react-csv获取导出csv文件的数据

时间:2018-01-25 16:34:17

标签: reactjs

我正在尝试react-csv。 https://www.npmjs.com/package/react-csv

这是数据:

users = [
{firtstname: 'Ahmed', lastname: 'Tomi' , email: 'ah@smthing.co.com'},
{firtstname: 'Raed', lastname: 'Labes' , email: 'rl@smthing.co.com'},
{firstname: 'Yezzi', lastname: 'Min l3b', email: 'ymin@cocococo.com'}
];

react-csv的数据:

<CSVLink data={this.props.users} >Download me</CSVLink>

然后,我如何只获得csv的姓氏和电子邮件。 我尝试这样做,但出了点问题。

handleExport = () => {
this.state.exportData = this.props.users.map((user, index) => (
user.lastname + user.email
));
}

感谢。

2 个答案:

答案 0 :(得分:2)

两个选择。可以映射您的数据,以便仅包含您需要的数据。像

<CSVLink data={this.props.users.map(x => ({ lastname: x.lastname, email: x.email }))}>Download me</CSVLink>

或者,使用标头属性定义所需的数据

headers = [
  { label: 'Last Name', key: 'lastname' },
  { label: 'Email', key: 'email' }
];

<CSVLink data={this.props.users} headers={headers}>Download me</CSVLink>

答案 1 :(得分:-1)

<CSVLink data={this.state.exportData} >Download me</CSVLink>
相关问题