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

vue3-treeselect數(shù)據(jù)綁定失敗的解決方案

 更新時(shí)間:2024年05月23日 15:43:48   作者:<Michael>  
這篇文章主要介紹了vue3-treeselect數(shù)據(jù)綁定失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vue3-treeselect數(shù)據(jù)綁定失敗

目標(biāo)插件

https://github.com/megafetis/vue3-treeselect

問(wèn)題描述

是用這個(gè)插件做數(shù)據(jù)綁定的時(shí)候會(huì)出現(xiàn)這樣的問(wèn)題,當(dāng)?shù)谝淮钨x值(v-model)時(shí),可以實(shí)現(xiàn)選中,但當(dāng)數(shù)據(jù)變化時(shí),選中的數(shù)據(jù)還是之前的數(shù)據(jù),不會(huì)自動(dòng)的切換選中的數(shù)據(jù)。

              <treeselect
                v-model="form.parentId"
                :options="deptOptions"
                :normalizer="normalizer"
                placeholder="選擇上級(jí)部門(mén)"
                ref="deptTree"
              />

即代碼中的form-parentId變化時(shí),仍然顯示選中之前的數(shù)據(jù)。

解決方案

手動(dòng)選中

deptTree.value?.select(deptTree.value?.getNode(row.deptId));

這里的deptTree是獲取的treeselectDom對(duì)象

const deptTree: Ref<typeof Treeselect | null> = ref(null);

vue3-treeselect綁定數(shù)據(jù)有bug問(wèn)題

Vue3-treeSelect,在第一次綁定值的時(shí)候沒(méi)有問(wèn)題,但是第二次開(kāi)始無(wú)法綁定,知道各位有沒(méi)有什么好的解決方法。

所以,我重寫(xiě)了個(gè)簡(jiǎn)單的,沒(méi)那么多功能的就只有v-model,options,placeholder,normalizer4個(gè)參數(shù),下面把代碼貼出來(lái),需要注意的是,placeholder,normalizer這倆是非必須項(xiàng),如果不需要可以不寫(xiě),placeholder不寫(xiě),默認(rèn)是空,normalizer不寫(xiě)默認(rèn)是

{
id: ‘id',
label: ‘label',
children: ‘children',
}

不過(guò)大佬們看看代碼估計(jì)也就懂了

<template>
  <div class="tree-container">
    <el-select
      ref="singleTree"
      v-model="singleSelectTreeVal"
      class="vab-tree-select"
      clearable
      :placeholder="placeholder"
      popper-class="select-tree-popper"
      value-key="id"
      @clear="selectTreeClearHandle('single')"
    >
      <el-option :value="singleSelectTreeKey">
        <el-tree
          id="singleSelectTree"
          ref="singleSelectTree"
          :current-node-key="singleSelectTreeKey"
          :data="selectTreeData"
          :default-expanded-keys="selectTreeDefaultSelectedKeys"
          :highlight-current="true"
          :node-key="selectTreeDefaultProps.id"
          :props="selectTreeDefaultProps"
          @node-click="selectTreeNodeClick"
        >
          <template #defalut="{ node }" class="vab-custom-tree-node">
            <span class="vab-tree-item">{{ node.label }}</span>
          </template>
        </el-tree>
      </el-option>
    </el-select>
  </div>
</template>
<script>
  import { onBeforeMount, onMounted, reactive, toRefs, watch } from 'vue'

  export default {
    name: 'VabSingleSelectTree',
    props: {
      //這里是綁定參數(shù)
      modelValue: {
        type: Number,
        default: undefined,
      },
      //這里是數(shù)組
      options: {
        type: Array,
        default: undefined,
      },
      //placeholder
      placeholder: {
        type: String,
        default: '',
      },
      //這里是轉(zhuǎn)換方法
      normalizer: {
        type: Object,
        default: undefined,
      },
    },
    emits: ['update:modelValue'],
    // { emit }
    setup(props, { emit }) {
      //$emit('update:modelValue', $event.target.value)
      const state = reactive({
        singleSelectTree: null,
        singleTree: null,
        singleSelectTreeKey: props.modelValue,
        singleSelectTreeVal: null,
        selectTreeData: props.options,
        selectTreeDefaultSelectedKeys: [],
        selectTreeDefaultProps: props.normalizer,
      })
      onBeforeMount(() => {
        defaultNormalizer()
      })
      //首次加載
      onMounted(() => {
        initialize()
      })
      watch(props, (newValue) => {
        //這里props里的值不會(huì)自動(dòng)賦值給state中常量,只有第一次過(guò)來(lái)的時(shí)候才會(huì)賦值之后需要手動(dòng)賦值
        state.singleSelectTreeKey = newValue.modelValue
        state.selectTreeData = newValue.options
        initialize()
      })

      //防止不寫(xiě)Normalizer報(bào)錯(cuò)
      const defaultNormalizer = () => {
        if (!state.selectTreeDefaultProps) {
          state.selectTreeDefaultProps = {
            id: 'id',
            label: 'label',
            children: 'children',
          }
        }
      }
      //初始化
      const initialize = () => {
        if (state.singleSelectTreeKey != null) {
          state['singleSelectTree'].setCurrentKey(state.singleSelectTreeKey) // 設(shè)置默認(rèn)選中
          let node = state['singleSelectTree'].getNode(
            state.singleSelectTreeKey
          )
          state.singleSelectTreeVal =
            node.data[state.selectTreeDefaultProps['label']]
          state.singleSelectTreeKey =
            node.data[state.selectTreeDefaultProps['id']]
        } else {
          selectTreeClearHandle()
        }
      }
      // 清除單選樹(shù)選中
      const selectTreeClearHandle = () => {
        state.selectTreeDefaultSelectedKeys = []
        clearSelected()
        emit('update:modelValue', null)
        state.singleSelectTreeVal = ''
        state.singleSelectTreeKey = null
        state['singleSelectTree'].setCurrentKey(null) // 設(shè)置默認(rèn)選中
      }
      const clearSelected = () => {
        const allNode = document.querySelectorAll(
          '#singleSelectTree .el-tree-node'
        )
        allNode.forEach((element) => element.classList.remove('is-current'))
      }
      const selectTreeNodeClick = (data) => {
        state.singleSelectTreeVal = data[state.selectTreeDefaultProps['label']]
        state.singleSelectTreeKey = data[state.selectTreeDefaultProps['id']]
        emit('update:modelValue', state.singleSelectTreeKey)
        state['singleTree'].blur()
        //data
        // if (data.rank >= this.selectLevel) {
        //
        // }
      }
      return {
        ...toRefs(state),
        selectTreeClearHandle,
        selectTreeNodeClick,
        defaultNormalizer,
        initialize,
      }
    },
  }
