欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue2+elementUI實現(xiàn)下拉樹形多選框效果實例

 更新時間:2023年07月10日 11:46:20   作者:半獸先生  
這篇文章主要給大家介紹了關(guān)于vue2+elementUI實現(xiàn)下拉樹形多選框效果的相關(guān)資料,這是最近的工作中遇到的一個需求,對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下

效果如圖所示:

1.新建el-select-tree.vue組件

<!--
 * 下拉樹形選擇
-->
<template>
    <el-select ref="selectTree" :value="value" v-model="valueName" :multiple="multiple" :clearable="clearable"
        @clear="clearHandle" @change="changeValue">
        <el-option :value="valueName" class="options">
            <el-tree id="tree-option" ref="selectTree" :accordion="accordion" :data="options" :props="props"
                :node-key="props.value" @node-click="handleNodeClick">
                <span slot-scope="{ data }">
                    <i :class="[data.color != null ? 'ification_col' : '']"
                        :style="{ 'background-color': data.color }"></i>&nbsp;&nbsp;{{ data.name }}
                </span>
            </el-tree>
        </el-option>
    </el-select>
</template>
<script>
export default {
    name: "el-tree-select",
    props: {
        // 配置項
        props: {
            type: Object,
            default: () => {
                return {
                    value: 'id',
                    children: 'children',
                    label: 'name'
                }
            }
        },
        // 選項列表數(shù)據(jù)(樹形結(jié)構(gòu)的對象數(shù)組)
        options: {
            type: Array,
            default: () => {
                return []
            }
        },
        // 初始值(單選)
        value: {
            type: Object,
            default: () => {
                return {}
            }
        },
        // 初始值(多選)
        valueMultiple: {
            type: Array,
            default: () => {
                return []
            }
        },
        // 可清空選項
        clearable: {
            type: Boolean,
            default: true
        },
        // 自動收起
        accordion: {
            type: Boolean,
            default: false
        },
        // 是否多選
        multiple: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            resultValue: [], // 傳給父組件的數(shù)組對象值
            valueName: this.multiple ? [] : '' // 輸入框顯示值
        }
    },
    watch: {
        value() {
            this.resultValue = this.multiple ? this.valueMultiple : this.value;   // 初始值
            this.initHandle()
        }
    },
    mounted() {
        this.resultValue = this.multiple ? this.valueMultiple : this.value;   // 初始值
        this.initHandle();
    },
    methods: {
        // 初始化顯示
        initHandle() {
            if (this.resultValue) {
                if (this.multiple) {
                    // 多選 
                    this.resultValue.forEach(item => this.valueName.push(item.name));
                } else {
                    // 單選
                    this.valueName = this.resultValue.name;
                }
            }
            this.initScroll()
        },
        // 初始化滾動條
        initScroll() {
            this.$nextTick(() => {
                let scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0]
                let scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar')
                scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;'
                scrollBar.forEach(ele => ele.style.width = 0)
            })
        },
        // 切換選項
        handleNodeClick(node) {
            // 設(shè)置點擊葉子節(jié)點后被選中 可以更改為點擊父節(jié)點也生效
            if (node.children == null || node.children == undefined) {
                if (this.multiple) {
                    // 多選(判重后添加)
                    let num = 0;
                    this.valueName.forEach(item => {
                        item == node[this.props.label] ? num++ : num;
                    })
                    if (num == 0) {
                        this.valueName.push(node[this.props.label]); // 輸入框顯示值
                        this.resultValue.push(node);
                    }
                } else {
                    // 單選
                    this.$refs.selectTree.blur();
                    this.valueName = node[this.props.label];
                    this.resultValue = node;
                }
                this.$emit('getValue', this.resultValue);
            }
        },
        // 從輸入框中直接刪除某個值時
        changeValue(val) {
            if (this.multiple) {
                // 多選(同時刪掉傳給父組件中多余的值,再傳給父組件)
                this.resultValue.map((item, index) => {
                    let i = val.indexOf(item.name)
                    if (i == -1) {
                        this.resultValue.splice(index, 1)
                    }
                })
                this.$emit('getValue', this.resultValue);
            } else {
                // 單選
                this.$emit('getValue', val);
            }
        },
        // 清除選中
        clearHandle() {
            this.valueName = this.multiple ? [] : ''
            this.resultValue = []
            this.clearSelected()
            this.$emit('getValue', this.resultValue)
        },
        // 清空選中樣式
        clearSelected() {
            let allNode = document.querySelectorAll('#tree-option .el-tree-node')
            allNode.forEach((element) => element.classList.remove('is-current'))
        }
    }
}
</script>
<style scoped>
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
    height: auto;
    max-height: 300px;
    padding: 0;
    overflow: hidden;
    overflow-y: auto;
}
.el-select-dropdown__item.selected {
    font-weight: normal;
}
ul li>>>.el-tree .el-tree-node__content {
    height: auto;
    padding: 0 20px;
}
.el-tree-node__label {
    font-weight: normal;
}
.el-tree>>>.is-current .el-tree-node__label {
    color: #409EFF;
    font-weight: 700;
}
.el-tree>>>.is-current .el-tree-node__children .el-tree-node__label {
    color: #606266;
    font-weight: normal;
}
.el-popper {
    z-index: 9999;
}
.ification_col {
    width: 20px;
    height: 10px;
    display: inline-block;
}
</style>

