vue 实现搜索的结果页面支持全选与取消全选功能

(编辑:jimmy 日期: 2024/9/24 浏览:2)

演示地址,打开、搜索、随便点

http://msisliao.github.io/dem...

npm i element-ui -S

// main.js
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

demo功能概览

  • 默认没有全选,搜索时支持全选与取消全选,
  • 将选择的数据添加到已选中,已选删除时改变当前搜索列表的状态与全选按钮的状态
  • 全选时全部追加到已选,取消全选时从已选中删除当前搜索的列表

功能列表

1、搜索时展示相应的数据列表,支持全选与取消全选,(默认展示所有数据时不支持全选)

datas() {
 // 每次搜索的数据 根据下拉菜单的值的变化
 if (this.value !== "") {
 return this.listItem.list.filter(item => {
  return item.BrandNames.includes(this.value);
 });
 } else {
 return this.listItem.list; // 没有搜索的关键词时展示全部数据
 }
 },

2、搜索的下拉菜单去重

filDatas() {
 // 利用reduce 下拉菜单去重
 var obj = {};
 return this.listItem.list.reduce(function(item, next) {
 obj[next.BrandNames] "" : (obj[next.BrandNames] = true && item.push(next));
 return item;
 }, []);
 }

3、当前界面全选时添加到已选中,当前界面取消全选时,从已选的数据删除当前搜索出来的列表数据,

// 每次搜索列表的全选 与 取消全选
 ckAll() {
 this.allck = !this.allck; //点击全选 变 取消选择
 let arrys = []; //存放复选框为取消状态的数据
 if (this.allck) { // 将当前搜索的列表数据追加到已选中
 this.datas.forEach(item => {
  item.ck = true; 
  if (!this.arr.includes(item)) { // 追加复选框为false的数据
  this.arr.push(item);
  this.ckarr.push(item);
  }
 });
 } else {
 this.datas.forEach(item => { item.ck = false; }); //当前搜索的数据列表复选框设为取消状态
 arrys = this.datas.filter(item => { return !item.ck; }); //把复选框为false的数据 拿出来
 this.datas.forEach(items => { //已选那里删除当前搜索列表复选框为false的数据
  arrys.forEach(item => {
  if (item.BrandID == items.BrandID) { this.arr.splice(this.arr.indexOf(item), 1);}
  });
 });
 this.ckarr = []; //当前搜索列表为复选框的数据清空
 }
 },

4、列表选中时添加到已选,全部选中时改变全选状态(变取消全选)

