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