Vue編寫(xiě)多地區(qū)選擇組件
看看效果圖:
效果圖
功能點(diǎn):
- 支持不限城市,不限地區(qū)(這個(gè)東西的實(shí)現(xiàn)..真心死磕了挺久) – 左右兩邊數(shù)據(jù)的同步
- 地區(qū)一次性多選,若是選擇了所有地區(qū)會(huì)自動(dòng)轉(zhuǎn)為不限地區(qū)
- 數(shù)據(jù)遷移箭頭的判斷..選中數(shù)據(jù)才會(huì)有對(duì)應(yīng)的按鈕可以點(diǎn)擊
- 已選位置的數(shù)據(jù)同步響應(yīng)調(diào)用的地方,當(dāng)然也可以外部傳入…(新增傳出,編輯依賴傳入再組合)
- 箭頭是iconfont,支持外部傳入,默認(rèn)是我這邊的cdn的啦….
!!!這是一個(gè)獨(dú)立的組件,css預(yù)處理是用的scss;
寫(xiě)的過(guò)程遇到的問(wèn)題:
因?yàn)檫@個(gè)功能會(huì)多次需要切換省份城市這些,所以我一次性拉取所有數(shù)據(jù)存儲(chǔ)到localstorage,不然請(qǐng)求接口次數(shù)太多了
一開(kāi)始的不限城市和不限地區(qū)我想并入JSON(重組JSON),但是發(fā)現(xiàn)雖然能降低處理麻煩,但是數(shù)據(jù)更亂了…非常不好維護(hù),也有其他組件調(diào)用這個(gè)JSON的地方(比如其他組件有個(gè)地址的三級(jí)聯(lián)動(dòng)也是依賴這個(gè)JSON)
*還有一個(gè)就是要考慮兩邊有不限制這東東的時(shí)候,要剔除不能存在的數(shù)據(jù),比如你不限制城市,那所有該省份的城市都要干掉,不限制地區(qū)也是類似
寫(xiě)左右兩邊數(shù)據(jù)的對(duì)比是最惡心的,為什么這么說(shuō)呢?
*左邊三級(jí)聯(lián)動(dòng)的,每個(gè)子項(xiàng)都有自己的id和name, 而選擇的是組合成的(看GIF圖),中間是中劃線隔開(kāi),這對(duì)于推入和推出就帶來(lái)一堆遍歷和比較
*我們這邊的后端大佬說(shuō)不限制的id均為0(城市或者地區(qū)),所以這個(gè)需要自行組合,最后就是動(dòng)態(tài)圖那格式的ID就是后臺(tái)接受的,,多地區(qū)再拼接成字符串….'3-13-2,2-44-3,4-0-0'這種提交到后臺(tái)..
聯(lián)動(dòng)JSON數(shù)據(jù)格式
regionName: 項(xiàng)的名稱
regionId: 項(xiàng)的ID
child: 是否包含有子項(xiàng)
本來(lái)想寫(xiě)個(gè)props映射下regionName,regionId,child; 但是感覺(jué)作用不大,就沒(méi)寫(xiě)了,(一般公司的地區(qū)JSON格式定下來(lái)了之后變動(dòng)的可能性太低)
你能學(xué)到什么?
1: 數(shù)組的比對(duì),數(shù)組的遍歷,數(shù)組的組合及響應(yīng)判斷
2: vue一些內(nèi)置指令的使用
3: 組件功能細(xì)節(jié)的考慮,不限制地區(qū),全部這些按鈕在什么情況下能點(diǎn)擊
4: 清空數(shù)據(jù)之后各個(gè)狀態(tài)的恢復(fù)和重置等等
代碼
manyAreaSelect.vue
<template> <div class="manyAreaSelect"> <div class="item"> <div class="item-title"> <span> 選擇省</span> </div> <div class="item-content"> <ul> <li v-for="(item,index) in chinaArea" :class="item.selected?'active':''" :key="index" @click="getCityList(item)">{{item.regionName}}</li> </ul> </div> <div class="item-footer"></div> </div> <div class="item"> <div class="item-title"> <span>選擇市</span> </div> <div class="item-content"> <ul v-show="cityList.length===0"> <li> <<請(qǐng)選擇省份</li> </ul> <ul v-show="!notLimitButton.notLimitCity &&cityList.length!==0"> <li v-for="(item,index) in cityList" :class="item.selected ? 'active':''" :key="index" @click="getDistricList(item)">{{item.regionName}}</li> </ul> </div> <div class="item-footer"> <button class="button" :class="notLimitButton.notLimitCity?'success':''" @click="cityNotLitmit({regionName:'不限',regionId:'0'})" size="mini" :disabled="!selectItem.province.regionName">不限城市</button> </div> </div> <div class="item"> <div class="item-title"> <span>選擇地區(qū)</span> </div> <div class="item-content"> <ul v-show="districList.length===0"> <li> <<請(qǐng)選擇城市</li> </ul> <ul v-show="!notLimitButton.notLimitCity && !notLimitButton.notLimitDistrict && districList.length!==0"> <li v-for="(item,index) in districList" :class="item.selected?'active':''" :key="index" @click="getAreaCombineID(item)">{{item.regionName}}</li> </ul> </div> <div class="item-footer"> <button class="button" :class="notLimitButton.notLimitDistrict ?'success':''" @click="districNotLitmit({regionName:'不限',regionId:'0'})" :disabled="!selectItem.city.regionName ||!selectItem.province.regionName || notLimitButton.notLimitCity ">不限地區(qū)</button> </div> </div> <div class="trangle"> <div class="trangle-wrap"> <div class="left"> <button class="button" @click="transferToRight" :disabled="direactionStatusToRight"> <i :class="this.iconDirection.right"></i> </button> </div> <div class="right"> <button class="button" @click="transferToLeft" :disabled="direactionStatusToLeft"> <i :class="this.iconDirection.left"></i> </button> </div> </div> </div> <div class=" item "> <div class="item-title "> <span>已選位置</span> </div> <div class="item-content "> <ul class="selectedContent"> <li v-for="(item,index) in selectedList" :class="item.selected?'active':''" :key="index" @click="selectedAreaSingle(item)">{{item.regionName}}</li> </ul> </div> <div class="item-footer"> <button class="button" @click="selectedAllArea()" :disabled="rightDataList.length=== 0" :class="selectedAllButtonStatus?'success':''">{{selectedAllButtonStatus?'反選':'全部'}}</button> </div> </div> </div> </template> <script> import _ from 'lodash'; export default { name: 'manyAreaSelect', data: function () { return { chinaArea: JSON.parse(window.localStorage.getItem('chinaArea')) || [], // 這是地區(qū)聯(lián)動(dòng)的JSON notLimitButton: { notLimitCity: false, // 城市不限 notLimitDistrict: false, // 地區(qū)不限 }, selectedAllButtonStatus: false, // 已選位置列表全部按鈕的狀態(tài) selectItem: { province: {}, city: {}, distric: {} }, cityList: [], // 城市列表 districList: [], // 區(qū)域列表 rightDataList: [], // 選中項(xiàng)目組合成的渲染列表 rightData: [], // 選中需要移除的 leftData: [], // 左邊選中的轉(zhuǎn)發(fā) } }, props: { selectedData: { type: [String, Object, Array] }, iconDirection: { type: Object, default: function () { // 箭頭圖標(biāo) return { left: 'fzicon fz-ad-you', right: 'fzicon fz-ad-right' } } } }, computed: { selectedList () { // 已選中列表 if (this.selectedData && this.selectedData !== '') { this.rightDataList = this.selectedData; return this.rightDataList; } else { return this.rightDataList; } }, direactionStatusToRight () { // 控制可以轉(zhuǎn)移的箭頭狀態(tài) if (this.notLimitButton.notLimitCity || this.notLimitButton.notLimitDistrict) { if (this.notLimitButton.notLimitCity) { this.removeAllSelected(this.cityList); this.removeAllSelected(this.districList); return false; } else { if (this.notLimitButton.notLimitDistrict) { this.removeAllSelected(this.districList); return false; } } return false; } else { if (this.selectItem.distric.regionName) { return false; } return true; } }, direactionStatusToLeft () { // 控制可以轉(zhuǎn)移的箭頭狀態(tài) if (this.rightData.length === 0) { return true } else { return false } } }, methods: { mapSelect (list, value, type) { // 高亮選中 if (type) { return list.map(pitem => { if (pitem.regionId === value.regionId) { if (value.selected && value.selected === true) { this.$delete(pitem, 'selected'); } else { this.$set(pitem, 'selected', true) } } }) } else { return list.map(pitem => { if (pitem.regionId === value.regionId) { if (value.selected && value.selected === true) { this.$delete(pitem, 'selected'); } else { this.$set(pitem, 'selected', true) } } else { this.$delete(pitem, 'selected'); } }) } }, resetToDefault () { this.leftData = []; // 清空需要轉(zhuǎn)移的數(shù)組 this.notLimitButton = { // 重置按鈕狀態(tài) notLimitCity: false, // 城市不限 notLimitDistrict: false, // 地區(qū)不限 }; this.selectItem.city = {}; this.selectItem.distric = {} this.removeAllSelected(this.cityList); // 清除選中狀態(tài) this.removeAllSelected(this.districList); // 清除選中狀態(tài) this.cityList = []; this.districList = []; }, getCityList (item) { this.resetToDefault(); if (item) { this.cityList = item.child; // 獲取城市列表 this.selectItem.province = item; // 保存省份對(duì)象 this.mapSelect(this.chinaArea, item); // 高亮選擇,單選 } }, getDistricList (item) { this.leftData = []; // 清空需要轉(zhuǎn)移的數(shù)組 this.notLimitButton.notLimitDistrict = false; // 重置按鈕狀態(tài) this.removeAllSelected(this.districList); // 清除選中狀態(tài) this.selectItem.distric = {}; this.districList = []; if (item) { this.districList = item.child; // 獲取區(qū)域列表 this.selectItem.city = item; // 保存省份對(duì)象 this.mapSelect(this.cityList, item); // 高亮選擇,單選 } }, getAreaCombineID (item) { // 獲取組合ID if (item) { this.selectItem.distric = item; this.mapSelect(this.districList, item, 'manySelect'); // 區(qū)域高亮選擇,多選 this.leftData.push({ regionName: this.selectItem.province.regionName + '-' + this.selectItem.city.regionName + '-' + item.regionName, regionId: this.selectItem.province.regionId + '-' + this.selectItem.city.regionId + '-' + item.regionId }) this.leftData = _.uniqBy(this.leftData, 'regionId'); if (this.leftData.length === this.districList.length) { this.leftData = []; this.notLimitButton.notLimitDistrict = true; // 轉(zhuǎn)為不限制地區(qū) this.leftData.push({ regionName: this.selectItem.province.regionName + '-' + this.selectItem.city.regionName + '-不限', regionId: this.selectItem.province.regionId + '-' + this.selectItem.city.regionId + '-0' }) } } }, cityNotLitmit (item) { // 城市不限 this.leftData = []; // 請(qǐng)空數(shù)組 this.notLimitButton.notLimitCity = !this.notLimitButton.notLimitCity; // 不限按鈕狀態(tài) this.leftData.push({ regionName: this.selectItem.province.regionName + '-不限-不限', regionId: this.selectItem.province.regionId + '-0-0' }) }, districNotLitmit (item) { // 區(qū)域不限 this.leftData = []; // 請(qǐng)空數(shù)組 this.notLimitButton.notLimitDistrict = !this.notLimitButton.notLimitDistrict; // 不限按鈕狀態(tài) this.leftData.push({ regionName: this.selectItem.province.regionName + '-' + this.selectItem.city.regionName + '-不限', regionId: this.selectItem.province.regionId + '-' + this.selectItem.city.regionId + '-0' }) }, transferToRight () { // 選中推入到已選中列表區(qū)域 if (this.leftData && this.leftData.length !== 0) { if (this.leftData.length === 1) { // 長(zhǎng)度只有1,那就只有不限城市或者地區(qū)了 let limitId = this.leftData[0].regionId.split('-'); // 比對(duì)比對(duì),切割成數(shù)組 this.rightDataList.map(item => { let id = item.regionId.split('-'); if (limitId[0] === id[0]) { if (limitId[1] === '0') { // 不限城市 this.rightDataList = this.rightDataList.filter(ritem => { let rid = ritem.regionId.split('-'); if (limitId[0] !== rid[0]) { return ritem; } }) } else { if (limitId[2] === '0') { // 不限地區(qū) this.rightDataList = this.rightDataList.filter(ritem => { let rid = ritem.regionId.split('-'); if ((limitId[0] === rid[0] && limitId[1] === rid[1])) { if (ritem[2] === '0') { return ritem; } } else { if (limitId[0] !== rid[0] || limitId[1] !== rid[1]) { return ritem; } } }) } else { this.rightDataList = this.rightDataList.filter(ritem => { let rid = ritem.regionId.split('-'); if (limitId[0] === rid[0]) { if (limitId[1] === rid[1]) { if (!(rid[2] === '0')) { return ritem; } } else { if (!(rid[1] === '0')) { return ritem } } } else { return ritem } }) } } } }) } else { let limitId = this.leftData[0].regionId.split('-'); // 比對(duì)比對(duì),切割成數(shù)組 this.rightDataList = this.rightDataList.filter(ritem => { let rid = ritem.regionId.split('-'); if (limitId[0] === rid[0]) { if (limitId[1] === rid[1]) { if (!(rid[2] === '0')) { return ritem; } } else { if (!(rid[1] === '0')) { return ritem } } } else { return ritem } }) } this.leftData.map(item => { this.rightDataList.push(item); }) this.rightDataList = _.uniqBy(this.rightDataList, 'regionId'); this.resetToDefault(); } }, selectedAreaSingle (item) { // 已選擇區(qū)域單個(gè)選擇 if (item) { this.rightData = []; this.mapSelect(this.rightDataList, item, 'manySelect'); // 區(qū)域高亮選擇,多選 this.rightDataList.map(item => { if (item.selected) { this.rightData.push(item) } }) } }, selectedAllArea () { // 已選中區(qū)域全選反選 if (this.selectedAllButtonStatus) { this.removeAllSelected(this.rightDataList); this.rightData = []; } else { this.rightDataList.map(item => this.$set(item, 'selected', true)); this.rightData = this.rightDataList; } this.selectedAllButtonStatus = !this.selectedAllButtonStatus; }, transferToLeft () { // 從已選中列表區(qū)域退回待轉(zhuǎn)發(fā)區(qū)域 if (this.rightData && this.rightData.length !== 0) { this.rightDataList = this.rightDataList.filter(item => { if (!item.selected) { return item; } }) this.rightData = []; } }, removeAllSelected (list) { // 清空選中狀態(tài) list.map(item => this.$delete(item, 'selected')); } }, watch: { 'rightDataList' (newValue, oldValue) { // 選擇列表的值變動(dòng)響應(yīng)外部值的變動(dòng) if (newValue.length !== this.rightData.length) { this.selectedAllButtonStatus = false; } else { if (newValue.length === 0) { this.selectedAllButtonStatus = false; } else { this.selectedAllButtonStatus = true; } } this.$emit('update:selectedData', newValue); } } } </script> <style scoped lang="scss"> ul { padding: 0; margin: 0; max-height: 100%; overflow-y: auto; li { cursor: pointer; text-align: center; padding: 5px; &.active, &:hover { background: #e4e8f1; color: #48576a; } } } .manyAreaSelect { position: relative; z-index: 2005; .item { border: 1px solid #d1dbe5; background: #fff; box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04); display: inline-block; vertical-align: middle; min-width: 180px; box-sizing: border-box; position: relative; height: 100%; min-height: 260px; } .item-title { height: 36px; line-height: 36px; background: #fbfdff; margin: 0; border-bottom: 1px solid #d1dbe5; box-sizing: border-box; color: #1f2d3d; text-align: center; } .trangle { background: transparent; display: inline-block; vertical-align: middle; width: 40px; box-sizing: border-box; height: 100%; position: relative; .trangle-wrap { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .left, .right { margin: 10px 5px; } ; } .item-content { font-size: 13px; height: 190px; padding: 8px 2px; } .item-footer { padding: 5px 0; height: 40px; text-align: center; } } .selectedContent { li { text-align: left; padding-left: 25px; } } .button { display: inline-block; line-height: 1; white-space: nowrap; cursor: pointer; background: #fff; border: 1px solid #c4c4c4; color: #1f2d3d; margin: 0; border-radius: 4px; padding: 4px; font-size: 12px; border-radius: 4px; -webkit-appearance: button; outline: none; &.success { background: #42d885; border-color: #42d885; color: #fff; } &:disabled { color: #bfcbd9; cursor: not-allowed; background-image: none; background-color: #eef1f6; border-color: #d1dbe5; } } </style>
用法
<!--selectedData就是響應(yīng)的數(shù)據(jù).sync是2.3回歸的語(yǔ)法糖--> <!--可以綁定iconDirection傳入箭頭的iconfont,Object--> <many-area-select :selectedData.sync="manyAreaValue"></many-area-select>
總結(jié)
這個(gè)組件的出爐,折騰了很久..
寫(xiě)的過(guò)程推倒了三版(三天三個(gè)版本),都是思路沒(méi)想對(duì)和理清..寫(xiě)著寫(xiě)著就寫(xiě)不下去了…
這個(gè)組件目前的功能是滿足我這邊的需求的,若是有更好的實(shí)現(xiàn)方式可以留言。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue移動(dòng)端項(xiàng)目中如何實(shí)現(xiàn)頁(yè)面緩存的示例代碼
這篇文章主要介紹了vue移動(dòng)端項(xiàng)目中如何實(shí)現(xiàn)頁(yè)面緩存的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Vue實(shí)現(xiàn)實(shí)時(shí)更新sessionStorage數(shù)據(jù)的示例代碼
這篇文章主要為大家詳細(xì)介紹了Vue如何實(shí)現(xiàn)實(shí)時(shí)更新sessionStorage數(shù)據(jù),文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以參考一下2023-06-06unplugin-svg-component優(yōu)雅使用svg圖標(biāo)指南
這篇文章主要為大家介紹了unplugin-svg-component優(yōu)雅使用svg圖標(biāo)指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03element el-table實(shí)現(xiàn)多級(jí)表頭的代碼
多級(jí)表頭的作用與優(yōu)勢(shì),多級(jí)表頭能夠清晰地展示數(shù)據(jù)的層次結(jié)構(gòu),幫助我們更好地理解數(shù)據(jù)之間的關(guān)系,下面通過(guò)本文給大家介紹element el-table實(shí)現(xiàn)多級(jí)表頭的代碼,感興趣的朋友跟隨小編一起看看吧2024-04-04Vue如何動(dòng)態(tài)改變列表搜索出關(guān)鍵詞的字體顏色
這篇文章主要介紹了Vue如何動(dòng)態(tài)改變列表搜索出關(guān)鍵詞的字體顏色問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Vuex中this.$store.commit()和this.$store.dispatch()區(qū)別說(shuō)明
這篇文章主要介紹了Vuex中this.$store.commit()和this.$store.dispatch()區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue實(shí)現(xiàn)滑塊拖拽校驗(yàn)功能的全過(guò)程
vue驗(yàn)證滑塊功能,在生活中很多地方都可以見(jiàn)到,使用起來(lái)非常方便,這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)滑塊拖拽校驗(yàn)功能的相關(guān)資料,需要的朋友可以參考下2021-08-08