react使用antd级联实现省市区选择
import { Component } from 'react';
import { Cascader } from 'antd';
import * as addressRequest from '../../services/address';
export default class AreaPicker extends Component {
constructor(props) {
super(props);
this.state = {
options: [],
defaultValue: this.props.value
};
}componentDidMount() {
if (this.state.defaultValue.length>0) {
/**有默认值时获取所有列表*/
this.getAllOpt();
} else {
this.getProvinceOpt();
}
}
render() {
return (
);
}onChange = (value, selectedOptions) => {
/**给父组件通知值变化*/
this.props.onChange(value);
};
loadData = https://www.it610.com/article/selectedOptions => {
let targetOption = selectedOptions[selectedOptions.length - 1];
targetOption.loading = true;
/**获取市列表 */
if (selectedOptions.length == 1) {
addressRequest.getCityList(targetOption.value).then(res => {
targetOption.loading = false;
let list = [];
res.forEach(el =>
list.push({
label: el.value,
value: el.code,
isLeaf: false
})
);
targetOption.children = list;
this.setState({
options: [...this.state.options]
});
});
} else {
/**获取区列表 */
addressRequest.getRegionList(targetOption.value).then(res => {
targetOption.loading = false;
let list = [];
res.forEach(el =>
list.push({
label: el.value,
value: el.code
})
);
targetOption.children = list;
this.setState({
options: [...this.state.options]
});
});
}
};
/**没有默认值时初始化省列表 */
getProvinceOpt() {
addressRequest.getProvinceList('0').then(res => {
let list = [];
res.forEach(el => {
list.push({
label: el.value,
value: el.code,
isLeaf: false
});
});
this.setState({
options: list
});
});
} /**有默认选中的值时对数据进行初始化*/
getAllOpt() {
let list1 = [],
list2 = [],
list3 = [];
/**区获取 */
addressRequest.getRegionList(this.state.defaultValue[1]).then(res => {
res.forEach(el =>
list3.push({
label: el.value,
value: el.code
})
);
/**市获取 */
addressRequest.getCityList(this.state.defaultValue[0]).then(res => {
res.forEach(el =>
list2.push({
label: el.value,
value: el.code,
isLeaf: false,
children: this.state.defaultValue[1] == el.code ? list3 : null
})
);
/**省获取 */
addressRequest.getProvinceList('0').then(res => {
res.forEach(el => {
list1.push({
label: el.value,
value: el.code,
isLeaf: false,
children: this.state.defaultValue[0] == el.code ? list2 : null
});
});
this.setState({
options: list1
});
});
});
});
}
}
【react使用antd级联实现省市区选择】
推荐阅读
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- 基于|基于 antd 风格的 element-table + pagination 的二次封装
- iOS中的Block
- Linux下面如何查看tomcat已经使用多少线程
- 使用composer自动加载类文件
- android|android studio中ndk的使用