微信小程序 筛选侧边栏 全选与反全选

小程序中经常会有筛选侧边栏的按钮,今天用原生小程序实现这一功能。

1.wxml核心代码

<button class="filterBtn" bindtap="translate">
	<text>筛选</text>
	<text class="iconfont icon-shaixuan"></text>
</button>
<view class="float {{isRuleTrue?'isRuleShow':'isRuleHide'}}">
	<view class="closebtn" bindtap="success"></view>
	<view class="animation-element" style="padding-top: {{navHeight}}px">
		<form class="filterForm" style="height: calc(100% - {{filterBtnHeight}}px);">
			<view class="filterbox">
				<view class="filter-label">
					<text class="title">模特类型</text>
				</view>					
				<view class="filtergroup">
					<checkbox-group bindchange="modelChange">													
						<label wx:for="{{listModel}}" wx:key="*this" class="txt-ellipsis {{item.checked?'active':''}}">
							<checkbox value="{{item.code}}" checked="{{item.checked}}" />{{item.value}}
						</label>
					</checkbox-group>
				</view>
			</view>
			<view class="filterbox">
				<view class="filter-label">
					<text class="title">年龄</text>
				</view>
				<view class="filtergroup">
					<input type="text" placeholder="最低年龄" placeholder-class="input-placeholder"></input>
					<view class="line"></view>
					<input type="text" placeholder="最高年龄" placeholder-class="input-placeholder"></input>
				</view>
			</view>
			<view class="filterbox">
				<view class="filter-label">
					<text class="title">所在城市</text>
					<label class="selectAll" bindtap="selectall">
					    <checkbox value="all" checked="{{select_all}}" />{{select_all_font}}
					</label>
				</view>
				<view class="filtergroup">
					<checkbox-group bindchange="checkChange">
						<label wx:for="{{listCity}}" wx:key="*this" class="txt-ellipsis {{item.checked?'active':''}}">
							<checkbox value="{{item.code}}" checked="{{item.checked}}" />{{item.value}}
						</label>
					</checkbox-group>
				</view>
			</view>
		</form>
		<view class='filter-btn'>
			<button formType="reset" class="filterbtn-reset">重置</button>
			<button formType="submit" class="filterbtn-submit" bindtap="success">确定</button>
		</view>
	</view>
</view>

2.wcss核心代码

.filter .filter-main .float {
  position: fixed;
  top: 0;
  right: 0;
  height: 100%;
  width: 100%;
  z-index: 20;
  background-color: rgba(0, 0, 0, 0.5);
  visibility: hidden;
  -webkit-transition: all 0.3s;
  -o-transition: all 0.3s;
  -moz-transition: all 0.3s;
  transition: all 0.3s;
}
.filter .filter-main .isRuleShow {
  visibility: visible;
}
.filter .filter-main .isRuleHide {
  visibility: hidden;
}
.filter .filter-main .closebtn {
  position: absolute;
  top: 0;
  left: 0;
  width: 22%;
  height: 100%;
}
.filter .filter-main .animation-element {
  overflow: hidden;
  position: absolute;
  top: 0;
  right: -100%;
  width: 78%;
  height: 100%;
  z-index: 30;
  padding-top: 38rpx;
  background-color: #ffffff;
  -webkit-transition: all 0.5s;
  -o-transition: all 0.5s;
  -moz-transition: all 0.5s;
  transition: all 0.5s;
}
.filter .filter-main .animation-element .filterForm {
  position: relative;
  display: block;
  width: 100%;
  overflow-y: auto;
}
.filter .filter-main .isRuleShow .animation-element {
  right: 0;
}
.filter .filter-main .filterbox {
  padding: 20rpx 30rpx 30rpx;
  border-bottom: 1px solid #f1f1f1;
  margin-bottom: 10rpx;
}
.filter .filter-main .filterbox .filter-label {
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-justify-content: space-between;
  -moz-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  -webkit-align-items: center;
  -moz-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}
.filter .filter-main .filterbox .filter-label .title {
  font-size: 30rpx;
  color: #333;
}
.filter .filter-main .filterbox .filter-label label {
  font-size: 26rpx;
  color: var(--themeColor);
}
.filter .filter-main .filterbox .filter-label label radio,
.filter .filter-main .filterbox .filter-label label checkbox {
  display: none;
}
.filter .filter-main .filterbox .filtergroup {
  padding-top: 20rpx;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-justify-content: space-between;
  -moz-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  -webkit-align-items: center;
  -moz-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}