2.頁面引入組件使用

<template>
  <div class="selectTree">
    <label>請選擇:</label>
    <!-- 單選 + 默認值 -->
    <!-- <el-select-tree :options="options" :value="value" @getValue="getSelectedValue"></el-select-tree> -->
    <!-- 多選 + 默認值 -->
    <el-select-tree :options="options" :multiple="multiple" :valueMultiple="valueMultiple"
      @getValue="getSelectedValue"></el-select-tree>
  </div>
</template>
<script>
import elSelectTree from '../components/el-select-tree.vue'
export default {
  components: {
    elSelectTree
  },
  data() {
    return {
      // 開啟/關(guān) 多選/單選
      multiple: true,
      value: {
        id: 3,
        name: '張三'
      },
      valueMultiple: [
        {
          id: 3,
          name: '張三'
        }, {
          id: 4,
          name: '李四'
        }
      ],
      options: [
        {
          id: 1,
          name: '一組',
          children: [{
            id: 2,
            name: '第一隊',
            children: [{
              id: 3,
              name: '小張'
            }, {
              id: 4,
              name: '小劉'
            }]
          }]
        },
        {
          id: 5,
          name: '二組',
          children: [{
            id: 6,
            name: '小馬'
          }, {
            id: 7,
            name: '小麗'
          }]
        },
        {
          id: 8,
          name: '三組',
          children: [{
            id: 9,
            name: '小韓'
          }, {
            id: 10,
            name: '小錢'
          }]
        }
      ]
    }
  },
  created() {
  },
  methods: {
    // 組件傳出來的選中結(jié)果
    getSelectedValue(value) {
      console.log('選中的結(jié)果值', value)
    }
  }
}
</script>
<style lang="scss"></style>

總結(jié)

到此這篇關(guān)于vue2+elementUI實現(xiàn)下拉樹形多選框效果的文章就介紹到這了,更多相關(guān)vue2+elementUI下拉樹形多選框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue多頁面項目中路由使用history模式的方法

    vue多頁面項目中路由使用history模式的方法

    這篇文章主要介紹了vue多頁面項目中路由如何使用history模式,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • vue-amap安裝和用法步驟

    vue-amap安裝和用法步驟

    vue-amap是餓了么開源的一套基于?Vue?2.0?和高德地圖的地圖組件。接下來通過本文給大家介紹vue-amap安裝和使用,需要的朋友可以參考下
    2021-12-12
  • 利用Vue2.x開發(fā)實現(xiàn)JSON樹的方法

    利用Vue2.x開發(fā)實現(xiàn)JSON樹的方法

    這篇文章主要給大家介紹了關(guān)于利用Vue2.x開發(fā)實現(xiàn)JSON樹的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • vue項目中使用Svg的方法

    vue項目中使用Svg的方法

    本文主要以 vue-cli3 搭建的項目為例,來聊一下如何在項目中更優(yōu)雅的使用 svg 。感興趣的朋友跟隨小編一起看看吧
    2018-10-10
  • 關(guān)于vue組件的更新機制?resize()?callResize()

    關(guān)于vue組件的更新機制?resize()?callResize()

    這篇文章主要介紹了關(guān)于vue組件的更新機制?resize()?callResize(),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue使用better-scroll實現(xiàn)下拉刷新、上拉加載

    vue使用better-scroll實現(xiàn)下拉刷新、上拉加載

    這篇文章主要為大家詳細介紹了vue使用better-scroll實現(xiàn)下拉刷新、上拉加載,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Vue?中的?computed?和?watch?的區(qū)別詳解

    Vue?中的?computed?和?watch?的區(qū)別詳解

    這篇文章主要介紹了Vue中的computed和watch的區(qū)別詳解,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Vue axios 中提交表單數(shù)據(jù)(含上傳文件)

    Vue axios 中提交表單數(shù)據(jù)(含上傳文件)

    本篇文章主要介紹了Vue axios 中提交表單數(shù)據(jù)(含上傳文件),具有一定的參考價值,有興趣的可以了解一下
    2017-07-07
  • vue實現(xiàn)圖片轉(zhuǎn)pdf的示例代碼

    vue實現(xiàn)圖片轉(zhuǎn)pdf的示例代碼

    這篇文章主要為大家詳細介紹了vue實現(xiàn)圖片轉(zhuǎn)pdf的相關(guān)知識,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的小伙伴可以跟隨小編一起了解一下
    2023-08-08
  • vue項目打包發(fā)布到Nginx后無法訪問后端接口的解決辦法

    vue項目打包發(fā)布到Nginx后無法訪問后端接口的解決辦法

    這篇文章主要給大家介紹了關(guān)于vue項目打包發(fā)布到Nginx后無法訪問后端接口的解決辦法,記錄一下項目需要注意的地方,方便以后快速使用,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-04-04

最新評論