使用vue的多条件筛选以及当前行多余内容隐藏

效果如下:

这是参考了其他博主的代码进行的更改。

实现对三种条件筛选,默认是第一项‘全部’,且实现点击后面的更多出现当前行隐藏的内容

 代码实现:


          <div class="row" v-for="(i, k) in typeList" :key="k">
            <div class="name">{{ i.title }}:</div>
            <div class="content" :class="{ hidden: !i.isActive }">
              <div
                v-for="(item, index) in i.contentList"
                :key="index"
                @click="handleClick(k, item, index)"
                :class="{ actvCss: item.isShow }"
              >
                {{ item.text }}
              </div>
            </div>
            <div class="collapse-right" @click="click(i, k)">
              <span v-if="!i.isActive">更多</span>
              <span v-if="i.isActive">收起</span>
            </div>
          </div>
      <!-- 商品展示 -->
          <div class="ContentBox" v-if="!comData.length">
            <div class="nullCss">暂无相关商品~</div>
          </div>
          <div class="DataListBox">
            <div class="box" v-for="(item, index) in comData" :key="index">
              <div>{{ item.Platform }}</div>
              <div>{{ item.Title }}</div>
              <div>{{ item.Condition }}</div>
            </div>
          </div>
    <!--实现点击的元素的展示-->
        <div v-for="(i, v) in newArr0" :key="v">
          {{ i.text }}
        </div>
data(){
    return{
         activeKey: "",
         newArr0: [],
         filterForm: {
            PlatformVar: "全部", // 表示当前选中的平台    设置默认  不选择时为全部
            TypeVar: "全部", // 表示当前选中的类型
            ConditionVar: "全部", // 表示当前选中的成色
          },
          typeList: [
        {
          title: "平台",
          contentList: [
            {
              id: 1,
              text: "全部",
            },
            {
              text: "淘宝",
            },
            {
              text: "天猫",
            },
            {
              text: "京东",
            },
            {
              text: "闲鱼",
            },
            {
              text: "转转",
            },
            {
              text: "1111",
            },
            {
              text: "222222",
            },
            {
              text: "3333",
            },
          ], // 平台
        },
        {
          title: "类型",
          contentList: [
            {
              id: 2,
              text: "全部",
            },
            {
              text: "CPU",
            },
            {
              text: "显卡",
            },
            {
              text: "内存条",
            },
            {
              text: "硬盘",
            },
          ], // 类型
        },
        {
          title: "成色",
          contentList: [
            {
              id: 3,
              text: "全部",
            },
            {
              text: "全新",
            },
            {
              text: "打折",
            },
            {
              text: "二手",
            },
            {
              text: "停售",
            },
          ], // 成色
        },
      ],
  DataList: [
        // 模拟接口返回的数据结构
        {
          id: 1,
          Title: "i9 13900k",
          Platform: "淘宝",
          Type: "CPU",
          Condition: "全新",
        },
        {
          id: 2,
          Title: "i9 9900ks",
          Platform: "闲鱼",
          Type: "CPU",
          Condition: "停售",
        },
        {
          id: 3,
          Title: "i9 11900k",
          Platform: "天猫",
          Type: "CPU",
          Condition: "全新",
        },
        {
          id: 4,
          Title: "i5 13600k",
          Platform: "转转",
          Type: "CPU",
          Condition: "停售",
        },
        {
          id: 5,
          Title: "i5 10400f",
          Platform: "京东",
          Type: "CPU",
          Condition: "全新",
        },
        {
          id: 6,
          Title: "i3 12100f",
          Platform: "闲鱼",
          Type: "CPU",
          Condition: "打折",
        },
      
      ],

    }
},
//当内容发生变化时,会重新计算(筛选)
computed:{
 comData() {
      return this.DataList.filter((i) => {
        console.log(i);
        if (
          (this.filterForm.PlatformVar === "全部" ||
            this.filterForm.PlatformVar === i.Platform) &&
          (this.filterForm.TypeVar === "全部" ||
            this.filterForm.TypeVar === i.Type) &&
          (this.filterForm.ConditionVar === "全部" ||
            this.filterForm.ConditionVar === i.Condition)
        ) {
          return i;
        }
      });
    },
},

