react写城市(省市区)联动的方法

一个新手
Lepaskan: 2017-10-01 07:44:41
asal
2744 orang telah melayarinya

在components中引入联动的json文件

import ProvinceData from '../../../../../json/area.json';
Salin selepas log masuk

在this.state中定义相应变量

    constructor(props){        
            super(props);        
            this.state = {
            active: 'male',
            mapconfig:{
                center: { lat: 42.872, lng: 3.644},
                zoom: 3
            },
            curOption: {
                province:'',
                city:'',
                county:'',
                job:'',
                edu:''
            },            //联动省级市数据
            deepProvince:null,
            deepCity:null
        };        
        this.changeTab = this.changeTab.bind(this);        
        this.chgActive = this.chgActive.bind(this);        
        this.chgOption = this.chgOption.bind(this);
Salin selepas log masuk

写联动的方法

 //drop 改变active
    chgActive(key) {        
    this.setState({
            curActive: key
        });
    }    //drop 改变option
    chgOption(key, value) {        
    var obj = {};
        obj[key] = value;
        obj = Object.assign({}, this.state.curOption, obj);        
        this.setState({
            curOption: obj
        });        if(key === 'province'){            
        this.setState({deepProvince:value})
        }else if(key === 'city'){            
        this.setState({deepCity:value})
        }
    }
Salin selepas log masuk

在render中声明变量以及调用和判断

        let {deepProvince,deepCity} = this.state;
        let provinceDropData =[],cityDropData=[],countyDropData =[];
            ProvinceData.provinceData.map(
            function(i){
                if(i.deep == 1){
                    provinceDropData.push({
                        nm:i.value,
                        value:i.id
                    })
                }
            }
        );
          if(deepProvince){
            ProvinceData.cities.map(function(i) {
                if (i.parentId === deepProvince) {
                    cityDropData.push({
                        nm: i.value,
                        value: i.id
                    })
                }
            });
        }
        if(deepCity){
            ProvinceData.counties.map(function(i) {
                if (i.parentId === deepCity) {
                    countyDropData.push({
                        nm: i.value,
                        value: i.id
                    })
                }
            });
        }
Salin selepas log masuk

写相应的页面显示

<span className="drop_city">
                        <DropList                            
                        className="country"
                            propKey="province"
                            placeholder={&#39;省&#39;}
                            curActive={this.state.curActive}
                            curoption={this.state.curOption[&#39;province&#39;]}
                            chgOption={this.chgOption}
                            chgActive={this.chgActive}
                            nodefault={true}
                            dropData={provinceDropData}>
                        </DropList></span>
                         <span className="drop_city">
                        <DropList                            
                        propKey="city"
                            placeholder={&#39;市&#39;}
                            curActive={this.state.curActive}
                            curoption={this.state.curOption[&#39;city&#39;]}
                            chgOption={this.chgOption}
                            chgActive={this.chgActive}
                            nodefault={true}
                            dropData={cityDropData}>
                        </DropList></span>
                         <span className="drop_city">
                        <DropList                            
                        propKey="county"
                            placeholder={&#39;区&#39;}
                            curActive={this.state.curActive}
                            curoption={this.state.curOption[&#39;county&#39;]}
                            chgOption={this.chgOption}
                            chgActive={this.chgActive}
                            nodefault={true}
                            dropData={countyDropData}>
                        </DropList>
                             </span>
Salin selepas log masuk

Atas ialah kandungan terperinci react写城市(省市区)联动的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!