elemenu-plus vue3如何下拉带树形结构,setup方式

1、效果图

在这里插入图片描述
2、封装组件SelectTree

<!--
 * @Author: zhangming zhangming@sinoma-tianjin.cn
 * @Date: 2024-03-14 10:45:54
 * @LastEditors: zhangming zhangming@sinoma-tianjin.cn
 * @LastEditTime: 2024-03-14 11:25:51
 * @FilePath: \Frontend\src\common\components\selectTree.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-select ref="mySelect" v-bind="$attrs" v-model="optionValue" :multiple="false" :disabled="disabled">
    <el-option :value="optionValue" :label="optionValue" class="options">
      <el-tree id="tree-option" ref="selectTree" default-expand-all :data="options" @node-click="handleNodeClick" />
    </el-option>
  </el-select>
</template>

<script setup>
import { defineComponent, ref, watch, onMounted } from 'vue';
const props = defineProps({
  modelValue: { type: String, default: '' },
  disabled: {
    type: Boolean,
    default: false,
  },
  options: {
    type: Array,
    default: () => [],
  },
});
const mySelect = ref();
const optionValue = ref('');
const emit = defineEmits(['nodeclick', 'update:modelValue']);

watch(
  () => {
    return props.modelValue;
  },
  () => {
    optionValue.value = getLable(props.options, props.modelValue);
  }
);

const getLable = (arr, value) => {
  let res = '';
  function find(arr, value) {
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].value === value) {
        res = arr[i].label;
      }
      if (arr[i].children && arr[i].children.length) {
        find(arr[i].children, value);
      }
    }
  }
  find(arr, value);
  return res;
};

const handleNodeClick = (node) => {
  optionValue.value = node.label;
  mySelect.value.blur();
  emit('nodeclick', node);
  emit('update:modelValue', node.value);
};
</script>

<style scoped>
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
  height: auto;
  max-height: 274px;
  padding: 0;
  overflow: hidden;
  overflow-y: auto;
}
.el-select-dropdown__item.selected {
  font-weight: normal;
}
ul li :deep(.el-tree .el-tree-node__content) {
  height: auto;
  padding: 0 20px;
}
.el-tree-node__label {
  font-weight: normal;
}
.el-tree :deep(.is-current .el-tree-node__label) {
  color: #409eff;
  font-weight: 700;
}
.el-tree :deep(.is-current .el-tree-node__children .el-tree-node__label) {
  color: #606266;
  font-weight: normal;
}
.selectInput {
  padding: 0 5px;
  box-sizing: border-box;
}
</style>

3、使用

<SelectTree v-model="form.elName" :options="treeList" @nodeclick="handleSelect" />

const handleSelect = (data) => {
  //下拉树形选择
  console.log('下拉树形选择');
  console.log(data);
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员阿明

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

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

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

打赏作者

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

抵扣说明:

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

余额充值