react省市二级联动
来源:互联网 发布:淘宝网做包邮亏 编辑:程序博客网 时间:2024/05/14 02:55
class Province extends Component { constructor(props) { super(props) } handleChange(e) { this .props .parentAction(e.target.value) } render() { return ( <select onChange={this .handleChange .bind(this)}> {this .props .value .map((item, key) => ( <option value={item.zhou}>{item.zhou}</option> ))} </select> ) }}//城市选择框class City extends Component { render() { return ( <select> {this .props .value .map(item => <option value={item}>{item}</option>)} </select> ) }}class Picker extends Component { constructor(props) { super(props) this.state = { province: [ { id: 1, zhou: "江苏", country: ["南京", "苏州", "无锡"] }, { id: 2, zhou: "安徽", country: ["合肥", "芜湖"] } ], isProvince: "江苏", isCity: '' } } handleParentAction(value) { this.setState({isProvince: value}) } render() { return ( <div> <Province value={this.state.province} parentAction={this .handleParentAction .bind(this)}/> <City value={this .state .province .find((item) => { return (item.zhou == this.state.isProvince) }) .country}/> </div> ) }}
基于react的省份和市的二级联动,处理数据替换父组件中的province即可
注意点是: isPrivince这个state一定要设置成默认选中的那个省
改进版本
class ProvinceCity extends Component { constructor(props) { super(props) this.state = { priciList: [ { id: 0, province: '请选择省份', cities: ['请选择城市'] }, { id: 1, province: '安徽', cities: ['芜湖', '合肥'] } ], provinceSelected: '请选择省份', citySelected: '' } } handleProvinceChange(e) { this.setState({provinceSelected: e.target.value}) } handleCityChange(e) { this.setState({citySelected: e.target.value}) } handleProvinceBlur(e) { this .props .parentAction({province: e.target.value}) } handleCityBlur(e) { this .props .parentAction({city: e.target.value}) } render() { return ( <div> <select onChange={this .handleProvinceChange .bind(this)} onBlur={this .handleProvinceBlur .bind(this)}> {this .state .priciList .map((item, key) => ( <option value={item.province} key={key}>{item.province}</option> ))} </select> <select onChange={this .handleCityChange .bind(this)} onBlur={this .handleCityBlur .bind(this)}> {this .state .priciList .find((item, key) => (item.province == this.state.provinceSelected)) .cities .map((item, key) => ( <option value={item} key={key}>{item}</option> ))} </select> </div> ) }}class Parent extends Component { constructor(props) { super(props) this.state = { province: '', city: '' } } handleParentAction(value) { value.city ? this.setState({city: value.city}) : this.setState({province: value.province}) } handleClick() { console.log(this.state.province, this.state.city) } render() { return ( <div> <ProvinceCity parentAction={this .handleParentAction .bind(this)}/> <input value="点击" type="button" onClick={this .handleClick .bind(this)}/> </div> ) }}
阅读全文
0 0
- react省市二级联动
- 省市联动二级菜单
- 二级联动 省市
- 省市联动二级菜单
- 省市二级联动代码
- 省市二级联动
- 省市二级联动 脚本
- js省市二级联动
- js 省市 二级 联动
- js 省市 二级联动
- 省市二级联动实例
- JavaScript省市二级联动
- 省市二级联动
- 省市二级联动
- 省市二级联动
- jquery 省市二级联动
- js省市二级联动
- 省市二级联动
- Node.js 运行.js文件出现错误找不到文件的解决办法
- android Utils大集合
- Android各控件绚丽Dome下载
- hibernate使用
- 06-IPTABLES.FIREWALLD防火墙
- react省市二级联动
- 时间选择器
- Android JNI反射调用Java构造方法、成员方法和静态方法
- 关于kettle初始化的那点事2
- Java Web项目开发流程
- MySQL优化
- python 科学计算环境安装(xgboost)
- UVM-regmodel
- java读取properties配置文件