|
作者:西江無月,來自原文地址
最近呢剛好做了一個省市聯(lián)動的功能,今天看到有人問這個怎么做,我就把我做的放上來共享一下:
首先呢,來看看效果,點(diǎn)擊文字‘點(diǎn)擊’,彈出選擇窗口,點(diǎn)擊取消或者確定(取消、確定按鈕在選擇框上邊,截圖有些不清楚),窗口下滑,

做這個我用的是picker-view這個組件,現(xiàn)在來看一看picker-view的屬性:

現(xiàn)在開始寫wxml的代碼,對了,插一句,我這里是把它寫成一個模板的,先看看目錄結(jié)構(gòu)

我們先來看看cascade.wxml里的代碼:
-
<template name="cascade">
-
<view class="cascade_box" animation="{{animationData}}">
-
<view class="cascade_hei"></view>
-
<view class="cascade_find">
-
<view class="cascade_header">
-
<text class='quxiao' catchtap="quxiao">取消</text>
-
<text class="queren" catchtap="queren">確認(rèn)</text>
-
</view>
-
<picker-view indicator-style="height: 80rpx;" style="width: 100%; height: 400rpx;" bindchange="bindChange">
-
<picker-view-column>
-
<view wx:for="{{sheng}}" wx:key='this' style="line-height: 80rpx;text-align:center;">{{item}}</view>
-
</picker-view-column>
-
<picker-view-column>
-
<view wx:for="{{shi}}" wx:key='this' style="line-height: 80rpx;text-align:center;">{{item}}</view>
-
</picker-view-column>
-
<picker-view-column>
-
<view wx:for="{{qu}}" wx:key='this' style="line-height: 80rpx;text-align:center;">{{item}}</view>
-
</picker-view-column>
-
</picker-view>
-
</view>
-
</view>
-
</template>
接下來是cascade.wxss的代碼:
-
.cascade_box{
-
font-size:28rpx;
-
width: 100%;
-
height: 0;
-
position: fixed;
-
bottom: 0;
-
left: 0;
-
}
-
.cascade_hei{
-
width: 100%;
-
height:732rpx;
-
background: #000;
-
opacity: 0.5;
-
}
-
.cascade_find{
-
width: 100%;
-
height: 500rpx;
-
position: relative;
-
/*bottom: 0;
-
left: 0;*/
-
background: #fff;
-
}
-
-
.quxiao,.queren{
-
display: block;
-
position: absolute;
-
width: 100rpx;
-
height: 80rpx;
-
line-height: 80rpx;
-
/*background: #00f;*/
-
text-align: center;
-
color: #0f0;
-
}
-
.queren{
-
right: 0;
-
top: 0;
-
}
-
.cascade_header{
-
height: 80rpx;
-
width: 100%;
-
margin-bottom: 20rpx;
-
}
好了這里的模板寫好了,接下來是引用,引用就很簡單了:
首先是las.wxml的引用:
-
<import src="../../teml/cascade.wxml"/>
-
<view bindtap="dianji">點(diǎn)擊</view>
-
<template is="cascade" data="{{animationData:animationData,sheng:sheng,shi:shi,qu:qu}}"/>
然后是las.wxss的引用(你沒看錯就是一句):
-
@import '../../teml/cascade.wxss';
接下來就是JS了:
在這里要先說一下保存省市的名稱的json文件格式,這個json文件里又全國的省市名稱(這里只寫了北京市為例子,這是我請求的數(shù)據(jù)的例子,你要根據(jù)你自己請求的數(shù)據(jù)來做一些JS的判斷):
-
{
-
"regions": [{
-
"id": 110000,
-
"name": "北京",
-
"regions": [{
-
"id": 110100,
-
"name": "北京市",
-
"regions": [{
-
"id": 110101,
-
"name": "東城區(qū)"
-
}, {
-
"id": 110102,
-
"name": "西城區(qū)"
-
}, {
-
"id": 110103,
-
"name": "崇文區(qū)"
-
}, {
-
"id": 110104,
-
"name": "宣武區(qū)"
-
}, {
-
"id": 110105,
-
"name": "朝陽區(qū)"
|