/** * File: table.js * Author: Markus Grigull * 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'; import { Table, Button, Glyphicon, FormControl, Label } from 'react-bootstrap'; import { Link } from 'react-router'; //import TableColumn from './table-column'; class CustomTable extends Component { constructor(props) { super(props); this.state = { rows: [], editCell: [ -1, -1 ] }; } static defaultProps = { width: null }; onClick(event, row, column) { this.setState({ editCell: [ column, row ]}); // x, y } 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({content}); } else if (child.props.clickable) { cell.push( child.props.onClick(index)}>{content}); } else { cell.push(content); } } // add label to content const labelKey = child.props.labelKey; if (labelKey && data[labelKey] != null) { var labelContent = data[labelKey]; if (child.props.labelModifier) { labelContent = child.props.labelModifier(labelContent); } cell.push( ); } if (child.props.dataIndex) { cell.push(index); } // add buttons if (child.props.editButton) { cell.push(); } if (child.props.deleteButton) { cell.push(); } 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 }); } render() { // get children var children = this.props.children; if (Array.isArray(this.props.children) === false) { children = [ children ]; } return ( {this.props.children} {this.state.rows.map((row, rowIndex) => ( {row.map((cell, cellIndex) => ( ))} ))}
this.onClick(event, rowIndex, cellIndex) : () => {}}> {(this.state.editCell[0] === cellIndex && this.state.editCell[1] === rowIndex ) ? ( children[cellIndex].props.onInlineChange(event, rowIndex, cellIndex)} /> ) : ( {cell.map((element, elementIndex) => ( {element} ))} )}
); } } export default CustomTable;