Last active
October 19, 2017 05:03
-
-
Save dvas0004/24a4749bc9df4e725a13f16440d4ac93 to your computer and use it in GitHub Desktop.
re-usable REACT JS component http://blog.davidvassallo.me/2016/07/20/react-js-tables-in-the-browser/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ReactTable = React.createClass({ | |
getInitialState: function() { | |
return {currentData: this.props.data}; | |
}, | |
render: function() { | |
var key = Date.now(); | |
var tableHeader = Object.keys(this.state.currentData[0]).map(function(columnName){ | |
key = key+1; | |
return ( | |
<th key={key} data-field={columnName}>{columnName}</th> | |
); | |
}); | |
var tableRow = this.state.currentData.map(function(rowObject){ | |
var i; | |
var returnValue = []; | |
for (i in rowObject){ | |
key = key+1; | |
returnValue.push( | |
<td key={key}> | |
{rowObject[i]} | |
</td> | |
) | |
}; | |
key = key+1; | |
return (<tr key={key}> | |
{returnValue} | |
</tr> | |
); | |
}); | |
return ( | |
<table> | |
<thead> | |
<tr> | |
{tableHeader} | |
</tr> | |
</thead> | |
<tbody> | |
{tableRow} | |
</tbody> | |
</table> | |
); | |
} | |
}); | |
window.__ReactTable__ = ReactTable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment