2017-03-02 12:47:52 +01:00
|
|
|
/**
|
|
|
|
* File: table.js
|
|
|
|
* Author: Markus Grigull <mgrigull@eonerc.rwth-aachen.de>
|
|
|
|
* Date: 02.03.2017
|
|
|
|
* Copyright: 2017, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
* This file is part of VILLASweb. All Rights Reserved. Proprietary and confidential.
|
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
|
|
|
**********************************************************************************/
|
|
|
|
|
|
|
|
import React, { Component } from 'react';
|
2017-03-07 11:12:22 +01:00
|
|
|
import { Table, Button, Glyphicon, FormControl } from 'react-bootstrap';
|
2017-03-06 22:01:33 +01:00
|
|
|
import { Link } from 'react-router';
|
|
|
|
|
2017-03-16 12:25:54 +01:00
|
|
|
//import TableColumn from './table-column';
|
2017-03-06 22:01:33 +01:00
|
|
|
|
|
|
|
class CustomTable extends Component {
|
2017-03-07 11:12:22 +01:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2017-03-16 12:25:54 +01:00
|
|
|
rows: [],
|
2017-03-07 11:12:22 +01:00
|
|
|
editCell: [ -1, -1 ]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-06 22:01:33 +01:00
|
|
|
static defaultProps = {
|
|
|
|
width: null
|
|
|
|
};
|
2017-03-02 12:47:52 +01:00
|
|
|
|
2017-03-07 11:12:22 +01:00
|
|
|
onClick(event, row, column) {
|
|
|
|
this.setState({ editCell: [ column, row ]}); // x, y
|
|
|
|
}
|
|
|
|
|
2017-03-16 12:25:54 +01:00
|
|
|
addCell(data, index, child) {
|
|
|
|
// add data to cell
|
|
|
|
const dataKey = child.props.dataKey;
|
|
|
|
var cell = [];
|
|
|
|
|
|
|
|
if (dataKey && data[dataKey] != null) {
|
|
|
|
// get content
|
|
|
|
var content;
|
|
|
|
const modifier = child.props.modifier;
|
|
|
|
|
|
|
|
if (modifier) {
|
|
|
|
content = modifier(data[dataKey]);
|
|
|
|
} else {
|
|
|
|
content = data[dataKey].toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if cell should be a link
|
|
|
|
const linkKey = child.props.linkKey;
|
|
|
|
if (linkKey && data[linkKey] != null) {
|
|
|
|
cell.push(<Link to={child.props.link + data[linkKey]}>{content}</Link>);
|
|
|
|
} else if (child.props.clickable) {
|
|
|
|
cell.push(<a onClick={() => child.props.onClick(index)}>{content}</a>);
|
|
|
|
} else {
|
|
|
|
cell.push(content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child.props.dataIndex) {
|
|
|
|
cell.push(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add buttons
|
|
|
|
if (child.props.editButton) {
|
|
|
|
cell.push(<Button bsClass='table-control-button' onClick={() => child.props.onEdit(index)}><Glyphicon glyph='pencil' /></Button>);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (child.props.deleteButton) {
|
|
|
|
cell.push(<Button bsClass='table-control-button' onClick={() => child.props.onDelete(index)}><Glyphicon glyph='remove' /></Button>);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
// check if data exists
|
|
|
|
if (nextProps.data == null) {
|
|
|
|
this.setState({ rows: [] });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create row data
|
|
|
|
var rows = nextProps.data.map((data, index) => {
|
|
|
|
// check if multiple columns
|
|
|
|
if (Array.isArray(nextProps.children)) {
|
|
|
|
var row = [];
|
|
|
|
|
|
|
|
nextProps.children.forEach(child => {
|
|
|
|
row.push(this.addCell(data, index, child));
|
|
|
|
});
|
|
|
|
|
|
|
|
return row;
|
|
|
|
} else {
|
|
|
|
// table only has a single column
|
|
|
|
return [ this.addCell(data, index, nextProps.children) ];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setState({ rows: rows });
|
|
|
|
}
|
|
|
|
|
2017-03-02 12:47:52 +01:00
|
|
|
render() {
|
2017-03-16 12:25:54 +01:00
|
|
|
// get children
|
|
|
|
var children = this.props.children;
|
|
|
|
if (Array.isArray(this.props.children) === false) {
|
|
|
|
children = [ children ];
|
2017-03-06 22:01:33 +01:00
|
|
|
}
|
2017-03-02 12:47:52 +01:00
|
|
|
|
|
|
|
return (
|
2017-03-16 12:25:54 +01:00
|
|
|
<Table style={{ width: this.props.width}} striped hover bordered>
|
2017-03-02 12:47:52 +01:00
|
|
|
<thead>
|
|
|
|
<tr>
|
2017-03-06 22:01:33 +01:00
|
|
|
{this.props.children}
|
2017-03-02 12:47:52 +01:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2017-03-16 12:25:54 +01:00
|
|
|
{this.state.rows.map((row, rowIndex) => (
|
2017-03-07 11:12:22 +01:00
|
|
|
<tr key={rowIndex}>
|
|
|
|
{row.map((cell, cellIndex) => (
|
2017-03-16 12:25:54 +01:00
|
|
|
<td key={cellIndex} onClick={children[cellIndex].props.inlineEditable === true ? (event) => this.onClick(event, rowIndex, cellIndex) : () => {}}>
|
2017-03-07 11:12:22 +01:00
|
|
|
{(this.state.editCell[0] === cellIndex && this.state.editCell[1] === rowIndex ) ? (
|
2017-03-16 12:25:54 +01:00
|
|
|
<FormControl type="text" value={cell} onChange={(event) => children[cellIndex].props.onInlineChange(event, rowIndex, cellIndex)} />
|
2017-03-07 11:12:22 +01:00
|
|
|
) : (
|
|
|
|
<span>
|
|
|
|
{cell.map((element, elementIndex) => (
|
|
|
|
<span key={elementIndex}>{element}</span>
|
|
|
|
))}
|
|
|
|
</span>
|
|
|
|
)}
|
2017-03-06 22:01:33 +01:00
|
|
|
</td>
|
2017-03-02 12:47:52 +01:00
|
|
|
))}
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
2017-03-06 22:01:33 +01:00
|
|
|
</Table>
|
2017-03-02 12:47:52 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 22:01:33 +01:00
|
|
|
export default CustomTable;
|