Skip to content

Instantly share code, notes, and snippets.

@securingsincity
Created March 24, 2015 20:08
Show Gist options
  • Save securingsincity/4058755b9d6573a48cd8 to your computer and use it in GitHub Desktop.
Save securingsincity/4058755b9d6573a48cd8 to your computer and use it in GitHub Desktop.
Check all example
var React = require('react/addons');
var Table = require('./table.jsx');
var rows = [
{
'id' : 1,
'foo': 'bar'
},
{
'id' : 2,
'foo': 'baarrrr'
},
{
'id' : 3,
'foo': 'baz'
}
];
React.render(<Table rows={rows}/>, document.body);
/** @jsx React.DOM */
var React = require('react/addons');
var row = React.createClass({
getInitialState: function() {
return {
checked: false
};
},
checkIt: function() {
this.props.callback(this.props.index, !this.props.checked);
return;
},
render: function() {
return (
<tr>
<td><input type="checkbox" checked={this.props.checked} onChange={this.checkIt} /></td>
<td>{this.props.obj.foo}</td>
</tr>
);
}
});
module.exports = row;
/** @jsx React.DOM */
var React = require('react/addons');
var _ = require('lodash');
var Row = require('./row.jsx');
var table = React.createClass({
getInitialState: function() {
var rowState =[];
for(var i = 0; i < this.props.rows.length; i++) {
rowState[i] = false;
}
return {
checkAll: false,
rowState:rowState
};
},
checkRow: function (id,value) {
this.state.rowState[id] = value;
if (this.state.checkAll) {
this.state.checkAll = !this.state.checkAll;
}
this.setState({
rowState: this.state.rowState,
checkAll: this.state.checkAll
});
},
checkAll: function () {
var rowState =[];
var checkState = !this.state.checkAll;
for(var i = 0; i < this.state.rowState.length; i++) {
rowState[i] = checkState;
}
this.state.checkAll = checkState;
this.setState({
rowState: rowState,
checkAll: this.state.checkAll
});
},
render: function() {
var self = this;
var rows = _.map(this.props.rows, function( row,index) {
return (<Row obj={row} index={index} key={row.id} checked={self.state.rowState[index]} callback={self.checkRow} />);
});
return (
<div className="table-holder container">
<input type="checkbox" checked={this.state.checkAll} onChange={this.checkAll} />
<table className="table">{rows}</table>
</div>
);
}
});
module.exports= table;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment