|
@@ -4,7 +4,7 @@ import Paper from 'material-ui/Paper'
|
|
|
import Button from 'material-ui/Button'
|
|
|
import Table, { TableHead, TableBody, TableRow, TableCell} from 'material-ui/Table'
|
|
|
import Checkbox from 'material-ui/Checkbox'
|
|
|
-import { difference } from 'lodash'
|
|
|
+import { indexOf, concat, difference, slice, isEqual, size, map, pick, get, has } from 'lodash';
|
|
|
|
|
|
const styles = theme => ({
|
|
|
root: {
|
|
@@ -34,20 +34,47 @@ class ContainersTable extends Component {
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
+ * @param {*} e
|
|
|
*/
|
|
|
- selectAll(e) {
|
|
|
- console.log(e)
|
|
|
+ onAction(e, action) {
|
|
|
+ if (isEqual(size(this.state.selectedRows), 0) && has(this.props, 'notify')) {
|
|
|
+ this.props.notify('No se ha seleccionado ningún elemento')
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ this.props.onAction(e, action, this.state.selectedRows);
|
|
|
+ this.clearSelection()
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ onSelectAll = (e) => {
|
|
|
+ this.setState({
|
|
|
+ selectedRows: isEqual(size(this.state.selectedRows), size(this.props.rows)) ? [] : map(this.props.rows, item => {
|
|
|
+ return get(pick(item, 'id'), 'id')
|
|
|
+ })
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
- selectRow(e, id) {
|
|
|
+ onSelect(e, id) {
|
|
|
+ let index = indexOf(this.state.selectedRows, id)
|
|
|
+
|
|
|
this.setState({
|
|
|
- selectedRows: difference([id], this.state.selectedRows)
|
|
|
+ selectedRows: index === -1 ? concat(this.state.selectedRows, id) : difference(this.state.selectedRows, slice(this.state.selectedRows, index, index + 1))
|
|
|
})
|
|
|
+ }
|
|
|
|
|
|
- console.log(this.state.selectedRows)
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ clearSelection() {
|
|
|
+ this.setState({
|
|
|
+ selectedRows: []
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -55,32 +82,29 @@ class ContainersTable extends Component {
|
|
|
*/
|
|
|
render() {
|
|
|
const { classes, columns, rows } = this.props
|
|
|
+ const { selectedRows } = this.state
|
|
|
|
|
|
return (
|
|
|
<div>
|
|
|
- <Button variant='raised' color='primary' size='small' className={classes.buttons}>Arrancar</Button>
|
|
|
- <Button variant='raised' color='primary' size='small' className={classes.buttons}>Parar</Button>
|
|
|
- <Button variant='raised' color='primary' size='small' className={classes.buttons}>Reiniciar</Button>
|
|
|
+ <Button variant='raised' color='primary' size='small' className={classes.buttons} onClick={(e) => this.onAction(e, 'start')}>Arrancar</Button>
|
|
|
+ <Button variant='raised' color='primary' size='small' className={classes.buttons} onClick={(e) => this.onAction(e, 'stop')}>Parar</Button>
|
|
|
+ <Button variant='raised' color='primary' size='small' className={classes.buttons} onClick={(e) => this.onAction(e, 'restart')}>Reiniciar</Button>
|
|
|
<Paper className={classes.root}>
|
|
|
<Table>
|
|
|
<TableHead>
|
|
|
<TableRow>
|
|
|
- <TableCell>
|
|
|
- { <Checkbox color='primary' onClick={(e) => this.selectAll(e)} /> }
|
|
|
- </TableCell>
|
|
|
+ <TableCell children={<Checkbox color='primary' onClick={(e) => this.onSelectAll(e)} checked={isEqual(size(selectedRows), size(this.props.rows))} />} />
|
|
|
{columns.map(column =>
|
|
|
- <TableCell>{column.title}</TableCell>
|
|
|
+ <TableCell key={column.key} children={column.title} />
|
|
|
)}
|
|
|
</TableRow>
|
|
|
</TableHead>
|
|
|
<TableBody>
|
|
|
{rows.map(item =>
|
|
|
<TableRow key={item.id} className={classes.tableRow}>
|
|
|
- <TableCell>
|
|
|
- <Checkbox color='primary' onClick={(e) => this.selectRow(e, item.id)} />
|
|
|
- </TableCell>
|
|
|
+ <TableCell children={<Checkbox color='primary' onClick={(e) => this.onSelect(e, item.id)} checked={!isEqual(indexOf(selectedRows, item.id), -1)} />} />
|
|
|
{columns.map(column =>
|
|
|
- <TableCell>{item[column.key]}</TableCell>
|
|
|
+ <TableCell key={column.key} children={!column.options ? item[column.key] : column.options[item[column.key]].text} />
|
|
|
)}
|
|
|
</TableRow>
|
|
|
)}
|