uniapp选项卡-过滤器

135 阅读1分钟
<template>
  <view>
    <view class="Info">
      <view @click="chgData(1)">
        <strong>{{stuList.length}}</strong>
        <text>人数</text>
      </view>
      <view @click="chgData(2)">
        <strong>{{qingnianList.length}}</strong>
        <text>青年</text>
      </view>
      <view @click="chgData(3)">
        <strong>{{zhongnianList.length}}</strong>
        <text>中年</text>
      </view>
      <view @click="chgData(4)">
        <strong>{{laonianList.length}}</strong>
        <text>老年</text>
      </view>
    </view>
    <!-- //内容区域 -->
    <view class="studensView">
      <view class="studnesItem" v-for="item in dataList" :key="item.id">
        <view class="stuName">
          {{item.name}}
        </view>
      </view>
    </view>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        stuList: [{
            id: 1,
            age: 20,
            gender: '男',
            name: '亚索'
          },
          {
            id: 2,
            age: 30,
            gender: '女',
            name: '俄洛依'
          },
          {
            id: 3,
            age: 60,
            gender: '男',
            name: '基兰'
          }

        ],
        dataList: [],
        qingnianList: [],
        zhongnianList: [],
        laonianList: []
      }
    },
    methods: {
      //选项卡
      chgData(type) {
        if (type == 1) { //全部
          this.dataList = this.stuList
        } else if (type == 2) { //青年
          this.dataList = this.qingnianList
        } else if (type == 3) { //中年
          this.dataList = this.zhongnianList
        } else if (type == 4) { //老年
          this.dataList = this.laonianList
        }
      },
      //过滤的方法
      filterData() {
        this.qingnianList = this.stuList.filter(v => v.age == 20)
        this.zhongnianList = this.stuList.filter(v => v.age == 30)
        this.laonianList = this.stuList.filter(v => v.age == 60)
      }
    },
    //页面加载时调用
    onLoad() {
      this.chgData(1)
      this.filterData()
    }
  }
</script>

<style scoped>
  .Info {
    display: flex;
    justify-content: space-around;
    margin-top: 20rpx;
  }

  .Info view {
    text-align: center;
  }

  .studensView {
    position: absolute;
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    height: 40rpx;
    top: 120rpx;
    border-top: 1px solid #ccc;
    text-align: center;
  }

  .studensView>view {
    width: 33.33%;
    height: 200rpx;
    border-right: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
    box-sizing: border-box;
  }

  .stuName {
    line-height: 200rpx;
  }
</style>