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

vue2結(jié)合element-ui實(shí)現(xiàn)TreeSelect樹(shù)選擇功能

 更新時(shí)間:2024年03月12日 16:06:23   作者:小白小白從不日別  
這篇文章主要為大家詳細(xì)介紹了vue2如何結(jié)合element-ui實(shí)現(xiàn)TreeSelect樹(shù)選擇功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下

效果

大致思路

el-select和el-tree進(jìn)行嵌套,將el-tree放到el-option里,循環(huán)遍歷el-option,同時(shí)定義一個(gè)方法比如:formatData,對(duì)樹(shù)形數(shù)據(jù)進(jìn)行遞歸處理,這樣就可以實(shí)現(xiàn)無(wú)論嵌套的層級(jí)有幾層都可以正常渲染在界面上 利用 v-model 和 update:selectValue 實(shí)現(xiàn)父子組件之間的雙向通信,同時(shí)利用computed進(jìn)行監(jiān)聽(tīng)

vue代碼

子組件:TreeSelect.vue

<template>
  <div class="app-container" style="padding: 0">
    <el-select
      class="main-select-tree"
      ref="selectTree"
      v-model="value"
      style="width: 240px"
      clearable
      @clear="clearSelectInput"
    >
      <el-input
        style="width: 220px; margin-left: 10px; margin-bottom: 10px"
        placeholder="輸入關(guān)鍵字進(jìn)行過(guò)濾"
        v-model="filterText"
        clearable
      >
      </el-input>
      <el-option
        v-for="item in formatData(data)"
        :key="item.value"
        :label="item.label"
        :value="item.value"
        style="display: none"
      />
      <el-tree
        class="main-select-el-tree"
        ref="selecteltree"
        :data="data"
        node-key="id"
        highlight-current
        :props="defaultProps"
        @node-click="handleNodeClick"
        :current-node-key="value"
        :expand-on-click-node="true"
        default-expand-all
        :filter-node-method="filterNode"
      />
    </el-select>
  </div>
</template>
    
    <script>
