微信小程序中,经常可见选择地区或者商品分类或者其他的分类,选择后显示,选择后删除
先介绍一下主要功能:点击 ‘地区’ ,下面选择区域出现,点击 ‘选择地区’ 中的按钮,上面 ‘已选地区’ 显示选择的地区,点击 ‘x’ 已选的地区就取消,点击 [完成] 整个选择区域隐藏,只留下地区。
整体样式用的弹性布局,就不细说了。
wxml:
<view class="container"> ? <text bindtap="show" class="color">地区</text> ? <view class="choose" wx-if="{{a}}"> ? ? <!-- 已选地区 --> ? ? <view class="chosed"> ? ? ? <view class="chosedtop"> ? ? ? ? <text>已选地区</text> ? ? ? ? <text bindtap="finish">完成</text> ? ? ? </view> ? ? ? <view class="view"> ? ? ? ? <view class="position" wx:for='{{area}}' wx:key='index'> ? ? ? ? ? <button class="buttond">{{item}}</button> ? ? ? ? ? <image src='' bindtap="shut" data-index="{{index}}" data-name="{{item}}"></image> ? ? ? ? ? //这是按钮右上角的关闭图片 ? ? ? ? </view> ? ? ? </view> ? ? </view> ? ? <!-- 选择地区 --> ? ? <view class="area"> ? ? ? <text>选择地区</text> ? ? ? <view class="chos"> ? ? ? ? <block wx:for='{{array}}' wx:key='index'> ? ? ? ? ? <button class="button {{tabindex == index?'active':''}}" data-index="{{index}}" data-name="{{item}}" bindtap="tabarea">{{item}}</button> ? ? ? ? </block> ? ? ? </view> ? ? </view> ? </view> </view>
js:
var chosedarea = []; var area = []; Page({? ? data: { ? ? a:false, ? ? tabindex:0, ? ? array: ["北京", '南京', '上海', '天津', '杭州', '云南', "北京", '南京', '上海', '天津', '杭州', '云南',"北京", '南京', '上海', '天津', '杭州', '云南'], ? }, ? // 选地区 ? ?tabarea:function(e){ ? ? this.setData({ ? ? ? tabindex:e.currentTarget.dataset.index ? ? }); ? ? ?var id = e.currentTarget.dataset.index; ? ? ?var name = e.currentTarget.dataset.name; ? ? ?chosedarea.push(name); ? ? ? ?this.setData({ ? ? ? ? ?"area": chosedarea ? ? ? ?}) ? }, ? // 取消已选地区 ? shut:function(e){ ? ? this.setData({ ? ? ? index: e.currentTarget.dataset.index, ? ? ? name : e.currentTarget.dataset.name ? ? }); ? ? var yid = e.currentTarget.dataset.index; ? ? var yname = e.currentTarget.dataset.name; ? ? chosedarea.splice(yid,1) ? ? this.setData({ ? ? ? "area": chosedarea ? ? }) ? }, ? // 点击完成隐藏 ? finish:function(){ ? ? var that = this; ? ? if (that.data.a == true) { ? ? ? that.setData({ ? ? ? ? a: false ?? ? ? ? }) ? ? } ? }, ? // 点击地区显示 ? show:function(){ ? ? var that = this; ? ? if (that.data.a == false) { ? ? ? that.setData({ ? ? ? ? a: true ? ? ? ? ? }) ? ? } ? }, ? })
css
.container{ ? width: 100%; ? height: 300rpx; } .choose{ ? width: 100%; } .buttond{ ? width: 88%; ? padding: 0; ? height: 68rpx; ? line-height: 68rpx; ? font-size: 32rpx; ? margin-bottom: 10rpx; } .area{ ? display: flex; ? flex-direction: column; ? margin: 0 20rpx; } .chos{ ? display: flex; ? flex-wrap: wrap; ? margin-top: 15rpx; } .button{ ? width: 22%; ? padding: 0; ? height: 68rpx; ? line-height: 68rpx; ? font-size: 32rpx; ? margin: 0 10rpx; ? margin-bottom: 10rpx; } .view{ ? display: flex; ? flex-wrap: wrap; ? height: auto; ? margin: 0 20rpx; } .position{ ? width: 24%; } .chosedtop{ ? display: flex; ? justify-content: space-between; ? margin: 0 30rpx 15rpx }
如果是选完 上面添加了对应的 下面可选择的与之对应的要隐藏掉 就这样:
js中
// 选地区 ? tabarea: function (e) { ? ? let that = this; ? ? that.setData({ ? ? ? tabindex: e.currentTarget.dataset.index ? ? }); ? ? var id = e.currentTarget.dataset.index; ? ? var name = e.currentTarget.dataset.name; ? ? chosedarea.push(name); ? ? that.data.array.splice(id, 1); ? ? that.setData({ ? ? ? "area": chosedarea, ? ? ? "array": that.data.array ? ? }) ? },
在上面点击删除的话 下面可选择的区域要对应的添加上:
wxml:
<view class="container"> ? <text bindtap="show" class="color">地区</text> ? <view class="choose" wx-if="{{a}}"> ? ? <!-- 已选地区 --> ? ? <view class="chosed"> ? ? ? <view class="chosedtop"> ? ? ? ? <text>已选地区</text> ? ? ? ? <text bindtap="finish">完成</text> ? ? ? </view> ? ? ? <view class="view"> ? ? ? ? <view class="position" wx:for='{{area}}' wx:key='index'> ? ? ? ? ? <button class="buttond" data-index="{{index}}" data-name="{{item}}" bindtap="addName">{{item}}</button> ? ? ? ? ? <!-- <image src='' bindtap="shut" data-index="{{index}}" data-name="{{item}}"></image> --> ? ? ? ? ? <!-- //这是按钮右上角的关闭图片 --> ? ? ? ? </view> ? ? ? </view> ? ? </view> ? ? <!-- 选择地区 --> ? ? <view class="area"> ? ? ? <text>选择地区</text> ? ? ? <view class="chos"> ? ? ? ? <block wx:for='{{array}}' wx:key='index'> ? ? ? ? ? <button class="button {{tabindex == index?'active':''}}" data-index="{{index}}" data-name="{{item}}" bindtap="tabarea">{{item}}</button> ? ? ? ? </block> ? ? ? </view> ? ? </view> ? </view> </view>
js
var chosedarea = []; var area = []; Page({ ? data: { ? ? a: false, ? ? tabindex: 0, ? ? array: ["北京", '南京', '上海', '天津', '杭州', '云南', "新疆", '黑龙江', '东北', '威海', '宁夏', '广西', "西安", '山东', '青岛', '济南', '烟台', '蓬莱'], ? }, ? // 选地区 ? tabarea: function (e) { ? ? let that = this; ? ? that.setData({ ? ? ? tabindex: e.currentTarget.dataset.index ? ? }); ? ? var id = e.currentTarget.dataset.index; ? ? var name = e.currentTarget.dataset.name; ? ? chosedarea.push(name); ? ? that.data.array.splice(id, 1); ? ? that.setData({ ? ? ? "area": chosedarea, ? ? ? "array": that.data.array ? ? }) ? }, ? // 添加回 ? addName:function(e){ ? ? let that = this; ? ? console.log(e) ? ? that.setData({ ? ? ? index: e.currentTarget.dataset.index, ? ? }) ? ? var aname = e.currentTarget.dataset.name; ? ? chosedarea.splice(that.data.index,1); ? ? that.data.array.push(aname); ? ? that.setData({ ? ? ? "area": chosedarea, ? ? ? "array": that.data.array ? ? }) ? }, ? // 取消已选地区 ? shut: function (e) { ? ? this.setData({ ? ? ? index: e.currentTarget.dataset.index, ? ? ? name: e.currentTarget.dataset.name ? ? }); ? ? var yid = e.currentTarget.dataset.index; ? ? var yname = e.currentTarget.dataset.name; ? ? chosedarea.splice(yid, 1) ? ? this.setData({ ? ? ? "area": chosedarea ? ? }) ? }, ? // 点击完成隐藏 ? finish: function () { ? ? var that = this; ? ? if (that.data.a == true) { ? ? ? that.setData({ ? ? ? ? a: false ? ? ? }) ? ? } ? }, ? // 点击地区显示 ? show: function () { ? ? var that = this; ? ? if (that.data.a == false) { ? ? ? that.setData({ ? ? ? ? a: true ? ? ? }) ? ? } ? }, })
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
查看更多关于微信小程序实现选择内容显示对应内容的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did123847