server_table/src/ServerTable.tsx

65 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-06-23 07:57:40 +00:00
import * as React from 'react';
import { Tips, Logo } from "./Utils";
import * as Papa from 'papaparse';
2018-06-23 08:20:08 +00:00
const reactcsv = require('react-csv');
const CSVLink = reactcsv.CSVLink;
2018-06-23 07:57:40 +00:00
// Import React Table
import ReactTable from 'react-table';
import "react-table/react-table.css";
export class ServerTable extends React.Component<any, any> {
constructor(props: any) {
super(props);
this.state = {
2018-06-23 08:20:08 +00:00
headers: [],
data: []
2018-06-23 07:57:40 +00:00
};
this.loaded = this.loaded.bind(this);
Papa.parse('/data.csv', {
header: true,
download: true,
skipEmptyLines: true,
complete: this.loaded
});
2018-06-23 07:57:40 +00:00
}
2018-06-23 07:57:40 +00:00
public render() {
2018-06-23 08:20:08 +00:00
const { data, headers } = this.state;
2018-06-23 07:57:40 +00:00
const filterPart = (filter: any, row: any) =>
row[filter.id].startsWith(filter.value) ||
row[filter.id].endsWith(filter.value)
2018-06-23 08:20:08 +00:00
const headerGen = (headerName: string) => {
const title = headerName.replace(/([A-Z])/g, ' $1').replace(/^./, (str:string) => str.toUpperCase());
return {
Header: title,
accessor: headerName
}
}
const headerCols = headers.map(headerGen);
2018-06-23 07:57:40 +00:00
return (
<div>
<ReactTable
data={data}
filterable={true}
2018-06-23 08:20:08 +00:00
defaultFilterMethod={filterPart}
columns={headerCols}
2018-06-23 07:57:40 +00:00
defaultPageSize={20}
className="-striped -highlight"
/>
<br />
<CSVLink data={data} filename={"data.csv"}>Click here to download CSV file</CSVLink>
2018-06-23 07:57:40 +00:00
<Tips />
<Logo />
</div>
);
}
public loaded(results: any) {
this.setState({
2018-06-23 08:20:08 +00:00
headers: results.meta.fields,
data: results.data
});
}
2018-06-23 07:57:40 +00:00
}