Created
March 24, 2015 20:08
-
-
Save securingsincity/4058755b9d6573a48cd8 to your computer and use it in GitHub Desktop.
Check all example
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 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); |
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
/** @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; |
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
/** @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