最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue實現多標簽選擇器

 更新時間:2019年11月28日 17:11:57   作者:彭世瑜  
這篇文章主要為大家詳細介紹了Vue實現多標簽選擇器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Vue實現多標簽選擇器展示的具體代碼,供大家參考,具體內容如下

實現效果

實現代碼

<html lang="en">

<head>
 <title>Document</title>

 <!-- 引入本地組件庫 -->
 <link rel="stylesheet" href="static/element-ui/index.css" >
 <script src="static/element-ui/vue.js"></script>
 <script src="static/element-ui/index.js"></script>

 <!-- 引入CDN樣式 -->
 <!-- <link rel="stylesheet"  > -->
 <!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> -->
 <!-- <script src="https://unpkg.com/element-ui/lib/index.js"></script> -->

 <style>
  .not-active {
   display: inline-block;
   font-size: 12px;
   margin: 5px 8px;
  }
  
  span {
   margin: 0 2px;
  }
 </style>
</head>

<body>

 <div id="app">
  <!-- 待選標簽 -->
  <div v-for='(category, categoryIndex) in categories' :key="category.id">
   <!-- 分類 -->
   <span class="not-active">{{category.name}}:</span>

   <template>
    <span v-if="category.count"class="not-active" @click="clearCategory(category, categoryIndex)"> 不限</span>
    <my-tag v-else>不限</my-tag>
   </template>

   <!-- 標簽 -->
   <template v-for='(child, childIndex) in category.children'>
    <my-tag v-if="child.active" :closable='true' @click-child='clickChild(category, categoryIndex, child, childIndex)'>
     {{ child.name }}
    </my-tag>
    <span v-else class="not-active" @click='clickChild(category, categoryIndex, child, childIndex)'>{{ child.name }}</span>
   </template>
  </div>

  <!-- 已選標簽 -->
  <div v-if='conditions.length'>
   <span class="not-active" @click="clearCondition">清空已選:<span>
    
   <el-tag
   v-for='(condition, index) in conditions' 
   :key="condition.id"
   type="primary"
   :closable="true"
   size="small"
   :disable-transitions="true"
   @close='removeCondition(condition, index)'
   @click='removeCondition(condition, index)'>
    {{condition.name}}
   </el-tag>
  </div>
 </div>

 <script src="./data.js"></script>

 <script>
  // 簡單封裝一個公用組件
  Vue.component('my-tag', {
   template: "<el-tag v-bind='$attrs' v-on='$listeners' effect='dark' size='small' :disable-transitions='true' @click='clickChild' @close='clickChild'><slot></slot></el-tag>",

   methods: {
    clickChild() {
     this.$emit("click-child")
    }
   }
  });

  var app = new Vue({
   el: '#app',
   data() {
    return {
     categories, // 分類標簽,可從外部加載配置
     conditions: [] // 已選條件
    }
   },

   watch: {
    // 監(jiān)聽條件變化,按照請求接口拼裝請求參數
    conditions(val){
     let selectedCondition = {};

     for(let categorie of this.categories){
      let selected_list = [];
      for(let child of categorie.children){
       if(child.active){
        selected_list.push(child.name);
       }
      }
      selectedCondition[categorie.name] = selected_list.join("|")
     }
     console.log(selectedCondition);
    }
   },

   methods: {
    // 處理標簽點擊事件,未選中則選中,已選中則取消選中
    clickChild(category, categoryIndex, child, childIndex) {
     let uid = `${categoryIndex}-${childIndex}`
     child.uid = uid;
     console.log(uid)
     
     // 取消選擇
     if (child.active === true) {
      category.count--;
      child.active = false;
      this.conditions.forEach((conditionChild, index) => {
       if (conditionChild.uid === child.uid) {
        this.conditions.splice(index, 1);
       }
      });
     // 選擇
     } else {
      category.count++;
      child.active = true;
      this.conditions.push(child);
     }
    },
   
    // 清除已選整個類別標簽
    clearCategory(category, categoryIndex) {
     category.count = 0;
     
     // 可選列表均為未選中狀態(tài)
     category.children.forEach(child => {
      child.active = false;
     })

     // 清空該類已選元素
     for (let index = this.conditions.length - 1; index >= 0; index--) {
      const conditionChild = this.conditions[index];
      if (conditionChild.uid.startsWith(categoryIndex)) {
       this.conditions.splice(index, 1);
      }
     }
    },
    
    // 移除一個條件
    removeCondition(condition, index) {
     let categoryIndex = condition.uid.split("-")[0];
     this.categories[categoryIndex].count --;

     this.conditions.splice(index, 1)
     condition.active = false;
    },

    // 清空所有條件
    clearCondition() {
     for(let i = this.conditions.length-1; i >=0 ; i--){
      this.removeCondition(this.conditions[i], i);
     }
    }
   }
  });
 </script>

