server_table/src/ServerTable.tsx

89 lines
2.2 KiB
TypeScript
Raw Normal View History

2018-06-23 07:57:40 +00:00
import * as React from 'react';
import { Tips } from "./Utils";
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: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) =>
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) => {
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 (
<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));
return (
<div>
{makeTable()}
2018-06-23 17:32:35 +00:00
<Chart data={lastFiltered} />
<CSVLink data={lastFiltered} filename={"data.csv"}>
Click here to download CSV file
</CSVLink>
<br />
<Tips />
</div>
)
}}
</ReactTable>
2018-06-23 07:57:40 +00:00
);
}
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
}