Skip to content

Commit 4d76d88

Browse files
authored
fix #945 (#967)
1 parent 1cd31dc commit 4d76d88

File tree

4 files changed

+222
-6
lines changed

4 files changed

+222
-6
lines changed

packages/react-bootstrap-table2-editor/README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ const columns = [
8989
In the following, we go though all the predefined editors:
9090

9191
### Dropdown Editor
92-
Dropdown editor give a select menu to choose a data from a list, the `editor.options` is required property for dropdown editor.
92+
Dropdown editor give a select menu to choose a data from a list. When use dropdown editor, either `editor.options` or `editor.getOptions` should be required prop.
93+
94+
#### editor.options
95+
This is most simple case for assign the dropdown options data directly.
9396

9497
```js
9598
import { Type } from 'react-bootstrap-table2-editor';
@@ -119,6 +122,46 @@ const columns = [
119122
}];
120123
```
121124

125+
#### editor.getOptions
126+
It is much flexible which accept a function and you can assign the dropdown options dynamically.
127+
128+
There are two case for `getOptions`:
129+
130+
* *Synchronous*: Just return the options array in `getOptions` callback function
131+
* *Asynchronous*: Call `setOptions` function argument when you get the options from remote.
132+
133+
134+
```js
135+
// Synchronous
136+
137+
const columns = [
138+
..., {
139+
dataField: 'type',
140+
text: 'Job Type',
141+
editor: {
142+
type: Type.SELECT,
143+
getOptions: () => [.....]
144+
}
145+
}];
146+
147+
// Asynchronous
148+
149+
const columns = [
150+
..., {
151+
dataField: 'type',
152+
text: 'Job Type',
153+
editor: {
154+
type: Type.SELECT,
155+
getOptions: (setOptions) => {
156+
setTimeout(() => setOptions([...]), 1500);
157+
}
158+
}
159+
}];
160+
161+
```
162+
163+
[here](https://react-bootstrap-table.github.io/react-bootstrap-table2/storybook/index.html?selectedKind=Cell%20Editing&selectedStory=Dropdown%20Editor%20with%20Dynamic%20Options) is an online example.
164+
122165
### Date Editor
123166
Date editor is use `<input type="date">`, the configuration is very simple:
124167

packages/react-bootstrap-table2-editor/src/dropdown-editor.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,32 @@ import cs from 'classnames';
44
import PropTypes from 'prop-types';
55

66
class DropDownEditor extends Component {
7+
constructor(props) {
8+
super(props);
9+
let options = props.options;
10+
if (props.getOptions) {
11+
options = props.getOptions(this.setOptions.bind(this)) || [];
12+
}
13+
this.state = { options };
14+
}
15+
716
componentDidMount() {
817
const { defaultValue, didMount } = this.props;
918
this.select.value = defaultValue;
1019
this.select.focus();
1120
if (didMount) didMount();
1221
}
1322

23+
setOptions(options) {
24+
this.setState({ options });
25+
}
26+
1427
getValue() {
1528
return this.select.value;
1629
}
1730

1831
render() {
19-
const { defaultValue, didMount, className, options, ...rest } = this.props;
32+
const { defaultValue, didMount, getOptions, className, ...rest } = this.props;
2033
const editorClass = cs('form-control editor edit-select', className);
2134

2235
const attr = {
@@ -31,7 +44,7 @@ class DropDownEditor extends Component {
3144
defaultValue={ defaultValue }
3245
>
3346
{
34-
options.map(({ label, value }) => (
47+
this.state.options.map(({ label, value }) => (
3548
<option key={ value } value={ value }>{ label }</option>
3649
))
3750
}
@@ -52,13 +65,16 @@ DropDownEditor.propTypes = {
5265
label: PropTypes.string,
5366
value: PropTypes.any
5467
}))
55-
]).isRequired,
56-
didMount: PropTypes.func
68+
]),
69+
didMount: PropTypes.func,
70+
getOptions: PropTypes.func
5771
};
5872
DropDownEditor.defaultProps = {
5973
className: '',
6074
defaultValue: '',
6175
style: {},
62-
didMount: undefined
76+
options: [],
77+
didMount: undefined,
78+
getOptions: undefined
6379
};
6480
export default DropDownEditor;
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/* eslint react/prefer-stateless-function: 0 */
2+
import React from 'react';
3+
4+
import BootstrapTable from 'react-bootstrap-table-next';
5+
import cellEditFactory, { Type } from 'react-bootstrap-table2-editor';
6+
import Code from 'components/common/code-block';
7+
import { jobsGenerator } from 'utils/common';
8+
9+
const jobs = jobsGenerator().map(j => ({
10+
...j,
11+
type2: j.type
12+
}));
13+
14+
const columns = [{
15+
dataField: 'id',
16+
text: 'Job ID'
17+
}, {
18+
dataField: 'name',
19+
text: 'Job Name'
20+
}, {
21+
dataField: 'owner',
22+
text: 'Job Owner'
23+
}, {
24+
dataField: 'type',
25+
text: 'Job Type1',
26+
editor: {
27+
type: Type.SELECT,
28+
getOptions: () => [{
29+
value: 'A',
30+
label: 'A'
31+
}, {
32+
value: 'B',
33+
label: 'B'
34+
}, {
35+
value: 'C',
36+
label: 'C'
37+
}, {
38+
value: 'D',
39+
label: 'D'
40+
}, {
41+
value: 'E',
42+
label: 'E'
43+
}]
44+
}
45+
}, {
46+
dataField: 'type2',
47+
text: 'Job Type2',
48+
editor: {
49+
type: Type.SELECT,
50+
getOptions: (setOptions) => {
51+
setTimeout(() => {
52+
setOptions([{
53+
value: 'A',
54+
label: 'A'
55+
}, {
56+
value: 'B',
57+
label: 'B'
58+
}, {
59+
value: 'C',
60+
label: 'C'
61+
}, {
62+
value: 'D',
63+
label: 'D'
64+
}, {
65+
value: 'E',
66+
label: 'E'
67+
}]);
68+
}, 2000);
69+
}
70+
}
71+
}];
72+
73+
const sourceCode = `\
74+
import BootstrapTable from 'react-bootstrap-table-next';
75+
import cellEditFactory, { Type } from 'react-bootstrap-table2-editor';
76+
77+
const columns = [{
78+
dataField: 'id',
79+
text: 'Job ID'
80+
}, {
81+
dataField: 'name',
82+
text: 'Job Name'
83+
}, {
84+
dataField: 'owner',
85+
text: 'Job Owner'
86+
}, {
87+
dataField: 'type',
88+
text: 'Job Type1',
89+
editor: {
90+
type: Type.SELECT,
91+
getOptions: () => [{
92+
value: 'A',
93+
label: 'A'
94+
}, {
95+
value: 'B',
96+
label: 'B'
97+
}, {
98+
value: 'C',
99+
label: 'C'
100+
}, {
101+
value: 'D',
102+
label: 'D'
103+
}, {
104+
value: 'E',
105+
label: 'E'
106+
}]
107+
}
108+
}, {
109+
dataField: 'type2',
110+
text: 'Job Type2',
111+
editor: {
112+
type: Type.SELECT,
113+
getOptions: (setOptions) => {
114+
setTimeout(() => {
115+
setOptions([{
116+
value: 'A',
117+
label: 'A'
118+
}, {
119+
value: 'B',
120+
label: 'B'
121+
}, {
122+
value: 'C',
123+
label: 'C'
124+
}, {
125+
value: 'D',
126+
label: 'D'
127+
}, {
128+
value: 'E',
129+
label: 'E'
130+
}]);
131+
}, 2000);
132+
}
133+
}
134+
}];
135+
136+
<BootstrapTable
137+
keyField="id"
138+
data={ jobs }
139+
columns={ columns }
140+
cellEdit={ cellEditFactory({ mode: 'click', blurToSave: true }) }
141+
/>
142+
`;
143+
144+
export default () => (
145+
<div>
146+
<h3>Dropdown Editor with Dynamic Options</h3>
147+
<BootstrapTable
148+
keyField="id"
149+
data={ jobs }
150+
columns={ columns }
151+
cellEdit={ cellEditFactory({ mode: 'click', blurToSave: true }) }
152+
/>
153+
<Code>{ sourceCode }</Code>
154+
</div>
155+
);

packages/react-bootstrap-table2-example/stories/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ import EditorStyleTable from 'examples/cell-edit/editor-style-table';
125125
import EditorClassTable from 'examples/cell-edit/editor-class-table';
126126
import DBClickEditWithSelection from 'examples/cell-edit/dbclick-to-edit-with-selection-table';
127127
import DropdownEditorTable from 'examples/cell-edit/dropdown-editor-table';
128+
import DropdownEditorWithDynamicOptionsTable from 'examples/cell-edit/dropdown-editor-with-dynamic-options-table';
128129
import TextareaEditorTable from 'examples/cell-edit/textarea-editor-table';
129130
import CheckboxEditorTable from 'examples/cell-edit/checkbox-editor-table';
130131
import DateEditorTable from 'examples/cell-edit/date-editor-table';
@@ -368,6 +369,7 @@ storiesOf('Cell Editing', module)
368369
.add('Custom Editor Style', () => <EditorStyleTable />)
369370
.add('DoubleClick to Edit with Selection', () => <DBClickEditWithSelection />)
370371
.add('Dropdown Editor', () => <DropdownEditorTable />)
372+
.add('Dropdown Editor with Dynamic Options', () => <DropdownEditorWithDynamicOptionsTable />)
371373
.add('Textarea Editor', () => <TextareaEditorTable />)
372374
.add('Checkbox Editor', () => <CheckboxEditorTable />)
373375
.add('Date Editor', () => <DateEditorTable />)

0 commit comments

Comments
 (0)