vue-element-UI el-tree 初始化默认选中样式

<el-tree :data="pdfTree" ref="tree" node-key="id" @node-click="handleNodeClick" :current-node-key="currentLivingId"
         default-expand-all/>

说明:

1、node-key="id"不能少

2、:current-node-key="currentLivingId"

async handleNodeClick(v) {
  this.currentLivingId = v.id
  this.$nextTick(() => {
    this.$refs.tree.setCurrentKey(v.id)
  })
  //子节点 点击获取文件
  if (v.children == null) {
    this.$emit("showFile", v)
  } else {
    //文件夹
  }
},

全部代码如下:

<template>
  <el-tree :data="pdfTree" ref="tree" node-key="id" @node-click="handleNodeClick" :current-node-key="currentLivingId"
           default-expand-all/>
</template>
<script>
export default {
  name: "fileTree",
  props: {
    //业务外键Id
    refId: {type: String},
    refType: {default: 'default'},
    groupCode: {default: 'default'},
    keyword: {type: String},
    //预览是否带水印
    watermark: {default: true},
  },
  data() {
    return {
      pdfTree: [],
      //当前选中节点key
      currentLivingId: ''
    }
  },
  watch: {
    refId() {
      this.loadData()
    }
  },
  methods: {
    $init() {
      this.loadData();
    },
    deletePdf(row) {
      this.$confirm('此操作将进行删除信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.post('files/delete/' + row.id)
            .then(({data}) => {
              if (data.success) {
                this.loadData()
                this.$message.success("删除成功")
                this.$emit("uploadYuanwen")
              }
            })
      })
    },
    fileToPdf(row) {
      this.loading = true
      this.$http.post('ofd/fileToOfd/', {fileId: row.id})
          .then(({data}) => {
            if (data && data.success) {
              this.$message.success(data.data)
              this.loadData()
            }
            this.loading = false
          })
    },
    async getNullChildren(v) {
      if (v.children == null) {
        await this.handleNodeClick(v)
      } else {
        await this.getNullChildren(v.children[0])
      }
    },
    async loadData() {
      this.loading = true
      const {data} = await this.$http.post('fileView/fileTree', {
        refId: this.refId,
        refType: this.refType,
        groupCode: this.groupCode
      })
      this.loading = false
      if (data.success) {
        this.loading = false
        if (data && data.success) {
          this.pdfTree = data.data
          if (this.pdfTree.length > 0) {
            // await this.handleNodeClick(this.pdfTree[0])
            await this.getNullChildren(this.pdfTree[0])
          }
          this.$forceUpdate()
        }
      }
      this.loading = false
    },
    async handleNodeClick(v) {
      this.currentLivingId = v.id
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(v.id)
      })
      //子节点 点击获取文件
      if (v.children == null) {
        this.$emit("showFile", v)
      } else {
        //文件夹
      }
    },
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-tree-node.is-current > .el-tree-node__content {
  background: #f0f4ff;
  color: $--color-primary-dark-5;
  font-weight: bold;
}
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

【这个世界会好的】

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值