码迷,mamicode.com
首页 > 其他好文 > 详细

es6 简单封装一个 省市县三级下拉框

时间:2019-01-10 15:32:13      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:技术   color   技术分享   set   配置   console   sync   三级   let   

   废话不多说,直接上效果图和代码:

        技术分享图片

 

  1,index.js

    

function $(el){
    return document.getElementById(el)
}

let render = Symbol(render)
let event = Symbol(event)

class Select {
    constructor (el) {
        console.log(el)
        if (!el) {
            throw 请填写el配置
        } else {
            this[render](el)
            this[event]()
        }
    }

    [render](el) {
        let tpl = `<div class="select-box disIn font14">
        <div class="province-box disIn pr">
            <input type="text" name="province" class="province-input" id="province-input" placeholder="请选择省" readonly>
            <ul class="province-list" id="province-list"></ul>
        </div>
        <div class="city-box disIn pr">
            <input type="text" name="city" class="city-input" id="city-input" placeholder="请选择市" readonly>
            <ul class="city-list" id="city-list"></ul>
        </div>
        <div class="area-box disIn pr">
            <input type="text" name="area" class="area-input" id="area-input" placeholder="请选择县" readonly>
            <ul class="area-list" id="area-list"></ul>
        </div>
    </div>`;
        el.innerHTML = tpl;
    }

    async [event]() {
        let datas = await fetchJson(/region)          // 这里用了mock 模拟了一个get请求,获取了一个全国省市县的json数据。
        let $provinceInput = $(province-input)
        let $provinceList = $(province-list)
        let $cityInput = $(city-input)
        let $cityList = $(city-list)
        let $areaInput = $(area-input)
        let $areaList = $(area-list)
        let provinceData = ‘‘;
        let cityData = ‘‘;
        let areaData = ‘‘;
        let choicedData = null;
        $provinceInput.onclick = function () {
            provinceData = ‘‘;
            for (let province of datas.data) {
                provinceData += `<li data-code="${province.code}">${province.name}</li>`
            }
            $provinceList.innerHTML = provinceData
            $provinceList.style.display = block
            $provinceList.onclick = function (e) {
                $provinceInput.value = e.target.innerHTML
                $provinceInput.setAttribute(data-code, e.target.getAttribute(data-code))
                $cityInput.value = $areaInput.value = ‘‘;
                this.style.display = none;
            }
        }
        $cityInput.onclick = function () {
            cityData = ‘‘;
            let provinceCode = $provinceInput.getAttribute(data-code)
            if (!provinceCode) {
                $provinceInput.click()
                return
            }
            choicedData = datas.data.filter(item => {
                return item.code === provinceCode
            })
            for (let item of choicedData[0].cityList) {
                cityData += `<li data-code="${item.code}">${item.name}</li>`
            }
            $cityList.innerHTML = cityData
            $cityList.style.display = block;
            $cityList.onclick = function (e) {
                $cityInput.value = e.target.innerHTML
                $cityInput.setAttribute(data-code, e.target.getAttribute(data-code))
                this.style.display = none
            }
        }

        $areaInput.onclick = function () {
            areaData = ‘‘;
            let cityCode = $cityInput.getAttribute(data-code);
            if (!cityCode) {
                $cityInput.click()
                return
            }
            let cityChoiced = choicedData[0].cityList.filter(item => {
                return item.code === cityCode
            })
            for (let item of cityChoiced[0].areaList) {
                areaData += `<li data-code="${item.code}">${item.name}</li>`
            }
            $areaList.innerHTML = areaData;
            $areaList.style.display = block
            $areaList.onclick = function (e) {
                $areaInput.value = e.target.innerHTML
                $areaInput.setAttribute(data-code, e.target.getAttribute(data-code))
                this.style.display = none
            }
        }
    }
}

 

es6 简单封装一个 省市县三级下拉框

标签:技术   color   技术分享   set   配置   console   sync   三级   let   

原文地址:https://www.cnblogs.com/wjyz/p/10249877.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!