//数据的初始化
 created() {
    this.typeList.forEach((i) => {
    //为对象数组的每一项设置 isActive  为 false
      this.$set(i, "isActive", false);
        //对当前行中的每个元素进行设置
      i.contentList.forEach((item, index) => {
   //设置一个项的为true,其余为false,这样默认第一项有样式
        if (index === 0) {
          this.$set(item, "isShow", true);
        } else {
          this.$set(item, "isShow", false);
        }
      });
    });
  },

methods:{
 handleClick(parentIndex, val, childIndex) {
      // 添加isShow===true ,显示点击出现的颜色
      this.typeList[parentIndex].contentList.map((item) => {
        item.isShow = false;
      });
      this.typeList[parentIndex].contentList[childIndex].isShow = true;

      //选中的数据
      let newArr1 = [];
      this.typeList.map((i) => {
        i.contentList.map((item) => {
          if (item.isShow == true) {
            newArr1.push(item);
          }
        });
      });
      this.newArr0 = newArr1;

      this.filterForm.PlatformVar = this.newArr0[0].text;
      this.filterForm.TypeVar = this.newArr0[1].text;
      this.filterForm.ConditionVar = this.newArr0[2].text;
    },

    click(item, index) {
      this.activeKey = index;
      //判断点击的 ‘activeKey’ 是否等于点击当前行的索引,
      if (this.activeKey === index) {
        item.isActive = !item.isActive;//根据索引值是否相等来对 isActive的值进行取反,实现‘更多’‘收起’的效果
      }
    },
}
.row {
  display: flex;
  margin-bottom: 10px;
  font-size: 20px;
  cursor: pointer;
  border: 1px solid seagreen;
  .name {
    width: 200px;
  }
  .content {
    display: flex;
    flex-wrap: wrap;
    width: 800px;
    div {
      margin-right: 50px;
    }
  }
  .hidden {
    height: 26px;
    overflow: hidden;
  }
}
.actvCss {
  background-color: aquamarine;
}

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用HTML和Vue框架来实现多条件筛选,具体步骤如下: 1. 在HTML中定义多个筛选条件的输入框,例如下拉菜单、文本框等。 2. 在Vue中定义一个数据对象,用于存储筛选条件的值。 3. 使用Vue的v-model指令,将输入框的值绑定到对应的数据对象上。 4. 使用Vue的计算属性computed,根据筛选条件的值,计算出符合条件的数据集合。 5. 在HTML中使用v-for指令,将符合条件的数据集合渲染到页面上。 6. 当筛选条件的值发生变化时,Vue会自动重新计算符合条件的数据集合,并更新页面上的数据。 示例代码如下: ```html <div id="app"> <select v-model="selectedType"> <option value="">全部类型</option> <option value="A">类型A</option> <option value="B">类型B</option> </select> <input type="text" v-model="searchText" placeholder="搜索..."> <ul> <li v-for="item in filteredItems">{{ item }}</li> </ul> </div> <script> new Vue({ el: '#app', data: { selectedType: '', searchText: '', items: ['Item 1', 'Item 2', 'Item 3', 'Item A', 'Item B'] }, computed: { filteredItems: function() { var self = this; return this.items.filter(function(item) { return (self.selectedType === '' || item.indexOf(self.selectedType) !== -1) && (self.searchText === '' || item.indexOf(self.searchText) !== -1); }); } } }); </script> ``` 在这个例子中,我们定义了两个筛选条件:类型和搜索关键字。使用Vue的v-model指令,将这两个条件的值绑定到数据对象selectedType和searchText上。然后使用Vue的计算属性computed,根据这两个条件的值,计算出符合条件的数据集合filteredItems。最后使用Vue的v-for指令,将filteredItems中的数据渲染到页面上。 注意,这只是一个简单的例子,实际的多条件筛选可能会更复杂,需要根据具体的需求进设计和实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值