el-tree与el-select结合,vue组件

<template>
    <div class="qhTree-main">
       <el-select
        style="text-overflow: ellipsis;"
        size="mini"
        ref="selectTree"
        v-model="checkName"
        placeholder="请选择...">
        <el-option
          :value="treeData"
          style="height: auto"
        >
          <el-tree
            ref="tree"
            lazy
            show-checkbox
            :load="loadFolder"
            node-key="id"
            empty-text="无匹配数据"
            :props="{children: 'children',label: 'name',isLeaf: 'leaf'}"
            @check-change="handleCheckChange"
          />
        </el-option>
      </el-select>
    </div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
  data () {
    return {
      checkName: '',
      treeData: '',
      tempData: [],
      timer: ''
    }
  },
  props: {},
  components: {},
  computed: {
    ...mapGetters({
      userInfo: 'getUserInfo'
    })
  },
  watch: {},
  mounted () {},
  methods: {
    loadFolder (node, resolve) { // 获取区划列表,这个按需使用
      if (node.data === undefined && this.userInfo.code === '0') { // 获取省
        // debugger
        this.$store.dispatch('', {code: this.userInfo.code, op: 2}).then((res) => {
          if (res.data.code === 1) {
            resolve(res.data.result)
          }
        }).catch(err => { console.log(err) })
      } else if (node.data.id.length === 2 || node.data.id.length === 4) { // 获取市县
        // debugger
        this.$store.dispatch('', {code: node.data.id, op: 2}).then((res) => {
          if (res.data.code === 1) {
            resolve(res.data.result)
          }
        }).catch(err => { console.log(err) })
      } else if (node.data.id.length === 6) { // 获取乡镇
        // debugger
        this.$store.dispatch('', {code: node.data.id}).then((res) => {
          if (res.data.code === 1) {
            resolve(res.data.result)
          }
        }).catch(err => { console.log(err) })
      } else { // 获取社区
        // debugger
        this.$store.dispatch('', {code: node.data.id}).then((res) => {
          if (res.data.code === 1) {
            resolve(res.data.result)
          }
        }).catch(err => { console.log(err) })
      }
    },
    handleCheckChange (data, checked, indeterminate) { // 多选
      if (checked) {
        if (data.source !== 0) {
          this.tempData.push(data)
        }
      } else {
        this.tempData.forEach((ele, index) => {
          if (ele.id === data.id) {
            this.tempData.splice(index, 1) // 根据下标删除不需要的对象
          }
        })
      }
      let _this = this
      if (this.timer) {
        clearTimeout(this.timer)
      }
      this.timer = setTimeout(() => { // 全选时会多次触发,设置防抖
        _this.putOut()
      }
        , 500)
    },
    putOut (data) {
      let zw = []
      let code = []
      this.tempData.forEach((ele, index) => {
        zw.push(ele.name)
        code.push(ele.id)
      })
      // debugger
      this.checkName = zw.toString()
      console.log(this.checkName, 'this.checkName')
      // console.log(code.toString(), 'code')
      this.$emit('qhCheckData', {code: code.toString(), zw: this.checkName})
      // this.$refs.tree.setCheckedKeys([])// 清空选择
      // this.tempData = []
    }
  }
}
</script>
<style lang="less" scoped>
.qhTree-main{
  width: 100%;
  height: 100%;

}
 /deep/ .el-select-dropdown__item, .selected, .hover{//dialog样式应设在总的外边
    background-color: #fff;
  }
</style>

在父组件,

<qhTree @qhCheckData="getCheckData" style="display: inline-block;width: 300px;">
</qhTree>





  getCheckData (val) { // 区划树传参
    // debugger
    this.qhCheckData = val
  }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值