mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2025-10-16 11:55:39 +00:00
Merge pull request #1166 from react-bootstrap-table/develop
20191116 release
This commit is contained in:
commit
acb9c9f61e
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
# Row selection
|
# Row selection
|
||||||
`react-bootstrap-table2` supports the row selection feature. By passing prop `selectRow` to enable row selection. When you enable this feature, `react-bootstrap-table2` will prepend a new selection column.
|
`react-bootstrap-table2` supports the row selection feature. By passing prop `selectRow` to enable row selection. When you enable this feature, `react-bootstrap-table2` will prepend a new selection column.
|
||||||
|
|
||||||
## Required
|
## Required
|
||||||
* [mode (**required**)](#mode)
|
* [mode (**required**)](#mode)
|
||||||
@ -11,6 +11,8 @@
|
|||||||
* [classes)](#classes)
|
* [classes)](#classes)
|
||||||
* [bgColor](#bgColor)
|
* [bgColor](#bgColor)
|
||||||
* [nonSelectable)](#nonSelectable)
|
* [nonSelectable)](#nonSelectable)
|
||||||
|
* [nonSelectableStyle](#nonSelectableStyle)
|
||||||
|
* [nonSelectableClasses](#nonSelectableClasses)
|
||||||
* [clickToSelect)](#clickToSelect)
|
* [clickToSelect)](#clickToSelect)
|
||||||
* [clickToExpand)](#clickToExpand)
|
* [clickToExpand)](#clickToExpand)
|
||||||
* [clickToEdit](#clickToEdit)
|
* [clickToEdit](#clickToEdit)
|
||||||
@ -139,6 +141,54 @@ const selectRow = {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <a name='nonSelectableStyle'>selectRow.nonSelectableStyle - [Object | Function]</a>
|
||||||
|
This prop allow you to customize the non selectable rows. `selectRow.nonSelectableStyle` accepts an style object
|
||||||
|
and a callback function for more flexible customization.
|
||||||
|
|
||||||
|
### Style Object
|
||||||
|
|
||||||
|
```js
|
||||||
|
const selectRow = {
|
||||||
|
mode: 'checkbox',
|
||||||
|
nonSelectable: [1, 3 ,5],
|
||||||
|
nonSelectableStyle: { backgroundColor: 'gray' }
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
### Callback Function
|
||||||
|
|
||||||
|
```js
|
||||||
|
const selectRow = {
|
||||||
|
mode: 'checkbox',
|
||||||
|
nonSelectable: [1, 3 ,5],
|
||||||
|
nonSelectableStyle: (row, rowIndex) => { return ...; }
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
### <a name='nonSelectableClasses'>selectRow.nonSelectableClasses - [String | Function]</a>
|
||||||
|
This prop allow you to set a custom class for the non selectable rows, or use a callback function for more
|
||||||
|
flexible customization
|
||||||
|
|
||||||
|
### String
|
||||||
|
|
||||||
|
```js
|
||||||
|
const selectRow = {
|
||||||
|
mode: 'checkbox',
|
||||||
|
nonSelectable: [1, 3 ,5],
|
||||||
|
nonSelectableClasses: 'my-class'
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
### Callback Function
|
||||||
|
|
||||||
|
```js
|
||||||
|
const selectRow = {
|
||||||
|
mode: 'checkbox',
|
||||||
|
nonSelectable: [1, 3 ,5],
|
||||||
|
nonSelectableClasses: (row, rowIndex) => { return ...; }
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
### <a name='clickToSelect'>selectRow.clickToSelect - [Bool]</a>
|
### <a name='clickToSelect'>selectRow.clickToSelect - [Bool]</a>
|
||||||
Allow user to select row by clicking on the row.
|
Allow user to select row by clicking on the row.
|
||||||
|
|
||||||
@ -149,7 +199,7 @@ const selectRow = {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
> Note: When you also enable [cellEdit](./cell-edit.md), the `selectRow.clickToSelect` will deactivate the functionality of cell editing
|
> Note: When you also enable [cellEdit](./cell-edit.md), the `selectRow.clickToSelect` will deactivate the functionality of cell editing
|
||||||
> If you want to click on row to select row and edit cell simultaneously, you are suppose to enable [`selectRow.clickToEdit`](#clickToEdit)
|
> If you want to click on row to select row and edit cell simultaneously, you are suppose to enable [`selectRow.clickToEdit`](#clickToEdit)
|
||||||
|
|
||||||
### <a name='clickToExpand'>selectRow.clickToExpand - [Bool]</a>
|
### <a name='clickToExpand'>selectRow.clickToExpand - [Bool]</a>
|
||||||
@ -307,7 +357,7 @@ const selectRow = {
|
|||||||
mode: 'checkbox',
|
mode: 'checkbox',
|
||||||
onSelectAll: (isSelect, rows, e) => {
|
onSelectAll: (isSelect, rows, e) => {
|
||||||
if (isSelect && SOME_CONDITION) {
|
if (isSelect && SOME_CONDITION) {
|
||||||
return [1, 3, 4]; // finally, key 1, 3, 4 will being selected
|
return [1, 3, 4]; // finally, key 1, 3, 4 will being selected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
61
packages/react-bootstrap-table2-example/examples/row-selection/non-selectable-rows-class.js
vendored
Normal file
61
packages/react-bootstrap-table2-example/examples/row-selection/non-selectable-rows-class.js
vendored
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
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 selectRow = {
|
||||||
|
mode: 'checkbox',
|
||||||
|
clickToSelect: true,
|
||||||
|
nonSelectable: [0, 2, 4],
|
||||||
|
nonSelectableClasses: 'row-index-bigger-than-2101'
|
||||||
|
};
|
||||||
|
|
||||||
|
const sourceCode = `\
|
||||||
|
import BootstrapTable from 'react-bootstrap-table-next';
|
||||||
|
|
||||||
|
const columns = [{
|
||||||
|
dataField: 'id',
|
||||||
|
text: 'Product ID'
|
||||||
|
}, {
|
||||||
|
dataField: 'name',
|
||||||
|
text: 'Product Name'
|
||||||
|
}, {
|
||||||
|
dataField: 'price',
|
||||||
|
text: 'Product Price'
|
||||||
|
}];
|
||||||
|
|
||||||
|
const selectRow = {
|
||||||
|
mode: 'checkbox',
|
||||||
|
clickToSelect: true,
|
||||||
|
nonSelectable: [0, 2, 4],
|
||||||
|
nonSelectableClasses: 'row-index-bigger-than-2101'
|
||||||
|
};
|
||||||
|
|
||||||
|
<BootstrapTable
|
||||||
|
keyField='id'
|
||||||
|
data={ products }
|
||||||
|
columns={ columns }
|
||||||
|
selectRow={ selectRow }
|
||||||
|
/>
|
||||||
|
`;
|
||||||
|
|
||||||
|
export default () => (
|
||||||
|
<div>
|
||||||
|
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow } />
|
||||||
|
<Code>{ sourceCode }</Code>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
61
packages/react-bootstrap-table2-example/examples/row-selection/non-selectable-rows-style.js
vendored
Normal file
61
packages/react-bootstrap-table2-example/examples/row-selection/non-selectable-rows-style.js
vendored
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
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 selectRow = {
|
||||||
|
mode: 'checkbox',
|
||||||
|
clickToSelect: true,
|
||||||
|
nonSelectable: [0, 2, 4],
|
||||||
|
nonSelectableStyle: { backgroundColor: 'gray' }
|
||||||
|
};
|
||||||
|
|
||||||
|
const sourceCode = `\
|
||||||
|
import BootstrapTable from 'react-bootstrap-table-next';
|
||||||
|
|
||||||
|
const columns = [{
|
||||||
|
dataField: 'id',
|
||||||
|
text: 'Product ID'
|
||||||
|
}, {
|
||||||
|
dataField: 'name',
|
||||||
|
text: 'Product Name'
|
||||||
|
}, {
|
||||||
|
dataField: 'price',
|
||||||
|
text: 'Product Price'
|
||||||
|
}];
|
||||||
|
|
||||||
|
const selectRow = {
|
||||||
|
mode: 'checkbox',
|
||||||
|
clickToSelect: true,
|
||||||
|
nonSelectable: [0, 2, 4],
|
||||||
|
nonSelectableStyle: { backgroundColor: 'gray' }
|
||||||
|
};
|
||||||
|
|
||||||
|
<BootstrapTable
|
||||||
|
keyField='id'
|
||||||
|
data={ products }
|
||||||
|
columns={ columns }
|
||||||
|
selectRow={ selectRow }
|
||||||
|
/>
|
||||||
|
`;
|
||||||
|
|
||||||
|
export default () => (
|
||||||
|
<div>
|
||||||
|
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow } />
|
||||||
|
<Code>{ sourceCode }</Code>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
@ -152,6 +152,8 @@ import HeaderStyleTable from 'examples/row-selection/header-style';
|
|||||||
import HideSelectAllTable from 'examples/row-selection/hide-select-all';
|
import HideSelectAllTable from 'examples/row-selection/hide-select-all';
|
||||||
import CustomSelectionTable from 'examples/row-selection/custom-selection';
|
import CustomSelectionTable from 'examples/row-selection/custom-selection';
|
||||||
import NonSelectableRowsTable from 'examples/row-selection/non-selectable-rows';
|
import NonSelectableRowsTable from 'examples/row-selection/non-selectable-rows';
|
||||||
|
import NonSelectableRowsStyleTable from 'examples/row-selection/non-selectable-rows-style';
|
||||||
|
import NonSelectableRowsClassTable from 'examples/row-selection/non-selectable-rows-class';
|
||||||
import SelectionBgColorTable from 'examples/row-selection/selection-bgcolor';
|
import SelectionBgColorTable from 'examples/row-selection/selection-bgcolor';
|
||||||
import SelectionHooks from 'examples/row-selection/selection-hooks';
|
import SelectionHooks from 'examples/row-selection/selection-hooks';
|
||||||
import HideSelectionColumnTable from 'examples/row-selection/hide-selection-column';
|
import HideSelectionColumnTable from 'examples/row-selection/hide-selection-column';
|
||||||
@ -410,6 +412,8 @@ storiesOf('Row Selection', module)
|
|||||||
.add('Custom Selection', () => <CustomSelectionTable />)
|
.add('Custom Selection', () => <CustomSelectionTable />)
|
||||||
.add('Selection Background Color', () => <SelectionBgColorTable />)
|
.add('Selection Background Color', () => <SelectionBgColorTable />)
|
||||||
.add('Not Selectabled Rows', () => <NonSelectableRowsTable />)
|
.add('Not Selectabled Rows', () => <NonSelectableRowsTable />)
|
||||||
|
.add('Not Selectabled Rows Style', () => <NonSelectableRowsStyleTable />)
|
||||||
|
.add('Not Selectabled Rows Class', () => <NonSelectableRowsClassTable />)
|
||||||
.add('Selection Hooks', () => <SelectionHooks />)
|
.add('Selection Hooks', () => <SelectionHooks />)
|
||||||
.add('Hide Selection Column', () => <HideSelectionColumnTable />)
|
.add('Hide Selection Column', () => <HideSelectionColumnTable />)
|
||||||
.add('Custom Selection Column Style', () => <SelectionColumnStyleTable />)
|
.add('Custom Selection Column Style', () => <SelectionColumnStyleTable />)
|
||||||
|
|||||||
@ -182,6 +182,8 @@ BootstrapTable.propTypes = {
|
|||||||
style: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
|
style: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
|
||||||
classes: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
|
classes: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
|
||||||
nonSelectable: PropTypes.array,
|
nonSelectable: PropTypes.array,
|
||||||
|
nonSelectableStyle: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
|
||||||
|
nonSelectableClasses: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
|
||||||
bgColor: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
|
bgColor: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
|
||||||
hideSelectColumn: PropTypes.bool,
|
hideSelectColumn: PropTypes.bool,
|
||||||
selectionRenderer: PropTypes.func,
|
selectionRenderer: PropTypes.func,
|
||||||
|
|||||||
@ -21,6 +21,7 @@ const Filters = (props) => {
|
|||||||
columns.forEach((column, i) => {
|
columns.forEach((column, i) => {
|
||||||
filterColumns.push(<FiltersCell
|
filterColumns.push(<FiltersCell
|
||||||
index={ i }
|
index={ i }
|
||||||
|
key={ column.dataField }
|
||||||
column={ column }
|
column={ column }
|
||||||
currFilters={ currFilters }
|
currFilters={ currFilters }
|
||||||
onExternalFilter={ onExternalFilter }
|
onExternalFilter={ onExternalFilter }
|
||||||
|
|||||||
@ -44,7 +44,7 @@ export default class ExpandCell extends Component {
|
|||||||
if (tabIndex !== -1) attrs.tabIndex = tabIndex;
|
if (tabIndex !== -1) attrs.tabIndex = tabIndex;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<td onClick={ this.handleClick } { ...attrs }>
|
<td className="expand-cell" onClick={ this.handleClick } { ...attrs }>
|
||||||
{
|
{
|
||||||
expandColumnRenderer ? expandColumnRenderer({
|
expandColumnRenderer ? expandColumnRenderer({
|
||||||
expandable,
|
expandable,
|
||||||
|
|||||||
@ -28,7 +28,7 @@ export default class ExpansionHeaderCell extends Component {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<th data-row-selection { ...attrs }>
|
<th className="expand-cell-header" data-row-selection { ...attrs }>
|
||||||
{
|
{
|
||||||
expandHeaderColumnRenderer ?
|
expandHeaderColumnRenderer ?
|
||||||
expandHeaderColumnRenderer({ isAnyExpands }) :
|
expandHeaderColumnRenderer({ isAnyExpands }) :
|
||||||
|
|||||||
@ -9,6 +9,7 @@ export default (Component) => {
|
|||||||
const key = props.value;
|
const key = props.value;
|
||||||
const selected = _.contains(selectRow.selected, key);
|
const selected = _.contains(selectRow.selected, key);
|
||||||
const selectable = !selectRow.nonSelectable || !_.contains(selectRow.nonSelectable, key);
|
const selectable = !selectRow.nonSelectable || !_.contains(selectRow.nonSelectable, key);
|
||||||
|
const notSelectable = _.contains(selectRow.nonSelectable, key);
|
||||||
|
|
||||||
let {
|
let {
|
||||||
style,
|
style,
|
||||||
@ -38,6 +39,22 @@ export default (Component) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (notSelectable) {
|
||||||
|
const notSelectableStyle = _.isFunction(selectRow.nonSelectableStyle)
|
||||||
|
? selectRow.nonSelectableStyle(props.row, props.rowIndex)
|
||||||
|
: selectRow.nonSelectableStyle;
|
||||||
|
|
||||||
|
const notSelectableClasses = _.isFunction(selectRow.nonSelectableClasses)
|
||||||
|
? selectRow.nonSelectableClasses(props.row, props.rowIndex)
|
||||||
|
: selectRow.nonSelectableClasses;
|
||||||
|
|
||||||
|
style = {
|
||||||
|
...style,
|
||||||
|
...notSelectableStyle
|
||||||
|
};
|
||||||
|
className = cs(className, notSelectableClasses) || undefined;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Component
|
<Component
|
||||||
{ ...props }
|
{ ...props }
|
||||||
|
|||||||
@ -86,7 +86,7 @@ export default class SelectionCell extends Component {
|
|||||||
<BootstrapContext.Consumer>
|
<BootstrapContext.Consumer>
|
||||||
{
|
{
|
||||||
({ bootstrap4 }) => (
|
({ bootstrap4 }) => (
|
||||||
<td onClick={ this.handleClick } { ...attrs }>
|
<td className="selection-cell" onClick={ this.handleClick } { ...attrs }>
|
||||||
{
|
{
|
||||||
selectionRenderer ? selectionRenderer({
|
selectionRenderer ? selectionRenderer({
|
||||||
mode: inputType,
|
mode: inputType,
|
||||||
|
|||||||
@ -112,7 +112,7 @@ export default class SelectionHeaderCell extends Component {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<th data-row-selection { ...attrs }>{ content }</th>
|
<th className="selection-cell-header" data-row-selection { ...attrs }>{ content }</th>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
exports[`<SelectionCell /> render should render component correctly 1`] = `
|
exports[`<SelectionCell /> render should render component correctly 1`] = `
|
||||||
<td
|
<td
|
||||||
|
className="selection-cell"
|
||||||
onClick={[Function]}
|
onClick={[Function]}
|
||||||
>
|
>
|
||||||
<input
|
<input
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user