Skip to content

Instantly share code, notes, and snippets.

@ripitrust
Last active June 28, 2017 17:18
Show Gist options
  • Save ripitrust/058dd8aca1645daa830b743848ace620 to your computer and use it in GitHub Desktop.
Save ripitrust/058dd8aca1645daa830b743848ace620 to your computer and use it in GitHub Desktop.
<Table data={servers} />
export default class Table extends Component {
static PropTypes = {
data: React.PropTypes.array.required,
};
constructor(props) {
super(props);
this._renderCell = :: this._renderCell;
this._getKeyArray = :: this._getKeyArray;
this._getHeaderTitleArray = :: this._getHeaderTitleArray;
}
_getKeyArray(data){
return Object.keys(data[0] || {});
}
_renderCell(keys) {
const {data} = this.props;
return function(rowIndex, columnIndex) {
const key = keys[columnIndex];
const content = data[rowIndex] ? data[rowIndex][key] : null;
return <Cell>
{content}
</Cell>
}
}
render(){
const { _renderCell } = this;
const { data, tableHeight } = this.props;
const numOfServers = data.length;
const keys = this._getKeyArray(data);
return (
<div style={{height:tableHeight}}>
<Table numRows={numOfServers}
style={{height:tableHeight}}
defaultColumnWidth={100}>
{ keys.map( function(key, index) {
return <Column name={key} key={index} renderCell={_renderCell(keys)}/>
})}
</Table>
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment