server_table/src/ServerTable.tsx

75 lines
1.9 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 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> {
private lastFiltered: any[] = []
2018-06-23 07:57:40 +00:00
constructor(props: any) {
super(props);
}
2018-06-23 07:57:40 +00:00
public render() {
const data = this.props.data;
const headers = this.props.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
}
}
debugger;
2018-06-23 08:20:08 +00:00
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"
}}
>
{(state, makeTable,) => {
this.lastFiltered = state.sortedData.map((o: any) => _.pick(o, headers));
return (
<div>
{makeTable()}
<CSVLink data={this.lastFiltered} filename={"data.csv"}>
Click here to download CSV file
</CSVLink>
<br />
<Tips />
</div>
)
}}
</ReactTable>
2018-06-23 07:57:40 +00:00
);
}
public componentWillUnmount() {
this.props.setData(this.lastFiltered);
}
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
}