mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2025-10-16 11:55:39 +00:00
add selection management example
This commit is contained in:
parent
6f9361934a
commit
d5ddd8c3af
144
packages/react-bootstrap-table2-example/examples/row-selection/selection-management.js
vendored
Normal file
144
packages/react-bootstrap-table2-example/examples/row-selection/selection-management.js
vendored
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import BootstrapTable from 'react-bootstrap-table-next';
|
||||||
|
import Code from 'components/common/code-block';
|
||||||
|
import { productsGenerator } from 'utils/common';
|
||||||
|
|
||||||
|
const products = productsGenerator();
|
||||||
|
|
||||||
|
const columns = [{
|
||||||
|
dataField: 'id',
|
||||||
|
text: 'Product ID'
|
||||||
|
}, {
|
||||||
|
dataField: 'name',
|
||||||
|
text: 'Product Name'
|
||||||
|
}, {
|
||||||
|
dataField: 'price',
|
||||||
|
text: 'Product Price'
|
||||||
|
}];
|
||||||
|
|
||||||
|
const sourceCode = `\
|
||||||
|
import BootstrapTable from 'react-bootstrap-table-next';
|
||||||
|
|
||||||
|
class SelectionManagment extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = { selected: [0, 1] };
|
||||||
|
}
|
||||||
|
|
||||||
|
handleBtnClick = () => {
|
||||||
|
if (!this.state.selected.includes(2)) {
|
||||||
|
this.setState(() => ({
|
||||||
|
selected: [...this.state.selected, 2]
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
this.setState(() => ({
|
||||||
|
selected: this.state.selected.filter(x => x !== 2)
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleOnSelect = (row, isSelect) => {
|
||||||
|
if (isSelect) {
|
||||||
|
this.setState(() => ({
|
||||||
|
selected: [...this.state.selected, row.id]
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
this.setState(() => ({
|
||||||
|
selected: this.state.selected.filter(x => x !== row.id)
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleOnSelectAll = (isSelect, rows) => {
|
||||||
|
const ids = rows.map(r => r.id);
|
||||||
|
if (isSelect) {
|
||||||
|
this.setState(() => ({
|
||||||
|
selected: ids
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
this.setState(() => ({
|
||||||
|
selected: []
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const selectRow = {
|
||||||
|
mode: 'checkbox',
|
||||||
|
clickToSelect: true,
|
||||||
|
selected: this.state.selected,
|
||||||
|
onSelect: this.handleOnSelect,
|
||||||
|
onSelectAll: this.handleOnSelectAll
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<button className="btn btn-success" onClick={ this.handleBtnClick }>Select/UnSelect 3rd row</button>
|
||||||
|
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow } />
|
||||||
|
<Code>{ sourceCode }</Code>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
export default class SelectionManagment extends React.Component {
|
||||||
|
constructor(props) {
|
||||||
|
super(props);
|
||||||
|
this.state = { selected: [0, 1] };
|
||||||
|
}
|
||||||
|
|
||||||
|
handleBtnClick = () => {
|
||||||
|
if (!this.state.selected.includes(2)) {
|
||||||
|
this.setState(() => ({
|
||||||
|
selected: [...this.state.selected, 2]
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
this.setState(() => ({
|
||||||
|
selected: this.state.selected.filter(x => x !== 2)
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleOnSelect = (row, isSelect) => {
|
||||||
|
if (isSelect) {
|
||||||
|
this.setState(() => ({
|
||||||
|
selected: [...this.state.selected, row.id]
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
this.setState(() => ({
|
||||||
|
selected: this.state.selected.filter(x => x !== row.id)
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleOnSelectAll = (isSelect, rows) => {
|
||||||
|
const ids = rows.map(r => r.id);
|
||||||
|
if (isSelect) {
|
||||||
|
this.setState(() => ({
|
||||||
|
selected: ids
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
this.setState(() => ({
|
||||||
|
selected: []
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const selectRow = {
|
||||||
|
mode: 'checkbox',
|
||||||
|
clickToSelect: true,
|
||||||
|
selected: this.state.selected,
|
||||||
|
onSelect: this.handleOnSelect,
|
||||||
|
onSelectAll: this.handleOnSelectAll
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<button className="btn btn-success" onClick={ this.handleBtnClick }>Select/UnSelect 3rd row</button>
|
||||||
|
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow } />
|
||||||
|
<Code>{ sourceCode }</Code>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -79,6 +79,7 @@ import SingleSelectionTable from 'examples/row-selection/single-selection';
|
|||||||
import MultipleSelectionTable from 'examples/row-selection/multiple-selection';
|
import MultipleSelectionTable from 'examples/row-selection/multiple-selection';
|
||||||
import ClickToSelectTable from 'examples/row-selection/click-to-select';
|
import ClickToSelectTable from 'examples/row-selection/click-to-select';
|
||||||
import DefaultSelectTable from 'examples/row-selection/default-select';
|
import DefaultSelectTable from 'examples/row-selection/default-select';
|
||||||
|
import SelectionManagement from 'examples/row-selection/selection-management';
|
||||||
import ClickToSelectWithCellEditTable from 'examples/row-selection/click-to-select-with-cell-edit';
|
import ClickToSelectWithCellEditTable from 'examples/row-selection/click-to-select-with-cell-edit';
|
||||||
import SelectionStyleTable from 'examples/row-selection/selection-style';
|
import SelectionStyleTable from 'examples/row-selection/selection-style';
|
||||||
import SelectionClassTable from 'examples/row-selection/selection-class';
|
import SelectionClassTable from 'examples/row-selection/selection-class';
|
||||||
@ -193,6 +194,7 @@ storiesOf('Row Selection', module)
|
|||||||
.add('Multiple Selection', () => <MultipleSelectionTable />)
|
.add('Multiple Selection', () => <MultipleSelectionTable />)
|
||||||
.add('Click to Select', () => <ClickToSelectTable />)
|
.add('Click to Select', () => <ClickToSelectTable />)
|
||||||
.add('Default Select', () => <DefaultSelectTable />)
|
.add('Default Select', () => <DefaultSelectTable />)
|
||||||
|
.add('Selection Management', () => <SelectionManagement />)
|
||||||
.add('Click to Select and Edit Cell', () => <ClickToSelectWithCellEditTable />)
|
.add('Click to Select and Edit Cell', () => <ClickToSelectWithCellEditTable />)
|
||||||
.add('Selection Style', () => <SelectionStyleTable />)
|
.add('Selection Style', () => <SelectionStyleTable />)
|
||||||
.add('Selection Class', () => <SelectionClassTable />)
|
.add('Selection Class', () => <SelectionClassTable />)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user