// 监听当前搜索列表的勾选数据
 ckarr: function() {
 if (this.value !== "") {
 this.ckarr.length == this.datas.length "htmlcode">
handleClose(tag) {
 this.arr.splice(this.arr.indexOf(tag), 1); // 点哪删哪
 this.ckarr.forEach(items => { // 判断删除的是否是当前搜索列表的数据 是的话改变全选状态
 if (items.BrandID == tag.BrandID) {
  this.ckarr.splice(this.ckarr.indexOf(tag), 1);
 }
 });
 this.listItem.list.forEach(items => { // 删除已选时改变数据列表状态
 if (items.BrandID == tag.BrandID) { items.ck = false; }
 });
 },

app.vue

<template>
 <div class='tpbox'>
 <el-select v-model="values" filterable placeholder="请选择" size="mini" clearable >
  <el-option v-for="item in filDatas" :key="item.BrandID" :label="item.BrandNames" :value="item.BrandNames" :value-key='item.BrandID'>
  </el-option>
 </el-select>
 <!-- 搜索的列表 -->
 <div v-if="values!=='' && values!==null ">
  <p class='ck-btn-box'>
  <el-button size="mini" @click="ckAll">{{allck"item in datas" :key="item.BrandID">
   <span>AA{{item.BrandTypeName}}</span>
   <span>BB{{item.BrandCName}}</span>
   <span>CC{{item.BrandName}}</span>
   <span>
   <el-checkbox v-model="item.ck" @change="handItem(item)">{{item.BrandNames}}</el-checkbox>
   </span>
  </li>
  </ul>

 </div>
 <!-- 默认列表 -->
 <ul v-else>
  <li v-for="item in datas" :key="item.BrandID">
  <span>AA{{item.BrandTypeName}}</span>
  <span>BB{{item.BrandCName}}</span>
  <span>CC{{item.BrandName}}</span>
  <span>
   <el-checkbox v-model="item.ck" @change="handItem(item)">{{item.BrandNames}}</el-checkbox>
  </span>
  </li>
 </ul>
 <p class='checked-box' v-if="this.arr.length>0">
  已选:
  <span @click="clearAll" class='clearll-txt'>清空</span>
  <el-tag v-for="tag in this.arr" :key="tag.BrandID" closable @close="handleClose(tag)" :disable-transitions=true>
  {{tag.BrandName}} / {{tag.BrandNames}}
  </el-tag>
 </p>
 </div>
</template>
<script>
export default {
 data() {
 return {
 allck: false, //控制全选 当没有任何操作时每次默认为 true
 ckarr: [], //每次搜索出来点击了复选框
 arr: [], //点击了input的数据 存放所有的已选
 values: "",
 listItem:{
 list: [
  {
  BrandTypeName: "大类1 建材/家居 ", //品牌正常
  BrandTypeID: 1,
  BrandCName: "中类1 建筑材料",
  BrandCID: 1,
  BrandName: "小类1 水泥",
  BransID: 1,
  BrandNames: "红水泥",
  BrandID: 1,
  ck: false
  },
  {
  BrandTypeName: "大类1 建材/家居 ", //品牌在多个小类里
  BrandTypeID: 1,
  BrandCName: "中类2 家私定制",
  BrandCID: 2,
  BrandName: "小类2 电饭煲",
  BransID: 2,
  BrandNames: "松下",
  BrandID: 2,
  ck: false
  },
  {
  BrandTypeName: "大类1 建材/家居 ",
  BrandTypeID: 1,
  BrandCName: "中类2 家私定制",
  BrandCID: 2,
  BrandName: "小类3 电压力锅",
  BransID: 3,
  BrandNames: "松下",
  BrandID: 3,
  ck: false
  },
  {
  BrandTypeName: "大类1 建材/家居 ", //品牌在多个中类小类里
  BrandTypeID: 1,
  BrandCName: "中类2 高档家具",
  BrandCID: 3,
  BrandName: "小类2 家具类",
  BransID: 4,
  BrandNames: "品牌",
  BrandID: 4,
  ck: false
  },
  {
  BrandTypeName: "大类1 建材/家居 ",
  BrandTypeID: 1,
  BrandCName: "中类2 豪华家具",
  BrandCID: 4,
  BrandName: "小类3 厨具类",
  BransID: 5,
  BrandNames: "品牌2",
  BrandID: 5,
  ck: false
  },
  {
  BrandTypeName: "大类1 装修/房产 ",
  BrandTypeID: 2,
  BrandCName: "中类2 豪华家具",
  BrandCID: 5,
  BrandName: "小类3 沙发类",
  BransID: 6,
  BrandNames: "品牌3",
  BrandID: 6,
  ck: false
  }
 ]
 }
 };
 },
 computed: {
 datas(){
 if(!this.values){
 return this.listItem.list
 }
 //每次搜索的数据
 if (this.values !== "") {
 return this.listItem.list.filter(item => {
  return item.BrandNames.includes(this.values);
 });
 } 
 },
 filDatas() {
 //select下拉菜单去重 相同名字的子选项会存放在多个类别里
 var obj = {};
 return this.listItem.list.reduce(function(item, next) {
 obj[next.BrandNames] "" : (obj[next.BrandNames] = true && item.push(next));
 return item;
 }, []);
 }
 },
 watch: {
 // 监听每次搜索时的数据变化
 datas: function(ary) {
 //搜索数据变化时 如果搜的结果全部是已选 第二次搜这个关键词就变成 取消选择
 if (this.values !== "") {
 this.allck = false; //默认每次搜索时是全选状态 需判断之前是否全选中的 有的话就是取消全选
  ary.every( item => { item.ck "") {
 this.ckarr.length == this.datas.length "color: #ff0000">总结

以上所述是小编给大家介绍的vue 实现搜索的结果页面支持全选与取消全选功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

一句话新闻

一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?