.filter .filter-main .filterbox .filtergroup label {
  float: left;
  position: relative;
  width: -webkit-calc(26.66666667%);
  width: -moz-calc(26.66666667%);
  width: calc((100% - 20rpx) / 3);
  padding: 20rpx 16rpx;
  margin-right: 10rpx;
  margin-bottom: 10rpx;
  background-color: #f6f6f6;
  font-size: 26rpx;
  color: #333;
  line-height: 30rpx;
  text-align: center;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  border-radius: 6rpx;
}
.filter .filter-main .filterbox .filtergroup label:nth-child(3n) {
  margin-right: 0;
}
.filter .filter-main .filterbox .filtergroup label.active {
  color: var(--themeColor);
  background-color: #fffcee;
}
.filter .filter-main .filterbox .filtergroup label.active::before {
  content: '+';
  position: absolute;
  right: 0;
  bottom: 0;
  z-index: 20;
  font-size: 20rpx;
  color: #fff;
  line-height: 20rpx;
  text-align: center;
  -webkit-transform: rotateZ(45deg);
  -moz-transform: rotateZ(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotateZ(45deg);
  transform: rotateZ(45deg);
}
.filter .filter-main .filterbox .filtergroup label.active::after {
  content: '';
  position: absolute;
  bottom: -20rpx;
  right: -20rpx;
  width: 40rpx;
  height: 40rpx;
  background-color: var(--themeColor);
  -webkit-transform: rotateZ(45deg);
  -moz-transform: rotateZ(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotateZ(45deg);
  transform: rotateZ(45deg);
}
.filter .filter-main .filterbox .filtergroup label radio,
.filter .filter-main .filterbox .filtergroup label checkbox {
  display: none;
}
.filter .filter-main .filterbox .filtergroup input {
  width: 36%;
  padding: 12rpx 20rpx;
  text-align: center;
  background-color: #f6f6f6;
  border-radius: 50rpx;
}
.filter .filter-main .filterbox .filtergroup .input-placeholder {
  color: #999;
  text-align: center;
}
.filter .filter-main .filterbox .filtergroup .line {
  width: 34rpx;
  height: 1px;
  background-color: #f6f6f6;
}
.filter .filter-main .filter-btn {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
}
.filter .filter-main .filter-btn button {
  width: 50%;
  padding: 30rpx 0;
  font-weight: normal;
  font-size: 28rpx;
  border-radius: 0;
}
.filter .filter-main .filter-btn .filterbtn-reset {
  color: #333;
  background-color: #f6f6f6;
}
.filter .filter-main .filter-btn .filterbtn-submit {
  color: var(--darkthemeColor);
  background-color: var(--assistColor);
}
.filter .filter-main .animation-reset {
  float: left;
  line-height: 2;
  width: 260rpx;
  margin: 15rpx 12rpx;
  border: 1px solid #f3f0f0;
  text-align: center;
}
.filter .filter-main .animation-button {
  float: left;
  line-height: 2;
  width: 260rpx;
  margin: 15rpx 12rpx;
  border: 1px solid #f3f0f0;
  text-align: center;
}

3.js核心代码

data: {
    listModel: [
      {code: "checkbox1",value: "男模"},
      {code: "checkbox2",value: "女模"},
      {code: "checkbox3",value: "中老年男模"}
    ],
    select_all:false,
    select_all_font: "全选",
    listCity: [
      {code: "checkbox1",value: "北京市"},
      {code: "checkbox2",value: "天津市"},
      {code: "checkbox3",value: "上海市"},
      {code: "checkbox4",value: "重庆市"},
      {code: "checkbox5",value: "河北省"}
    ]
},
 
//全选与反全选
selectall: function() {
    var that = this;
    that.data.select_all = !that.data.select_all
    for (let i = 0; i < that.data.listCity.length; i++) {
      that.data.listCity[i].checked = that.data.select_all
    }
    if (that.data.select_all) {
      that.data.select_all_font = "取消全选"
    } else {
      that.data.select_all_font = "全选"
    }
    that.setData({
      listCity: that.data.listCity,
      select_all: that.data.select_all,
      select_all_font: that.data.select_all_font
    })
},
 
//多选,自定义选中样式
checkChange: function(e) {
    var _listCity = this.data.listCity
    var _value = e.detail.value
    for (let i = 0; i < _listCity.length; i++) {      
      for (let j = 0; j <= _value.length; j++) {
        if (_listCity[i].code == _value[j]) {//如果选中的checkbox等于该checkbox,则将checked属性改为true,结束本次循环
          _listCity[i].checked = true
          break
        }
        _listCity[i].checked = false
      }
    }
    this.setData({
      listCity: _listCity
    })
    if (_value.length == _listCity.length) {
      this.setData({
        select_all: true,
        select_all_font: "取消全选"
      });
    } else {
      this.setData({
        select_all: false,
        select_all_font: "全选"
      });
    }
},
//checkbox组只能选择一个
modelChange: function(e) {
    var listModel = this.data.listModel
    var value = e.detail.value
    for (let i = 0; i < listModel.length; i++) {
      listModel[i].checked = listModel[i].code == value[value.length - 1]
    }
    this.setData({
      listModel
    })
},
  
//隐藏底部导航栏tabbar
translate: function () {
    this.setData({
      isRuleTrue: true
    })
    wx.hideTabBar({
      animation: true
    });
},
 
//显示底部导航栏tabbar
success: function () {
    this.setData({
      isRuleTrue: false
    })
    wx.showTabBar({
      animation: true
    });
},

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 微信小程序中, 侧边栏和商品栏联动的代码可能长这样: ``` // 在侧边栏组件中 // 1. 声明一个自定义事件, 名称为 selectCategory // 2. 在点击侧边栏每一项时, 触发自定义事件, 并将当前点击的分类的id作为参数传递 onCategoryClick: function (e) { let id = e.currentTarget.dataset.id this.setData({ activeCategoryId: id }) this.triggerEvent('selectCategory', { id: id }, {}) } // 在商品列表组件中 // 1. 监听侧边栏的 selectCategory 自定义事件 // 2. 在事件处理函数中, 获取到当前选中的分类id, 然后请求对应的商品数据并更新商品列表 pageLifetimes: { show: function () { this.getGoodsList() } }, methods: { getGoodsList: function () { let that = this WxRequest.get({ url: '/goods/list', data: { categoryId: that.data.categoryId } }).then(res => { if (res.code === 0) { that.setData({ goodsList: res.data.goodsList }) } }) }, onSelectCategory: function (e) { let id = e.detail.id this.setData({ categoryId: id }) this.getGoodsList() } } ``` 希望这能帮到你! ### 回答2: 微信小程序侧边栏和商品栏联动的代码可以通过监听组件的点击事件来实现。以下是一个简单的示例: 1. 在json文件中设置页面结构,包括侧边栏和商品栏的组件,例如: ```json { "usingComponents": { "sidebar": "/components/sidebar", "product-list": "/components/product-list" } } ``` 2. 在wxml文件中编写页面布局,包括侧边栏和商品栏的位置及样式。 ```html <sidebar bind:selectItem="onSidebarItemClick"></sidebar> <product-list products="{{products}}"></product-list> ``` 3. 在js文件中编写逻辑代码,包括监听侧边栏点击事件和更新商品栏数据。 ```javascript Page({ data: { products: [] }, onLoad: function() { // 从服务器获取商品数据,并更新到this.data.products中 }, onSidebarItemClick: function(event) { const selectedCategory = event.detail.category; // 获取点击的侧边栏分类 // 根据分类筛选商品数据,并更新到this.data.products中 this.setData({ products: filteredProducts }); } }) ``` 4. 在侧边栏组件的js文件中,编写点击事件的触发和传递函数。 ```javascript Component({ methods: { onItemClick: function(event) { const category = event.currentTarget.dataset.category; // 获取点击的分类 this.triggerEvent('selectItem', { category: category }); } } }) ``` 这样,当侧边栏中的某个分类被点击时,就会触发selectItem事件,在页面的onSidebarItemClick方法中处理该事件,根据选择的分类筛选商品数据并更新到商品栏中。 ### 回答3: 微信小程序侧边栏商品栏联动代码的实现主要是通过使用wxml、wxss和js文件,并结合微信小程序提供的API来实现的。 首先,需要在wxml文件中创建一个侧边栏和商品栏的布局。可以使用<view>标签来创建侧边栏,并使用<scroll-view>标签来创建商品栏。可以给<scroll-view>标签添加一个id属性,方便在js文件中获取该元素。然后,分别为侧边栏和商品栏中的每个商品项添加一个点击事件,用来触发联动效果。 接下来,在js文件中,可以通过选择器获取侧边栏和商品栏的元素。可以使用wx.createSelectorQuery()方法来获取指定元素的信息。获取到元素后,可以使用setData()方法将数据传递给wxml文件,从而更新显示的内容。 在点击侧边栏的商品项时,可以触发相应的点击事件。点击事件中,可以根据点击的商品项的索引,来滚动到对应商品栏中的商品项位置。可以使用wx.pageScrollTo()方法来实现滚动,传入滚动到的位置的偏移量即可。 此外,还可以在点击侧边栏的商品项时,将点击的商品项的索引传递给商品栏,用来控制商品栏中对应项的样式变化。可以使用wx:if或wx:show来控制相应商品项的显示与隐藏,并给对应的商品项添加不同的样式。 通过以上的代码实现,可以实现微信小程序侧边栏和商品栏的联动效果。用户在侧边栏选择不同的商品项时,商品栏会相应地滚动到对应的位置,并且高亮显示当前选中的商品项。这样可以提升用户体验,使得用户能够快速地浏览和选择商品。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值