React后台管理系统-品类选择器二级联动组件
1.页面大致是这个样子
2.页面结构
-
<div className="col-md-10">
-
<select name="" onChange={(e) => this.onFirstCategoryChange(e)} className="form-control cate-select">
-
<option value="">请选择一级分类</option>
-
{
-
//箭头函数=>右边,加上了{}就需要return,不加就不需要return
-
this.state.firstCategoryList.map(
-
(category, index) => <option value={category.id} key={index}>{category.name}</option>)
-
}
-
</select>
-
{ this.state.secondCategoryList.length ?
-
<select name="" onChange={(e) => this.onSecondCategoryChange(e)} className="form-control cate-select">
-
<option value="">请选择二级分类</option>
-
{
-
this.state.secondCategoryList.map(
-
(category, index)=> <option value={category.id} key={index}>{category.name}</option>
-
)
-
}
-
</select> : null
-
}
-
</div>
3.定义state里边的数据
-
this.state = {
-
firstCategoryList : [],
-
firstCategoryId : 0,
-
secondCategoryList : [],
-
secondCategoryId : 0,
-
}
监听select选择框,当一级品类和二级品类改变的时候, 更新state里边firstCategoryId和secondCategoryId的值
-
//一级品类改变事件
-
onFirstCategoryChange(e){
-
//取一级品类的值,没有的话为0
-
let newValue=e.target.value || 0;
-
this.setState({
-
-
firstCategoryId : newValue,
-
//当一级品类改变时清空二级品类
-
secondCategoryList : [],
-
secondCategoryId : 0,
-
},() => {
-
//加载二级分类
-
this.loadSecondCategory()
-
})
-
}
-
//二级品类改变事件
-
onSecondCategoryChange(e){
-
//取一级品类的值,没有的话为0
-
let newValue=e.target.value || 0;
-
this.setState({
-
secondCategoryId : newValue,
-
},() => {
-
//加载二级分类
-
this.onPropsCategoryChange();
-
})
-
}
加载一级分类
-
//加载一级分类
-
loadFirstCategory(){
-
_product.getCategoryList().then(res => {
-
this.setState({
-
firstCategoryList : res
-
});
-
}, errMsg => {
-
_mm.errorTips(errMsg);
-
});
-
}
加载二级分类
-
//加载二级分类
-
// 加载二级分类
-
loadSecondCategory(){
-
_product.getCategoryList(this.state.firstCategoryId).then(res => {
-
this.setState({
-
secondCategoryList : res
-
});
-
}, errMsg => {
-
_mm.errorTips(errMsg);
-
});
-
}
4.把最新的firstCategoryId和secondCategoryId的值传入父组件,更新父组件里边一级品类和二级品类
-
// 传给父组件选中的结果
-
onPropsCategoryChange(){
-
// 判断props里的回调函数存在
-
let categoryChangable = typeof this.props.onCategoryChange === 'function';
-
// 如果是有二级品类
-
if(this.state.secondCategoryId){
-
categoryChangable && this.props.onCategoryChange(this.state.secondCategoryId, this.state.firstCategoryId);
-
}
-
// 如果只有一级品类
-
else{
-
categoryChangable && this.props.onCategoryChange(this.state.firstCategoryId, 0);
-
}
-
}
父组件使用CategorySelector组件
-
<div className="form-group">
-
<label className="col-md-2 control-label">所属分类</label>
-
<CategorySelector
-
categoryId={this.state.categoryId}
-
parentCategoryId={this.state.parentCategoryId}
-
onCategoryChange={ (categoryId,parentCategoryId) => this.onCategoryChange(categoryId,parentCategoryId)} />
更新父组件state里边一级品类和二级品类的值
-
//品类改变事件
-
onCategoryChange(categoryId,parentCategoryId){
-
this.setState({
-
categoryId : categoryId,
-
parentCategoryId : parentCategoryId
-
});
-
}
No bean named 'springSecurityFilterChain' is defined
VS 2010中对WPF4有哪些多点触摸支持?
文件管理File类
VS 2010 Beta2中WPF有哪些改进?
WPF的实质
C#中AppDomain.CurrentDomain.BaseDirectory与Application.StartupPath的区别
VS 2010 Beta2中WPF与Silverlight的关键区别?
C# 图片与byte[]之间以及byte[]与string之间的转换
日期格式化{0:yyyyMMdd HH:mm:ss.fff}和{0:yyyyMMdd hh:mm:ss.fff}的区别