export default {
  props: {
    selectValue: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      filterText: "",
      value: "",
      data: [
        {
          id: 1,
          label: "云南",
          children: [
            {
              id: 2,
              label: "昆明",
              children: [
                {
                  id: 3,
                  label: "五華區(qū)",
                  children: [
                    {
                      id: 8,
                      label: "xx街道",
                      children: [
                        {
                          id: 81,
                          label: "yy社區(qū)",
                          children: [{ id: 82, label: "北辰小區(qū)" }],
                        },
                      ],
                    },
                  ],
                },
                { id: 4, label: "盤(pán)龍區(qū)" },
              ],
            },
          ],
        },
        {
          id: 5,
          label: "湖南",
          children: [
            { id: 6, label: "長(zhǎng)沙" },
            { id: 7, label: "永州" },
          ],
        },
        {
          id: 12,
          label: "重慶",
          children: [
            { id: 10, label: "渝北" },
            { id: 9, label: "合川" },
          ],
        },
        {
          id: 13,
          label: "江蘇",
          children: [{ id: 14, label: "鹽城" }],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  watch: {
    filterText(val) {
      this.$refs.selecteltree.filter(val);
    },
  },
  methods: {
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 遞歸遍歷數(shù)據(jù)
    formatData(data) {
      let options = [];
      const formatDataRecursive = (data) => {
        data.forEach((item) => {
          options.push({ label: item.label, value: item.id });
          if (item.children && item.children.length > 0) {
            formatDataRecursive(item.children);
          }
        });
      };
      formatDataRecursive(data);
      return options;
    },
    // 點(diǎn)擊事件
    handleNodeClick(node) {
      this.value = node.id;
      this.$refs.selectTree.blur();
      this.$emit('update:selectValue', node.label);
    },
    // 清空事件
    clearSelectInput() {
        this.$emit('update:selectValue', '');
        // 獲取 el-tree 實(shí)例的引用
        const elTree = this.$refs.selecteltree;
       // 將當(dāng)前選中的節(jié)點(diǎn)設(shè)置為 null
       elTree.setCurrentKey(null);
    },
  },
};
</script>
    <style>
.main-select-el-tree .el-tree-node .is-current > .el-tree-node__content {
  font-weight: bold;
  color: #409eff;
}
.main-select-el-tree .el-tree-node.is-current > .el-tree-node__content {
  font-weight: bold;
  color: #409eff;
}
</style>

父組件:

<TreeSelect v-model="selectedValue" @update:selectValue="handleSelectValueChange"></TreeSelect>

<el-button size="medium" :disabled="todoIsTotal">交接當(dāng)前{{ tableData.length }}條任務(wù)</el-button>
                
import TreeSelect from "./TreeSelect.vue";

export default {
    components: {
        TreeSelect,
    },
    data() {
        selectedValue: "",
    },
    computed: {
        todoIsTotal() {
            return this.selectedValue === "";
        },
    },
    methods: {
        handleSelectValueChange(value) {
            if (value && value.length > 0) {
                this.selectedValue = value;
            } else {
                this.selectedValue = "";
            }
        },
    },
}

到此這篇關(guān)于vue2結(jié)合element-ui實(shí)現(xiàn)TreeSelect樹(shù)選擇功能的文章就介紹到這了,更多相關(guān)vue2 element-ui實(shí)現(xiàn)TreeSelect樹(shù)選擇內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Nuxt 項(xiàng)目性能優(yōu)化調(diào)研分析

    Nuxt 項(xiàng)目性能優(yōu)化調(diào)研分析

    這篇文章主要介紹了Nuxt 項(xiàng)目性能優(yōu)化調(diào)研分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • vue實(shí)現(xiàn)token過(guò)期自動(dòng)跳轉(zhuǎn)到登錄頁(yè)面

    vue實(shí)現(xiàn)token過(guò)期自動(dòng)跳轉(zhuǎn)到登錄頁(yè)面

    本文主要介紹了vue實(shí)現(xiàn)token過(guò)期自動(dòng)跳轉(zhuǎn)到登錄頁(yè)面,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • vue數(shù)據(jù)雙向綁定原理解析(get & set)

    vue數(shù)據(jù)雙向綁定原理解析(get & set)

    這篇文章主要為大家詳細(xì)解析了vue.js數(shù)據(jù)雙向綁定原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Vue.js實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器應(yīng)用

    Vue.js實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器應(yīng)用

    這篇文章主要為大家詳細(xì)介紹了Vue.js實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • vue打印瀏覽器頁(yè)面功能的兩種實(shí)現(xiàn)方法

    vue打印瀏覽器頁(yè)面功能的兩種實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于vue打印瀏覽器頁(yè)面功能的兩種實(shí)現(xiàn)方法,這個(gè)功能其實(shí)也是自己學(xué)習(xí)到的,做完也有一段時(shí)間了,一直想記錄總結(jié)一下,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • 深入了解Vue.js 混入(mixins)

    深入了解Vue.js 混入(mixins)

    這篇文章主要介紹了Vue.js 混入的相關(guān)資料,文中講解非常細(xì)致,示例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • 如何正確快速使用Vue中的插槽和配置代理

    如何正確快速使用Vue中的插槽和配置代理

    這篇文章主要介紹了正確快速使用Vue中的插槽和配置代理的相關(guān)知識(shí),插槽分為三種,分別是默認(rèn)插槽、具名插槽、作用域插槽,下面分別列出了如何使用這三種插槽,需要的朋友可以參考下
    2023-01-01
  • Vue的H5頁(yè)面喚起支付寶支付功能

    Vue的H5頁(yè)面喚起支付寶支付功能

    目前項(xiàng)目中比較常用的第三方支付無(wú)非就是支付寶支付和微信支付。下面介紹一下Vue中H5頁(yè)面如何使用支付寶支付。這篇文章主要介紹了Vue的H5頁(yè)面喚起支付寶支付,需要的朋友可以參考下
    2019-04-04
  • Vue3集成Leaflet實(shí)現(xiàn)一個(gè)功能完整的地圖可視化組件

    Vue3集成Leaflet實(shí)現(xiàn)一個(gè)功能完整的地圖可視化組件

    在現(xiàn)代 Web 應(yīng)用中,地圖可視化是一個(gè)常見(jiàn)的需求,無(wú)論是展示位置信息、軌跡追蹤,還是數(shù)據(jù)統(tǒng)計(jì)分析,地圖都能提供直觀(guān)的視覺(jué)體驗(yàn),本文將詳細(xì)介紹如何在 Vue 3 項(xiàng)目中集成 Leaflet 地圖庫(kù),實(shí)現(xiàn)一個(gè)功能完整的地圖可視化組件,需要的朋友可以參考下
    2026-01-01
  • springboot+vue使用流式響應(yīng)的實(shí)現(xiàn)示例

    springboot+vue使用流式響應(yīng)的實(shí)現(xiàn)示例

    本文主要介紹了springboot+vue使用流式響應(yīng)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2026-02-02

最新評(píng)論

克山县| 贺州市| 中宁县| 太仓市| 彩票| 河间市| 台中市| 安多县| 邵阳市| 靖远县| 禄丰县| 常德市| 玉溪市| 汶川县| 泰州市| 顺昌县| 闽清县| 蓝山县| 罗田县| 秭归县| 沾化县| 寻乌县| 荆门市| 通城县| 汾西县| 吴堡县| 灵台县| 敖汉旗| 铁力市| 福贡县| 正安县| 山西省| 尖扎县| 涟源市| 富裕县| 静宁县| 阜宁县| 得荣县| 博客| 永昌县| 中阳县|