1. 程式人生 > >Vue2中的省市區三級聯動(仿淘寶)

Vue2中的省市區三級聯動(仿淘寶)

三級聯動,隨著越來越多的審美,出現了很多種,好多公司都仿著淘寶的三級聯動 ,好看時尚,so我們公司也一樣……為了貼程式碼方便,我把寫在data裡面省市區的json獨立了出來,下載貼進去即可用,連結如下:http://download.csdn.net/detail/zhaohaixin0418/9862255。

首先頁面顯示如下:

然後我們縣級所在地區會出現三級聯動,如下:(以下是片段,背景色未擷取)


這個張什麼樣,以什麼形式出現,取決於貴公司的UI需求,我們公司是做成彈出層了。。然後背景色透明,這裡為了節省流量,我只截取了一段,最後顯示如下:

如果貴公司也跟我們需求一樣,希望這個可以幫到你們。下面是在vue2專案中寫的三級聯動程式碼以及css樣式:

<template>
    <section class="myAddress">        <section>
           <section class="cont" @click="choseAdd()">
              <section>
                 <span>所在地區:{{Province?Province:''}} {{City?City:''}} {{District?District:''}}</span>              </
section> <img src="../../assets/main/right.png" alt=""> <div style="clear: both"></div> </section> </section> <!-- 居住地址三級聯動選項 --> <section class="showChose" v-show="showChose"> <section class="address">
<section class="title"> <h4>居住地址</h4> <span @click="closeAdd()">×</span> </section> <section class="title"> <div class="area" @click="provinceSelected()"> {{Province?Province:info[province-1].name}} </div> <div class="area" @click="citySelected()" :class="City?'':'active'"> {{City?City:'請選擇'}} </div> <div class="area" @click="districtSelected()" :class="District?'':'active'" v-show="City"> {{District?District:'請選擇'}} </div> </section> <ul> <li class="addList" v-for="(v,k) in info" @click="getProvinceId(v.id, v.name, k)" v-show="showProvince" :class="v.selected ? 'active' : ''">{{v.name}}</li> <li class="addList" v-for="(v,k) in showCityList" @click="getCityId(v.id, v.name, k)" v-show="showCity" :class="v.selected ? 'active' : ''">{{v.name}}</li> <li class="addList" v-for="(v,k) in showDistrictList" @click="getDistrictId(v.id, v.name, k)" v-show="showDistrict" :class="v.selected ? 'active' : ''">{{v.name}}</li> </ul> </section> </section> <!-- 頁面內容 --> <section class="cont"> <span>詳細地址:</span> <input type="text" v-model="address" placeholder=" 請填寫詳細地址"> </section> </section> </template> <script>import { mapActions, mapGetters } from 'vuex'; import api from './../../fetch/api.js' export default { name: 'address', data(){},此處的data直接下載json複製進去即可。http://download.csdn.net/detail/zhaohaixin0418/9862255。 components: { MineHeader }, computed: { ...mapGetters([ 'BCcontextPathSrc', 'sessionId', 'token', ]), }, methods: { choseAdd: function() { this.showChose = true; }, closeAdd: function() { this.showChose = false; }, _filter(add, name, code) { let result = []; for (let i = 0; i < add.length; i++) { if (code == add[i].id) { result = add[i][name]; } } return result; }, getProvinceId: function(code, input, index) { this.province = code; this.Province = input; this.showProvince = false; this.showCity = true; this.showDistrict = false; this.showCityList = this._filter(this.info, 'city', this.province); // 點選選擇當前 this.info.map(a => a.selected = false); this.info[index].selected = true; this.areaProvince = input; }, provinceSelected: function() { // 清除市級和區級列表 this.showCityList = false; this.showDistrictList = false; // 清除市級和區級選項 this.City = false; this.District = false; // 選項頁面的切換 this.showProvince = true; this.showCity = false; this.showDistrict = false; }, getCityId: function(code, input, index) { this.city = code; this.City = input; this.showProvince = false; this.showCity = false; this.showDistrict = true; this.showDistrictList = this._filter(this.showCityList, 'district', this.city); // 選擇當前新增active this.showCityList.map(a => a.selected = false); this.showCityList[index].selected = true; this.areaCity = input; }, citySelected: function() { this.showProvince = false; this.showCity = true; this.showDistrict = false; }, getDistrictId: function(code, input, index) { this.district = code; this.District = input; // 選擇當前新增active this.showDistrictList.map(a => a.selected = false); this.showDistrictList[index].selected = true; // 選取市區選項之後關閉彈層 this.showChose = false; this.areaDistrict = input; }, districtSelected: function() { this.showProvince = false; this.showCity = false; this.showDistrict = true; }, saveProfile: function() { api.commonApi('後臺介面', 這裡是貴公司後臺介面,按照你們公司的改了就好'param_key={"head":{"TYPE":"ADD_UPD_INFO",' + '"SESSION_ID":"' + this.sessionId + '",' + '"TOKEN":"' + this.token + '","DEVICE_ID":""},' + '"param":{"PROVINCE":"' + this.areaProvince + '", ' + '"CITY":"' + this.areaCity + '", "COUNTY":"' + this.areaDistrict + '",' + '"ADDRESS": "' + this.address + '"}}') .then(res => { console.log(res.data); }); } } } </script> <style scoped> .myAddress { width: 100%; background-color: white; border-top: 4px solid rgba(245, 245, 245, 1); color: #333; } .myAddress .cont { border-bottom: 1px solid rgba(245, 245, 245, 0.8); } .myAddress .cont span { display: inline-block; font-size: 0.28rem; color: #333; line-height: 0.88rem; margin-left: 0.32rem; } .myAddress .cont section { float: left; } .myAddress .cont img{ float: right; width: 0.14rem; height: 0.24rem; margin: 0.32rem 0.32rem 0.32rem 0; } .showChose { width: 100%; height: 100%; position: fixed; top: 0; left: 0; z-index: 120; background: rgba(77, 82, 113, 0.8); } .address { position: absolute; bottom: 0; left: 0; z-index: 121; background: #fff; width: 100%; } .title h4 { display: inline-block; margin-left: 3.2rem; font-size: 0.32rem; line-height: 0.88rem; font-weight: normal; color: #999; } .title span { margin: 0.42rem 0 0 2.2rem; font-size: 0.45rem; line-height: 0.34rem; color: #D8D8D8; } .area { display: inline-block; font-size: 0.24rem; line-height: 0.88rem; margin-left: 0.42rem; color: #333; } .addList { padding-left: 0.32rem; font-size: 0.34rem; line-height: 0.88rem; color: #333; } /* 修改的格式 */.address ul { height: 100%; margin-left: 5%; max-height: 4.4rem; overflow: auto; } .address .title .active { color: #0071B8; border-bottom: 0.02rem solid #0071B8; } .address ul .active { color: #0071B8; } </style>
這樣就完成了一個省市區的三級聯動……悲催啊……整個專案裡到處都是坑爹的UI坑爹的需求……哭

相關推薦

Vue2省市區三級聯動仿

三級聯動,隨著越來越多的審美,出現了很多種,好多公司都仿著淘寶的三級聯動 ,好看時尚,so我們公司也一樣……為了貼程式碼方便,我把寫在data裡面省市區的json獨立了出來,下載貼進去即可用,連結如下:http://download.csdn.net/detail/zhao

JQuery+Ajax實現省市區三級聯動附效果圖+資料庫

一、效果圖 1、頁面載入後(初始化:北京市-北京市-東城區) 2、單擊選擇省份後(改變成:河北省-石家莊市-長安區) 二、jsp頁面 <!DOCTYPE html> <html> <head> <meta http-equiv

使用PickerView實現省市區三級聯動小白篇

1.概述    之前自己做專案的時候需要用到省市區三級聯動的效果,本來想自己做一個,可是後來看到了github上的開源專案pickerView,個人感覺非常好用,然後去百度的時候發現好多人的部落格寫的太過於簡單,好多人是直接複製開發者的使用介紹,導致很多新手朋友沒辦法使用,

省市區-三級聯動json資料模擬

html. <div> <!--<span>請選擇您所在區/縣</span>--> <select id="province" name="province" value="省">

mvp實現Xrecyclerview的上下拉和購物車功能仿

首先先匯入我們要使用的依賴 implementation 'com.android.support:design:28.0.0' implementation 'com.google.code.gson:gson:2.8.5' implementation 'cn.bingoo

android自定義ProgressBar仿的載入效果

三種方式實現自定義圓形頁面載入中效果的進度條 To get a ProgressBar in the default theme that is to be used on white/light back ground, use one of the inverse st

vue省市區三級聯動仿京東

destroy 傳參 sla solid 列表 git type tom ott 該栗子是我直接從公司的項目單獨拉出來的(懶得重新寫一次了),所以代碼會有些冗余,下面直接看效果: 接著上代碼: html: <template> <div>

javaweb--json--ajax--mysql實現省市區三級聯動三級聯動數據庫

知識點 request test div 對數 能說 面向對象 inf HA 在web中,實現三級聯動很常見,尤其是利用jquery+json。但是從根本上來說jquery並不是最能讓人容易理解的,接下來從最基本的javascript開始,實現由javascript+jso

iview省市區三級聯動無後臺數據情況下

 1、先引入 import city from"../../../../static/city.json"  2、然後在data裡新增  city:city  3、省(1)寫法,打包沒問題 mounted(){

純JS省市區三級聯動行政區劃程式碼更新至2015-9-30

本文程式碼實現的功能是省市區三級聯動下拉列表,純Javascript,網上已有很多這方面的程式碼。但是作為一個新手,這是我的第一篇CSDN部落格,發此文的目的主要是學習交流,希望看到的朋友發現有什麼不對的地方批評指正一下。 整體的實現思路是:1、建立一個"place類"

省市區三級聯動模板

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath

三級聯動前端代碼

nbsp cnblogs http 三級聯動 img bsp 三級 技術分享 級聯 前端代碼: 三級聯動(前端代碼)

Nginx的預設主機及location設定摘自

基於名字的虛擬主機 Nginx首先選定由哪一個虛擬主機來處理請求。讓我們從一個簡單的配置(其中全部3個虛擬主機都在埠*:80上監聽)開始: server { listen 80; server_name example.org www.example.org; ...

圖片自動輪播仿??

先上效果圖(給美宅瘋狂打call~) 是不是你們要找的效果,是的話就繼續往下看hahaha 上重點(敲黑板)!!! js <script> $(function () { var containl

js實現圖片滑動及放大鏡效果仿京東圖片展示

程式碼地址: https://github.com/EsionChang/HTML/tree/master/ThreeInOne 實現效果為: 滑鼠放到小圖上,上部的大圖會滑動到相應的位置,滑動方式為減速。滑鼠移動到上部的大圖上,會顯示大圖的對應放大部位。

Android購物車的實現升級版 仿

前面有篇文章實現了Android 簡單的購物車 這篇實現了購物車中商品按照店鋪分類顯示的 也就是淘寶購物車的顯示方式。 主要程式碼 package com.jock.shopcart; import java.util.ArrayList; impo

實現垂直輪播廣告仿頭條 OC版本

1.先上效果圖 2 .可以實現下面效果   1》.可自由定義展示的內容(修改BannerContentView即可)。  2》使用方式類似ListView/RecyclerView。  3》可為當前顯示的內容新增各種事件,比如點選開啟某個頁面等。   4,實現垂直自動輪播

js實現選擇地址的三級聯動精確到區

頁面上使用的是 <select class="form-control" id="cmbProvince" name="cmbProvince" ></select><select class="form-control" id="cmbCit

nginx健康檢測ngx_http_upstream_check_module

1、首先去這裡下載nginx的模組https://github.com/yaoweibin/nginx_upstream_check_module 下面是nginx打上模組補丁的安裝 $ wget ‘http://nginx.org/download/nginx-1.0.1

Android 多商品訂單評價類似

前幾日一商城類專案,有一需求,需要對一份訂單的裡面幾個商品進行分別評價(圖片,文字內容,星級);以前都是對一份訂單所有商品一起評價,那種簡單的多; 後來,承蒙老大細心指導,終於弄出來個看起來還算湊活的,還是先貼一下效果圖吧。。。 抖動的有點卡,gif圖的問題 圖片的相關操作展示.gif 具體評價實現