mirror of
https://github.com/gosticks/react-bootstrap-table2.git
synced 2025-10-16 11:55:39 +00:00
fix #947
This commit is contained in:
parent
db612eaa99
commit
0d0d1a8913
@ -22,6 +22,7 @@
|
||||
* [selectionRenderer](#selectionRenderer)
|
||||
* [selectionHeaderRenderer](#selectionHeaderRenderer)
|
||||
* [headerColumnStyle](#headerColumnStyle)
|
||||
* [selectColumnStyle](#selectColumnStyle)
|
||||
|
||||
### <a name="mode">selectRow.mode - [String]</a>
|
||||
|
||||
@ -225,6 +226,42 @@ const selectRow = {
|
||||
};
|
||||
```
|
||||
|
||||
### <a name='selectColumnStyle'>selectRow.selectColumnStyle - [Object | Function]</a>
|
||||
A way to custome the selection cell. `selectColumnStyle` not only accept a simple style object but also a callback function for more flexible customization:
|
||||
|
||||
### Style Object
|
||||
|
||||
```js
|
||||
const selectRow = {
|
||||
mode: 'checkbox',
|
||||
selectColumnStyle: { backgroundColor: 'blue' }
|
||||
};
|
||||
```
|
||||
|
||||
### Callback Function
|
||||
If a callback function present, you can get below information to custom the selection cell:
|
||||
|
||||
* `checked`: Whether current row is seleccted or not
|
||||
* `disabled`: Whether current row is disabled or not
|
||||
* `rowIndex`: Current row index
|
||||
* `rowKey`: Current row key
|
||||
|
||||
|
||||
```js
|
||||
const selectRow = {
|
||||
mode: 'checkbox',
|
||||
selectColumnStyle: ({
|
||||
checked,
|
||||
disabled,
|
||||
rowIndex,
|
||||
rowKey
|
||||
}) => (
|
||||
// ....
|
||||
return { backgroundColor: 'blue' };
|
||||
)
|
||||
};
|
||||
```
|
||||
|
||||
### <a name='onSelect'>selectRow.onSelect - [Function]</a>
|
||||
This callback function will be called when a row is select/unselect and pass following three arguments:
|
||||
`row`, `isSelect`, `rowIndex` and `e`.
|
||||
|
||||
110
packages/react-bootstrap-table2-example/examples/row-selection/select-column-style.js
vendored
Normal file
110
packages/react-bootstrap-table2-example/examples/row-selection/select-column-style.js
vendored
Normal file
@ -0,0 +1,110 @@
|
||||
/* eslint no-unused-vars: 0 */
|
||||
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(2);
|
||||
|
||||
const columns = [{
|
||||
dataField: 'id',
|
||||
text: 'Product ID'
|
||||
}, {
|
||||
dataField: 'name',
|
||||
text: 'Product Name'
|
||||
}, {
|
||||
dataField: 'price',
|
||||
text: 'Product Price'
|
||||
}];
|
||||
|
||||
const selectRow1 = {
|
||||
mode: 'checkbox',
|
||||
clickToSelect: true,
|
||||
selectColumnStyle: {
|
||||
backgroundColor: 'grey'
|
||||
}
|
||||
};
|
||||
|
||||
const sourceCode1 = `\
|
||||
import BootstrapTable from 'react-bootstrap-table-next';
|
||||
|
||||
const columns = ...
|
||||
|
||||
const selectRow = {
|
||||
mode: 'checkbox',
|
||||
clickToSelect: true,
|
||||
selectColumnStyle: {
|
||||
backgroundColor: 'grey'
|
||||
}
|
||||
};
|
||||
|
||||
<BootstrapTable
|
||||
keyField='id'
|
||||
data={ products }
|
||||
columns={ columns }
|
||||
selectRow={ selectRow }
|
||||
/>
|
||||
`;
|
||||
|
||||
const selectRow2 = {
|
||||
mode: 'checkbox',
|
||||
clickToSelect: true,
|
||||
selectColumnStyle: ({
|
||||
checked,
|
||||
disabled,
|
||||
rowIndex,
|
||||
rowKey
|
||||
}) => {
|
||||
if (checked) {
|
||||
return {
|
||||
backgroundColor: 'yellow'
|
||||
};
|
||||
}
|
||||
return {
|
||||
backgroundColor: 'pink'
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const sourceCode2 = `\
|
||||
import BootstrapTable from 'react-bootstrap-table-next';
|
||||
|
||||
const columns = ...
|
||||
|
||||
const selectRow = {
|
||||
mode: 'checkbox',
|
||||
clickToSelect: true,
|
||||
selectColumnStyle: ({
|
||||
checked,
|
||||
disabled,
|
||||
rowIndex,
|
||||
rowKey
|
||||
}) => {
|
||||
if (checked) {
|
||||
return {
|
||||
backgroundColor: 'yellow'
|
||||
};
|
||||
}
|
||||
return {
|
||||
backgroundColor: 'pink'
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
<BootstrapTable
|
||||
keyField='id'
|
||||
data={ products }
|
||||
columns={ columns }
|
||||
selectRow={ selectRow }
|
||||
/>
|
||||
`;
|
||||
|
||||
export default () => (
|
||||
<div>
|
||||
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow1 } />
|
||||
<Code>{ sourceCode1 }</Code>
|
||||
<BootstrapTable keyField="id" data={ products } columns={ columns } selectRow={ selectRow2 } />
|
||||
<Code>{ sourceCode2 }</Code>
|
||||
</div>
|
||||
);
|
||||
@ -150,6 +150,7 @@ import NonSelectableRowsTable from 'examples/row-selection/non-selectable-rows';
|
||||
import SelectionBgColorTable from 'examples/row-selection/selection-bgcolor';
|
||||
import SelectionHooks from 'examples/row-selection/selection-hooks';
|
||||
import HideSelectionColumnTable from 'examples/row-selection/hide-selection-column';
|
||||
import SelectionColumnStyleTable from 'examples/row-selection/select-column-style';
|
||||
import SelectionColumnPositionTable from 'examples/row-selection/selection-column-position';
|
||||
|
||||
// work on row expand
|
||||
@ -396,6 +397,7 @@ storiesOf('Row Selection', module)
|
||||
.add('Not Selectabled Rows', () => <NonSelectableRowsTable />)
|
||||
.add('Selection Hooks', () => <SelectionHooks />)
|
||||
.add('Hide Selection Column', () => <HideSelectionColumnTable />)
|
||||
.add('Custom Selection Column Style', () => <SelectionColumnStyleTable />)
|
||||
.add('Selection Column Position', () => <SelectionColumnPositionTable />);
|
||||
|
||||
storiesOf('Row Expand', module)
|
||||
|
||||
@ -169,6 +169,7 @@ BootstrapTable.propTypes = {
|
||||
selectionRenderer: PropTypes.func,
|
||||
selectionHeaderRenderer: PropTypes.func,
|
||||
headerColumnStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
|
||||
selectColumnStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
|
||||
selectColumnPosition: PropTypes.oneOf([
|
||||
Const.INDICATOR_POSITION_LEFT,
|
||||
Const.INDICATOR_POSITION_RIGHT
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
import React, { Component } from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import Const from '../const';
|
||||
import _ from '../utils';
|
||||
import { BootstrapContext } from '../contexts/bootstrap';
|
||||
|
||||
export default class SelectionCell extends Component {
|
||||
@ -17,7 +18,8 @@ export default class SelectionCell extends Component {
|
||||
rowIndex: PropTypes.number,
|
||||
tabIndex: PropTypes.number,
|
||||
clickToSelect: PropTypes.bool,
|
||||
selectionRenderer: PropTypes.func
|
||||
selectionRenderer: PropTypes.func,
|
||||
selectColumnStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.func])
|
||||
}
|
||||
|
||||
constructor() {
|
||||
@ -31,7 +33,8 @@ export default class SelectionCell extends Component {
|
||||
this.props.selected !== nextProps.selected ||
|
||||
this.props.disabled !== nextProps.disabled ||
|
||||
this.props.rowKey !== nextProps.rowKey ||
|
||||
this.props.tabIndex !== nextProps.tabIndex;
|
||||
this.props.tabIndex !== nextProps.tabIndex ||
|
||||
this.props.selectColumnStyle !== nextProps.selectColumnStyle;
|
||||
|
||||
return shouldUpdate;
|
||||
}
|
||||
@ -57,17 +60,28 @@ export default class SelectionCell extends Component {
|
||||
|
||||
render() {
|
||||
const {
|
||||
rowKey,
|
||||
mode: inputType,
|
||||
selected,
|
||||
disabled,
|
||||
tabIndex,
|
||||
rowIndex,
|
||||
selectionRenderer
|
||||
selectionRenderer,
|
||||
selectColumnStyle
|
||||
} = this.props;
|
||||
|
||||
const attrs = {};
|
||||
if (tabIndex !== -1) attrs.tabIndex = tabIndex;
|
||||
|
||||
attrs.style = _.isFunction(selectColumnStyle) ?
|
||||
selectColumnStyle({
|
||||
checked: selected,
|
||||
disabled,
|
||||
rowIndex,
|
||||
rowKey
|
||||
}) :
|
||||
selectColumnStyle;
|
||||
|
||||
return (
|
||||
<BootstrapContext.Consumer>
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user