【发布时间】:2019-06-17 07:41:56
【问题描述】:
编辑数据库中现有的文章对象时,我无法更改类别的下拉值。类别是一个对象,它是我的文章对象上的一个属性(字段)。
我在网上进行了很多研究,但无法解决这个特定问题。我可以成功更改值并提交字符串字段的更改 - 例如文章标题和文章正文。
这是代码。问题似乎出在 handleChange() 和/或 <Input type="select" name="category" ...>
class ArticleEdit extends Component {
emptyItem = {
articleTitle: '',
articleText: '',
imageUrl: '',
category: {},
tags: []
};
constructor(props) {
super(props);
this.state = {
item: this.emptyItem,
categories: []
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
async componentDidMount() {
let allCategories = [];
if (this.props.match.params.articleId !== 'new') {
const article = await (await fetch(`/articles/${this.props.match.params.articleId}`)).json();
fetch ('/categories')
.then(response => {
return response.json();
}).then(data => {
allCategories = data._embedded.categoryList.map(category => {
return category
});
this.setState({item: article, categories: allCategories});
});
}
}
handleChange(event) {
const target = event.target
const name = target.name;
const value = target.value;
let item = {...this.state.item};
item[name] = value;
this.setState({item});
console.log("The category you selected is: " + item.category.categoryName);
alert("The category you selected is: " + item.category.categoryName);
}
async handleSubmit(event) {
event.preventDefault();
const {item} = this.state;
await fetch((item.articleId) ? '/articles/' + (item.articleId) : '/articles', {
method: (item.articleId) ? 'PUT' : 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(item),
});
this.props.history.push('/articles');
}
render() {
const {item} = this.state;
const categoriesToDisplay = this.state.categories
const categoryOptionItems = categoriesToDisplay.map(category =>
<option key={category.categoryId} value={category.categoryName}>{category.categoryName}</option>
);
const title = <h2>{item.articleId ? 'Edit Article' : 'Create Article'}</h2>;
return (
<div>
<Header/>
<Container>
{title}
<Form onSubmit={this.handleSubmit}>
<FormGroup>
<Label for="articleTitle">Title</Label>
<Input type="text" name="articleTitle" id="articleTitle" value={item.articleTitle || ''}
onChange={this.handleChange} autoComplete="name"/>
</FormGroup>
<FormGroup>
<Label for="articleText">Text</Label>
<Input type="textarea" name="articleText" id="articleText" value={item.articleText || ''}
onChange={this.handleChange}/>
</FormGroup>
<FormGroup>
<Label for="imageUrl">Image URL</Label>
<Input type="text" name="imageUrl" id="imageUrl" value={item.imageUrl || ''}
onChange={this.handleChange}/>
</FormGroup>
<div className="row">
<FormGroup className="col-md-6 mb-3">
<Label for="category">Select Category</Label>
<Input type="select" name="category" id="category" value={item.category.categoryName || ''}
onChange={value => this.handleChange({target : {name : 'categoryName', value}})}>
<option value="">Select</option>
{categoryOptionItems}
</Input>
</FormGroup>
<FormGroup className="col-md-6 mb-3">
<Label for="taqs">Select Tag(s)</Label>
<Input type="select" name="taqs" id="taqs" value={item.tags.map(tag => tag.tagName) || ''} onChange={this.handleChange} multiple>
<option>Depression</option>
<option>Anxiety</option>
<option>Phobias</option>
<option>Psychotherapy</option>
<option>Mindfulness</option>
<option>Religion</option>
<option>Supernatural</option>
<option>Healing</option>
<option>Eastern Practices</option>
<option>Motivation</option>
<option>Relationships</option>
<option>Positive Thinking</option>
<option>Emotions</option>
<option>Self-Help</option>
<option>Time Management</option>
<option>Learning From Experience</option>
<option>Personal Development Methods</option>
</Input>
</FormGroup>
</div>
<FormGroup className="float-right">
<Button color="primary" type="submit">Save</Button>{' '}
<Button color="secondary" tag={Link} to="/articles">Cancel</Button>
</FormGroup>
</Form>
</Container>
<Footer/>
</div>
);
}
}
当我点击文章列表页面上的“编辑”按钮并转到现有文章形式的页面时,我看到在“选择类别”下拉列表中预先选择了我的文章类别。
当我尝试选择另一个类别时,它会发出警报并记录现有类别的名称,请参阅https://ibb.co/k4sj1jq。之后,我在下拉列表中看到现有类别并且无法更改它。
我如何才能成功地为这篇文章选择(并提交)一个新类别?
提前谢谢你。
【问题讨论】:
标签: javascript reactjs