</script>

<style scoped></style>
/* .vab-hey-message */
    .vab-hey-message {
      @mixin vab-hey-message {
        min-width: 246px;
        padding: 15px;
        background-color: $base-color-white;
        border-color: $base-color-white;
        box-shadow: 0 5px 10px rgba(0, 0, 0, 0.15);

        .el-message__content {
          padding-right: $base-padding;
          color: #34495e;
        }

        .el-icon-close {
          color: #34495e;

          &:hover {
            opacity: 0.8;
          }
        }
      }

有需要的各位隨意取用!

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue實(shí)現(xiàn)tab欄點(diǎn)擊高亮效果

    vue實(shí)現(xiàn)tab欄點(diǎn)擊高亮效果

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)tab欄點(diǎn)擊高亮效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • Vue3實(shí)現(xiàn)自定義Input組件的示例詳解

    Vue3實(shí)現(xiàn)自定義Input組件的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何使用Vue3自定義實(shí)現(xiàn)一個(gè)類似el-input的組件,可以v-model雙向綁定,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • Vue打包路徑配置過(guò)程

    Vue打包路徑配置過(guò)程

    這篇文章主要介紹了Vue打包路徑配置過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 在vue中使用jsx語(yǔ)法的使用方法

    在vue中使用jsx語(yǔ)法的使用方法

    這篇文章主要介紹了在vue中使用jsx語(yǔ)法的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Vue3如何動(dòng)態(tài)設(shè)置ref

    Vue3如何動(dòng)態(tài)設(shè)置ref

    文章介紹了在某些場(chǎng)景下,需要根據(jù)動(dòng)態(tài)數(shù)據(jù)來(lái)設(shè)置ref,例如在表格中的輸入框需要聚焦時(shí),需要為每個(gè)輸入框設(shè)置一個(gè)ref,通過(guò)點(diǎn)擊編輯按鈕,可以自動(dòng)聚焦到相應(yīng)的輸入框中
    2024-12-12
  • vue-cil之a(chǎn)xios的二次封裝與proxy反向代理使用說(shuō)明

    vue-cil之a(chǎn)xios的二次封裝與proxy反向代理使用說(shuō)明

    這篇文章主要介紹了vue-cil之a(chǎn)xios的二次封裝與proxy反向代理使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue中$once的兩個(gè)實(shí)用小技巧分享

    Vue中$once的兩個(gè)實(shí)用小技巧分享

    $once是一個(gè)函數(shù),可以為Vue組件實(shí)例綁定一個(gè)自定義事件,但該事件只能被觸發(fā)一次,觸發(fā)之后隨即被移除,下面這篇文章主要給大家介紹了關(guān)于Vue中$once的兩個(gè)實(shí)用小技巧,需要的朋友可以參考下
    2022-04-04
  • mui-player自定義底部導(dǎo)航在vue項(xiàng)目中顯示不出來(lái)的解決

    mui-player自定義底部導(dǎo)航在vue項(xiàng)目中顯示不出來(lái)的解決

    這篇文章主要介紹了mui-player自定義底部導(dǎo)航在vue項(xiàng)目中顯示不出來(lái)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 詳談vue+webpack解決css引用圖片打包后找不到資源文件的問(wèn)題

    詳談vue+webpack解決css引用圖片打包后找不到資源文件的問(wèn)題

    下面小編就為大家分享一篇詳談vue+webpack解決css引用圖片打包后找不到資源文件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • vue-cli3+typescript新建一個(gè)項(xiàng)目的思路分析

    vue-cli3+typescript新建一個(gè)項(xiàng)目的思路分析

    這篇文章主要介紹了vue-cli3+typescript新建一個(gè)項(xiàng)目的思路,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08

最新評(píng)論

随州市| 沧州市| 岑溪市| 永德县| 巴林左旗| 万荣县| 盐源县| 梁平县| 尉氏县| 南部县| 林州市| 全州县| 自治县| 汉源县| 鸡东县| 家居| 稷山县| 射洪县| 永修县| 新河县| 正定县| 塔城市| 应城市| 九台市| 策勒县| 京山县| 林周县| 哈尔滨市| 邯郸县| 铜鼓县| 司法| 大邑县| 依兰县| 汝城县| 恭城| 乐陵市| 开原市| 通山县| 株洲市| 荥经县| 宁明县|