</body>

</html>

標簽篩選的數據格式

data.js

var categories = [{
 name: '品牌',
 count: 0,
 children: [{
  name: '聯想',
 }, {
  name: '小米',

 }, {
  name: '蘋果',

 }, {
  name: '東芝',

 }]
}, {
 name: 'CPU',
 count: 0,
 children: [{
  name: 'intel i7 8700K',

 }, {
  name: 'intel i7 7700K',

 }, {
  name: 'intel i9 9270K',

 }, {
  name: 'intel i7 8700',

 }, {
  name: 'AMD 1600X',


 }]
}, {
 name: '內存',
 count: 0,
 children: [{
  name: '七彩虹8G',

 }, {
  name: '七彩虹16G',

 }, {
  name: '金士頓8G',

 }, {
  name: '金士頓16G',

 }]
}, {
 name: '顯卡',
 count: 0,
 children: [{
  name: 'NVIDIA 1060 8G',

 }, {
  name: 'NVIDIA 1080Ti 16G',

 }, {
  name: 'NVIDIA 1080 8G',

 }, {
  name: 'NVIDIA 1060Ti 16G',

 }]
}]

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • vue使用動畫實現滾動表格效果

    vue使用動畫實現滾動表格效果

    這篇文章主要為大家詳細介紹了vue使用動畫實現滾動表格效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Vue單頁應用引用單獨的樣式文件的兩種方式

    Vue單頁應用引用單獨的樣式文件的兩種方式

    這篇文章給大家介紹Vue單頁應用如何引用單獨的樣式文件,本文以css文件為例,通過兩種方式給大家介紹的非常詳細,需要的朋友參考下吧
    2018-03-03
  • Vue中添加手機驗證碼組件功能操作方法

    Vue中添加手機驗證碼組件功能操作方法

    組件是Vue.js最強大的功能之一。組件可以擴展HTML元素,封裝可重用的代碼。這篇文章主要介紹了VUE 中添加手機驗證碼組件,需要的朋友可以參考下
    2017-12-12
  • Vue項目實現token無感刷新的示例代碼

    Vue項目實現token無感刷新的示例代碼

    最近在使用系統的過程中,業(yè)務人員反饋剛登錄一會就提示token過期需要重新登錄,所以本文為大家分享一個無感刷新的實現代碼,需要的可以參考下
    2023-07-07
  • Vue源碼學習之數據初始化

    Vue源碼學習之數據初始化

    這篇文章主要為大家介紹了Vue源碼學習之數據初始化實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • vue 登錄滑動驗證實現代碼

    vue 登錄滑動驗證實現代碼

    這篇文章主要介紹了vue 登錄滑動驗證實現代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • Vue中的數據監(jiān)聽和數據交互案例解析

    Vue中的數據監(jiān)聽和數據交互案例解析

    這篇文章主要介紹了Vue中的數據監(jiān)聽和數據交互案例解析,在文章開頭部分先給大家介紹了vue中的數據監(jiān)聽事件$watch,具體代碼講解,大家可以參考下本文
    2017-07-07
  • vue中修改瀏覽器圖標和名字的幾種方式

    vue中修改瀏覽器圖標和名字的幾種方式

    當針對不同客戶需要提供不同的圖標時,頁面其他圖標好替換,但是網頁圖標就不太一樣了,下面這篇文章主要給大家介紹了關于vue中修改瀏覽器圖標和名字的幾種方式,需要的朋友可以參考下
    2022-10-10
  • vue 系列——vue2-webpack2框架搭建踩坑之路

    vue 系列——vue2-webpack2框架搭建踩坑之路

    本文從零搭建vue項目,給大家分享了我的vue2-webpack2框架搭建踩坑之路,需要的朋友可以參考下
    2017-12-12
  • Vue cli+mui 區(qū)域滾動的實例代碼

    Vue cli+mui 區(qū)域滾動的實例代碼

    下面小編就為大家分享一篇Vue cli+mui 區(qū)域滾動的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01

最新評論

四川省| 新竹市| 东方市| 集安市| 固原市| 兴业县| 聊城市| 张家港市| 普洱| 阳西县| 凤山市| 通榆县| 桃江县| 南华县| 平湖市| 揭东县| 育儿| 全椒县| 潼南县| 基隆市| 松原市| 临西县| 板桥市| 陇南市| 山阳县| 拉孜县| 霞浦县| 慈溪市| 社会| 炎陵县| 阜新市| 嵩明县| 渝中区| 郑州市| 澄江县| 邹平县| 准格尔旗| 江安县| 资兴市| 安岳县| 朝阳区|