亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

elementui?Select選擇器嵌套tree實現(xiàn)TreeSelect方式

 更新時間:2023年10月08日 14:46:09   作者:向錢看`向厚賺  
這篇文章主要介紹了elementui?Select選擇器嵌套tree實現(xiàn)TreeSelect方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

個人實現(xiàn)記錄

  • 效果 可以設(shè)置默認要展開和勾選的狀態(tài)
  • 點擊select里的標簽 關(guān)閉樹形圖對應(yīng)的取消勾選效果

html

<el-select v-model="value1" multiple placeholder="請選擇" @change="changeData">
      <el-option style="height:auto" :value="SelectedArray">
        <el-tree
          :data="dataList"
          show-checkbox
          node-key="id"
          ref="tree"
          :check-strictly="true"
          highlight-current
          :props="defaultProps"
          @check="checkClick"
          :default-expanded-keys="setkey"
          :default-expand-all="false"
        >
        </el-tree>
      </el-option>
    </el-select>
  • default-expand-all 是否默認展開所有節(jié)點
  • default-expanded-keys 默認展開的節(jié)點的 key 的數(shù)組
  • check 當復選框被點擊的時候觸發(fā)
  • -check-strictly 父子節(jié)點不相互關(guān)聯(lián)
  • highlight-current 是否高亮當前選中節(jié)點,默認值是 false。

代碼部分

 data () {
    return {
      setkey: [1], // 默認展開節(jié)點
      dateList: [], // 遍歷用
      SelectedArray: [12, 13], // 選中的數(shù)組
      dataList: [
        {
          id: 1,
          name: '總公司',
          parent_id: null,
          parent_name: null,
          has_children: true,
          children: [
            {
              id: 2,
              name: '1xxxx部門',
              parent_id: 1,
              parent_name: '總公司',
              has_children: true,
              children: [
                {
                  id: 12,
                  name: 'x1x項目',
                  parent_id: 1,
                  parent_name: '1xxxx部門',
                  has_children: false,
                  children: []
                },
                {
                  id: 13,
                  name: 'x22222x項目',
                  parent_id: 2,
                  parent_name: '1xxxx部門',
                  has_children: true,
                  children: [
                    {
                      id: 19,
                      name: 'xxx',
                      parent_id: 13,
                      parent_name: 'x22222x項目',
                      has_children: false,
                      children: []
                    }
                  ]
                }
              ]
            },
            {
              id: 15,
              name: '管理辦公室',
              parent_id: 1,
              parent_name: '總公司',
              has_children: false,
              children: []
            },
            {
              id: 16,
              name: '技術(shù)中心',
              parent_id: 1,
              parent_name: '總公司',
              has_children: false,
              children: []
            }
          ]
        }
      ], // tree數(shù)據(jù)
      value1: [], // select綁定的值
      // 對應(yīng)的字段
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
<script>
// highlight-current是否高亮當前選中節(jié)點,默認值是 false。
  methods: {
    async getTreeData () {
      try {
        const {
          data: { data }
        } = await this.$http.get('http://localhost:8848/treeData')
        console.log(data)
        this.dataList = data.data_list
        console.log(this.dataList)
      } catch (error) {
        console.log(error)
      }
    },
    changeData (e) {
      console.log('選中改變的值', e, this.dateList)
      const setkey = []
      for (let index = 0; index < this.dateList.length; index++) {
        for (let index1 = 0; index1 < e.length; index1++) {
          if (this.dateList[index].name === e[index1]) {
            setkey.push(this.dateList[index].id)
          }
        }
      }
      console.log(setkey)
      this.setkey = setkey
      // 重新 設(shè)置tree
      this.$refs.tree.setCheckedKeys(this.setkey)
    },
    // 點擊樹形圖復選框觸發(fā) 
    checkClick (checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys) {
      //   console.log(checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys)
      //   點擊了復選框 使用this.$refs.tree.getCheckedNodes獲取當前選中的節(jié)點
      const res = this.$refs.tree.getCheckedNodes(false, true) // 這里兩個true,1. 是否只是葉子節(jié)點 2. 是否包含半選節(jié)點(就是使得選擇的時候不包含父節(jié)點)
      console.log('點擊樹形圖獲取當前選中的節(jié)點', res)
      this.dateList = res
      const labelArr = []
      const valueArr = []
      res.forEach((element, index) => {
        labelArr.push(element.name)
        valueArr.push(element.id)
      })
      this.value1 = labelArr // select顯示的數(shù)據(jù)
      this.SelectedArray = valueArr // 我要發(fā)送給后端的數(shù)據(jù)
      console.log(this.value1, this.SelectedArray)
    }
  },
  created () {
  //獲取tree數(shù)據(jù)  data里面寫了
    // this.getTreeData() 
  },
  // 默認選中樹形圖的復選框  回顯的操作
  mounted () {
    this.$refs.tree.setCheckedKeys(this.setkey)
  }
}
</script>

總結(jié)

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

相關(guān)文章

最新評論