Skip to content

Instantly share code, notes, and snippets.

@pauldambra
Created February 16, 2015 23:27
Show Gist options
  • Save pauldambra/cc364759476076779bcd to your computer and use it in GitHub Desktop.
Save pauldambra/cc364759476076779bcd to your computer and use it in GitHub Desktop.
Sorting some data with ReactJS
var PayTable = React.createClass({
getInitialState: function() {
return {
sortDirection: 'descending',
data: this.props.payYears.sort(sortDescending)
};
},
sortData: function() {
if(this.state.sortDirection==='descending') {
this.setState({
sortDirection: 'ascending',
data: this.props.payYears.sort(sortAscending)
});
} else {
this.setState({
sortDirection: 'descending',
data: this.props.payYears.sort(sortDescending)
});
}
},
render: function() {
return (
<table className="table table-striped">
<thead>
<tr>
<th onClick={this.sortData}
className={this.state.sortDirection}>
Year
</th>
<th>All</th>
<th>Full-time</th>
<th>Part-time</th>
</tr>
</thead>
<tbody>
{this.state.data.map(function(payYear) {
return <PayRow key={payYear.year} payYear={payYear} />;
})}
</tbody>
</table>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment