微信小程序功能——展开和收起查看

时间:2024-04-05 15:04:03

效果图

这里我是控制只显示2个数组

微信小程序功能——展开和收起查看
微信小程序功能——展开和收起查看

1.wxml

在{{!showMore? ‘hiddenmore’ : ‘showmore’}}中,如果showMore为false,则hiddenmore下的more-item列表都不显示

// WXML
<view class="wrong-rate">
    <view class="wr-title">本班错题知识点汇总</view>
    <view class="{{!showMore? 'hiddenmore' : 'showmore'}}">
      <view wx:for="{{rankList}}" wx:for-item="item" wx:for-index="idx" wx:key="" class="wr-list {{idx>1 ? 'more-item' : ''}}">
        <view>知识点1错误率({{item.percentage0}}%),{{item.amount}}</view>
        <view>其中:概念题错误率({{item.percentage1}}%)</view>
        <view>其中:基础题错误率({{item.percentage2}}%)</view>
        <view>其中:综合题错误率({{item.percentage3}}%)</view>
      </view>
    </view>
    <block wx:if="{{rankList.length>2}}">
      <view wx:if="{{showMore}}" class='' bindtap='listToggle'>
        <image style='width:38rpx;height:31rpx;transform:rotate(180deg);' src="/static/images/down1.png"></image>
      </view>
      <view wx:else class='' bindtap='listToggle'>
        <image style='width:38rpx;height:31rpx;' src="/static/images/down1.png"></image>
      </view>
    </block>
</view>

2.WXSS

// WXSS
.wrong-rate {
  text-align: center;
}

.wr-title {
  margin-bottom: 20rpx;
}

.wr-list {
  font-size: 30rpx;
  line-height: 50rpx;
  margin-bottom: 20rpx;
}

.hiddenmore .more-item {
  display: none;
}

3.JS

通过点击事件重置showMore的值传递到视图层

// JS
data{
    showMore: false,
    rankList: [
      {
        amount:9.37,
        percentage0:30,
        percentage1: 30,
        percentage2: 30,
        percentage3: 30
      },
      {
        amount: 9.38,
        percentage0: 10,
        percentage1: 10,
        percentage2: 10,
        percentage3: 10
      },
      {
        amount: 9.39,
        percentage0: 20,
        percentage1: 20,
        percentage2: 30,
        percentage3: 30
      },
    ],//这里面的数据是我写死的数据,应该要通过请求获取的   
},
listToggle: function () {
    this.setData({
      showMore: !this.data.showMore
    })
},