【发布时间】:2019-12-29 16:48:28
【问题描述】:
我有一个选择,它显示了我从 api 检索到的一系列数据。 初始化时,它会显示已选择的选项卡,这很好。问题是当尝试选择更多选项或删除那些选项时,什么都没有发生。
import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
// Externals
import classNames from 'classnames';
import PropTypes from 'prop-types';
import compose from 'recompose/compose';
import validate from 'validate.js';
import _ from 'underscore';
// Material helpers
import { withStyles } from '@material-ui/core';
import moment from 'moment';
import 'bootstrap/dist/css/bootstrap.min.css';
import Select from 'react-select';
// Material components
import {
Button,
Checkbox,
Grid,
TextField,
Typography
} from '@material-ui/core';
// Shared utilities
import validators from 'common/validators';
// Shared components
import {
Portlet,
PortletContent,
PortletFooter
} from 'components';
// Component styles
import styles from './styles';
// Form validation schema
import schema from './schema';
class UserDetails extends Component {
constructor (props) {
super(props)
this.state = {
offices: [],
officesSelected: [],
isValid: false,
isLoading: false,
submitError: null
};
fetch(global.url_base+'/office' , {
method: 'POST',
body: JSON.stringify({
id_user: this.props.location.id_user
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(response => response.json())
.then(responseJson => {
let langString = responseJson;
let officesSelected = langString.map(item => ({ value: item.id_office, label: item.name_office, image: item.logo_office }));
this.setState({
officesSelected
});
}).catch(error => {
console.error(error);
});
}
componentWillMount() {
let currentComponent = this;
url = global.url_base+'/office'
fetch(url)
.then(response => response.json())
.then(responseJson => {
let langString = responseJson;
let offices = langString.map(item => ({ value: item.id_office, label: item.name_office }));
currentComponent.setState({
offices
});
}).catch(error => {
console.error(error);
});
}
handleChangeMultiple = (event) => {
const value = [];
for (let i = 0, l = event.length; i < l; i += 1) {
value.push(event[i].value);
}
const newState = { ...this.state };
var field = 'id_office';
newState.submitError = null;
newState.touched[field] = true;
newState.values[field] = value;
this.setState(newState, this.validateForm);
}
render() {
const { classes, className, ...rest } = this.props;
const {
values,
touched,
errors,
isValid,
submitError,
offices,
officesSelected
} = this.state;
const rootClassName = classNames(classes.root, className);
return (
<Select
className={classes.textField}
styles={selectStyles}
label="Office"
name="id_office"
onChange={this.handleChangeMultiple}
options={offices}
value={officesSelected}
isMulti
/>
如果我删除函数“value={officesSelected}”,我可以毫无问题地更改和添加多选选项卡,但我无法从 api 恢复已选择的选项卡数据。它总是显示好像我没有选择任何选项卡。
【问题讨论】:
-
嗨 Vortex,您似乎使用的是 react-select 而不是 material-ui select,正如问题标签所暗示的那样。此外,您提供的代码还不够。我看不到officeSelected 值何时发生变化。请提供更多代码,最好是 Codesandbox 上的工作示例
-
感谢您的回答。在这里我尽可能简洁地编辑了我的代码,我删除了几个不必要的部分。
标签: reactjs react-native jsx