Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leslie-alldridge/5ffd25cfbd48c3134495b2245aa9d7ad to your computer and use it in GitHub Desktop.
Save leslie-alldridge/5ffd25cfbd48c3134495b2245aa9d7ad to your computer and use it in GitHub Desktop.
stackoverflow react table
import React from "react";
import "./App.css";
import Table from "react-bootstrap/Table";
class App extends React.Component {
state = {
values: [
{ id: 1, name: "test", isActive: true },
{ id: 2, name: "test2", isActive: false },
{ id: 3, name: "test3", isActive: false },
],
};
handleToggleChange = (item) => {
console.log(item);
this.setState({
values: this.state.values.map((obj) =>
obj.id === item.id ? { ...obj, isActive: !item.isActive } : obj
),
});
};
renderRowValue = (value) => {
const sourceId = value.id;
//const payload = { id: value.id, isActive: !value.isActive };
return (
<tr>
<td key={sourceId}>{value.name}</td>
<td>
<input
type="checkbox"
checked={value.isActive}
onChange={() => this.handleToggleChange(value)}
/>
</td>
</tr>
);
};
render() {
return (
<div className="App">
<Table striped bordered hover>
<thead>
<tr>
<th>Source</th>
</tr>
</thead>
<tbody>
{this.state.values.map((value) => this.renderRowValue(value))}
</tbody>
</Table>
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment