/** @jsx React.DOM */ var Table = React.createClass({ getInitialState: function(props) { var sortCol = -1; var sortDir = -1; this.props.columns.map(function(column, index) { if (column.sort) { if ((this.props.defaultsort && column.name == this.props.defaultsort) || sortCol == -1) { sortCol = index; sortDir = column.reverse ? 1 : -1; } } }.bind(this)); return { offset: 0, sortCol: sortCol, sortDir: sortDir, key: Math.floor(Math.random() * 573573573), }; }, colLength: function() { var len = 0; this.props.columns.map(function(column) { if (column.hidden) { return; } len = len + 1; }.bind(this)); return len; }, sort: function() { if (this.state.sortCol < 0 || this.state.sortCol >= this.colLength()) { return this.props.rows; } else if (this.props.rows.length == 0) { return []; } else { return this.props.rows.concat().sort(function(a, b) { var val = this.props.columns[this.state.sortCol].sort(a, b); if (this.state.sortDir < 0) { return val; } else { return -val; } }.bind(this)); } }, render: function() { if (this.props.rows.length == 0) { var msg = this.props.emptymessage ? this.props.emptymessage : 'There is no data to display.'; return {msg}; } return ( {this.props.columns.map(function(column, index) { if (column.hidden) { return null; } var sort = ; var click = null; if (index == this.state.sortCol) { if (this.state.sortDir < 0) { sort = { " \u2191" }; click = function() { this.setState({sortDir: 1}); }.bind(this); } else { sort = { " \u2193" }; click = function() { this.setState({sortDir: -1}); }.bind(this); } } else { if (column.sort) { sort = { " \u2195" }; click = function() { this.setState({sortCol: index, sortDir: column.reverse ? 1 : -1}); }.bind(this); } } if (column.action) { return ( ); } else { return ( ); } }.bind(this))} {this.sort().map(function(row, index) { if (this.props.paginate) { if (index < this.state.offset || index >= this.state.offset + this.props.paginate) { return null; } } return ( {this.props.columns.map(function(column) { if (column.hidden) { return null; } return ( ); }.bind(this))} ); }.bind(this))} { this.props.paginate ? : null }
{column.name}{sort}{column.name}{sort}
{ column.render(row) }
{ this.state.offset > 0 ? : null } { (this.state.offset + this.props.paginate) < this.props.rows.length ? = this.props.rows.length) { return } this.setState({offset: page}); }.bind(this)}/> : this.props.loading ? loading more data... : null }
); }, });