首页 > 其他分享 >el-tree封装。可以搜索/下拉/高亮/能操作增删改查

el-tree封装。可以搜索/下拉/高亮/能操作增删改查

时间:2024-08-22 16:52:26浏览次数:10  
标签:el default tree 改查 data Boolean type emit

项目场景:

      el-tree树形图组写成一个组件,并控制默认高亮在这里插入图片描述


问题描述

      el-tree树形图组写成一个组件,并控制默认高亮。上边存在搜索框和下拉框。能添加和删除


解决方案:

组件代码:

<template>
  <div class="grid-content bg-purple">
  <!-- 标题 -->
    <div class="tab">{{ title }}</div>
    <!-- input输入框 -->
    <div v-if="showInput">
      <el-input
        style="width: 85%; margin-right: 15px"
        v-model="resource"
        size="small"
        placeholder="搜索"
        prefix-icon="el-icon-search"
      ></el-input>
      <el-link :underline="false"
        ><i
          v-if="!isMysql"
          class="el-icon-folder-add"
          style="font-size: 18px"
          @click="addTree"
        ></i>
        <el-popover
          popper-class="popoverStyle"
          placement="right"
          width="100"
          trigger="hover"
          v-else
        >
          <div>
            <p
              class="mysqlP"
              v-for="item in dict.type.dap_datasource_class"
              :key="item.code"
              @click="addTreeMysql(item.value)"
            >
              {{ item.label }}
            </p>
          </div>
          <i
            slot="reference"
            class="el-icon-folder-add"
            style="font-size: 18px"
          ></i>
        </el-popover>
      </el-link>
    </div>
    <!-- select 下拉框 -->
    <div v-if="showSelect">
      <el-select
        style="width: 60% !important; margin-right: 15px"
        v-model="selectVal"
        size="small"
        placeholder="请选择"
        @change="getChange"
      >
        <el-option
          v-for="item in options"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <el-link :underline="false"
        ><i
          class="el-icon-folder-add"
          style="font-size: 18px"
          @click="addTree"
        ></i>
      </el-link>
    </div>
    <!-- el-tree 树形图 -->
    <div :class="type == 1 ? 'treeBottom' : 'treeBottom2'">
      <el-tree
        :data="deptOptions"
        :props="props"
        node-key="id"
        :default-expanded-keys="defaultExpandedKeys"
        @node-click="handleNodeClick"
        :filter-node-method="filterNode"
        :current-node-key="highlightKey"
        highlight-current
        accordion
        ref="testTree"
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">   
        <!-- 每个节点的图标 -->
          <div >
            <span>
              <i
                v-if="Array.isArray(data.children) && data.children.length > 0"
                style="margin-right: 10px"
                class="el-icon-folder"
              ></i>
              <i v-else style="margin-right: 10px" class="el-icon-tickets"></i>
              {{ node.label }}</span
            >
          </div>
          <!-- 更多操作 -->
          <div
            v-if="showMore"
            class="tree_div"
            :class="
              checkId == data.id || checkId == data.value ? 'tree_checked' : ''
            "
          >
            <el-dropdown
              v-if="node.label != '任务分类' || node.label != '资源目录'"
            >
              <img src="@/assets/image/more.png" alt="" />
              <el-dropdown-menu
                slot="dropdown"
                style="padding: 0 10px; color: #606266"
              >
                <div class="menu">
                  <div
                    v-if="isView"
                    class="caid"
                    style="font-size: 14px"
                    @click="viewTree(node)"
                  >
                    <i style="margin-right: 10px" class="el-icon-view"></i>查看
                  </div>
                  <div
                    v-if="isResource"
                    class="caid"
                    style="font-size: 14px"
                    @click="resTree(node)"
                  >
                    <i
                      style="margin-right: 10px"
                      class="el-icon-s-management"
                    ></i
                    >资源
                  </div>
                  <div
                    class="caid"
                    style="font-size: 14px"
                    @click="editTree(node)"
                  >
                    <i style="margin-right: 10px" class="el-icon-edit"></i>编辑
                  </div>
                  <div
                    v-if="isSubordinate"
                    class="caid"
                    style="font-size: 14px"
                    @click="subordinate(node)"
                  >
                    <i style="margin-right: 10px" class="el-icon-folder-add"></i
                    >下级
                  </div>
                  <div
                    class="caid"
                    style="font-size: 14px; color: red"
                    @click="delTree(node)"
                  >
                    <i style="margin-right: 10px" class="el-icon-delete"></i
                    >删除
                  </div>
                </div>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </span>
      </el-tree>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: "",
    },
    type: {
      type: String,
      default: "1",
    },
    /* 搜索框显示 */
    showInput: {
      type: Boolean,
      default: true,
    },
    /* 下拉框 */
    showSelect: {
      type: Boolean,
      default: false,
    },
    /* 下拉框值 */
    resourceSelect: {
      type: String,
    },
    /* 下拉框选项的值 */
    options: {
      type: Array,
    },
    checkId: {
      type: String,
      default: "",
    },
    /* 树形图data */
    deptOptions: {
      type: Array,
      default: [],
    },
    /* 默认展开节点的值 */
    defaultExpandedKeys: {
      type: Array,
    },
    /* tree型子级的格式 */
    props: {
      type: Object,
    },
    /* 显示增删改操作  */
    showMore: {
      type: Boolean,
      default: true,
    },
    /* 下级跟添加切换 */
    isSubordinate: {
      type: Boolean,
      default: true,
    },
    /* 查看 */
    isView: {
      type: Boolean,
      default: false,
    },
    /* 资源按钮 */
    isResource: {
      type: Boolean,
      default: false,
    },
    /* 默认高亮 */
    highlightKey: {
      type: [String, Number],
      default: "",
    },
    /* 数据库新增 */
    isMysql: {
      type: Boolean,
      default: false,
    },
    /* 数据库 图标显示 */
    isMysqlIcon: {
      type: Boolean,
      default: false,
    },
  },
  dicts: ["dap_datasource_class"],
  data() {
    return {
      resource: "",
      selectVal: this.resourceSelect,
    };
  },
  watch: {
    // 根据目录查找数据
    resource(val) {
      this.$refs.testTree.filter(val);
    },
    // 高亮操作
    highlightKey(val) {
      this.$nextTick(() => {
        this.$refs["testTree"].setCurrentKey(val);
      });
    },
  },
  methods: {
  // 节点筛选
    filterNode(value, data) {
      if (!value) return true;
      var res = "";
      if (data.name) {
        res = data.name.indexOf(value) !== -1;
        return res;
      }
      // console.log("data.label",data.label);
      if (data.label) {
        res = data.label.indexOf(value) !== -1;
        return res;
      }
    },
    // 下拉框改变值
    getChange(id) {
      this.$emit("selectChange", id);
    },
    // 新增
    addTree() {
      this.$emit("addTree");
    },
    
    // 资源
    resTree(data) {
      this.$emit("resTree", data);
    },
    
    // 节点点击
    handleNodeClick(data, node, component) {
      this.$emit("nodeClick", data, node, component);
    },
    
    // 修改 
    editTree(data) {
      this.$emit("editTree", data);
    },
    
    // 下级
    subordinate(data) {
      this.$emit("subordinate", data);
    },
    
    // 删除
    delTree(data) {
      this.$emit("delTree", data);
    },
    
    // 查看
    viewTree(data) {
      this.$emit("viewTree", data);
    },
    /* 数据源添加 */
    addTreeMysql(val) {
      this.$emit("mysqlVal", val);
    },
  },
};
</script>

<style lang="scss" scoped>
.treeBottom {
  margin-top: 15px;
  height: calc(100vh - 220px);
  overflow: hidden;
  overflow-y: auto;
  font-size: 14px;
}
.treeBottom2 {
  margin-top: 15px;
  height: calc(100vh - 250px);
  overflow: hidden;
  overflow-y: auto;
  font-size: 14px;
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
.custom-tree-node {
  &:hover .tree_div {
    display: block;
  }
}

.tree_div {
  width: 10px;
  display: none;
  img {
    height: 12px;
  }
}
.tree_checked {
  display: block;
}
.caid {
  text-align: center;
  height: 20px;
  line-height: 20px;
  margin: 10px 5px;
  cursor: pointer;
  &:hover {
    background-color: #edf6ff;
  }
}
::v-deep #el-popover {
  min-width: 80px !important;
}
.mysqlP {
  cursor: pointer;
  margin: 0px;
  padding: 5px;
  &:hover {
    background-color: #edf6ff;
  }
}
</style>
 <style lang="scss">
.el-popover.popoverStyle {
  height: 158px;
  overflow: auto;
}
.tab {
  font-size: 16px;
  padding-bottom: 10px;
  font-weight: 800;
}
</style>

引用代码:
我在main.js里面引用全局。
根据自己需求,显示想要的条件

<tree-value
            ref="treeValue"
            :deptOptions="deptOptions"
            :props="props"
            :showInput="true"
            :showSelect="false"
            :isSubordinate="false"
            :highlightKey="highlightKey"
            type="2"
            @nodeClick="handleNodeClick"
            @addTree="addTree"
            @delTree="delTree"
            @editTree="editTree"
          ></tree-value>

标签:el,default,tree,改查,data,Boolean,type,emit
From: https://blog.csdn.net/weixin_44563526/article/details/141431482

相关文章

  • build linux kernel
    https://www.kernel.org/doc/html/latest/translations/zh_CN/admin-guide/README.htmlhttps://www.kernel.org/https://docs.kernel.org/6.8/安装内核源代码如果您要安装完整的源代码,请把内核tar档案包放在您有权限的目录中(例如您的主目录)并将其解包:xz......
  • 安裝ComfyUI-Docker & 下載Model & Krita電繪軟件 & krita-ai-diffusion電繪插件 & AU
    1.0安裝ComfyUI-Dockergitclonehttps://github.com/YanWenKun/ComfyUI-Docker下載ComfyUI-Docker。sudochmod-R777*設置ComfyUI-Docker最高讀寫權限。dockerrmcomfyuidockerpullyanwk/comfyui-boot:latest下載Docker鏡像。mkdir./Comfy......
  • 【流量特征+webshell】蚁剑篇 · 一句话木马
    一、准备一句话<?phpeval($_POST[x]);?>打开蚁剑连接webshell二、特征分析通过抓取流量包,分析流量特征一:POST方法,一句话路径图片特征二:请求包存在固定格式字段默认格式为@ini_set("display_errors","0");@set_time_limit(0,在base64数据包中它是QGluaV9zZXQ,在chr编码数......
  • el-select allow-create 后出现重复选项
    前情提要之前封装了一个显示输入建议的组件InputLoadMore,见链接:点击输入框,底部弹出下拉框显示输入建议后来需求更改,还要求:某个表单项选择值后,其他表单项的值自动填充;允许多选;去掉下拉框选项分页加载更多。因此,封装了智能填充组件SmartFill。问题复现SmartFill中输入......
  • Dijkstra、Bellman_Ford、SPFA、Floyd算法复杂度比较
    说明Dijkstra:适用于权值为非负的图的单源最短路径,用斐波那契堆的复杂度O(E+VlgV)BellmanFord:适用于权值有负值的图的单源最短路径,并且能够检测负圈,复杂度O(VE)SPFA:适用于权值有负值,且没有负圈的图的单源最短路径,论文中的复杂度O(kE),k为每个节点进入Queue的次数,且k一般<=2,但此处......
  • 详解Elastic Search及架构
    前言             如果我有三段文本,id分别为0、1、2,具体如下,我要找到哪段文本里有关键词es,这时最容易想到的办法就是依次遍历文本,匹配es,最后将符合的文本id输出。    0 ilike es    1 ilovees    2 iusedevops......
  • Cell子刊|最新研究:多种细胞的花样死法均与表观遗传密切相关
    细胞凋亡是哺乳动物细胞中发现的第一种可被调节的细胞死亡形式,由caspase-3和caspase-7执行。活细胞中caspase-3和caspase-7处于休眠状态,当细胞外细胞因子或细胞内应激信号刺激后,caspase-3和caspase-7由上游caspase-8和caspase-9激活,引发凋亡。当caspase-8被抑制时,相同的细胞死......
  • element plus el-table 合并行或列(根据列表数据动态合并第一列重复的单元格)
    http://element-plus.org/zh-CN/component/table.html#%E5%90%88%E5%B9%B6%E8%A1%8C%E6%88%96%E5%88%97 <scriptsetup>import{onMounted,ref}from'vue'import'./index.css'constobjectSpanMethod=({row,column,rowInde......
  • 【流量特征+webshell】 菜刀 · 一句话流量分析
    连接webshell准备一句话<?phpeval($_POST[caidao]);?>打开菜刀连接webshell使用wireshark查询流量特征分析特征一:POST方法,一句话路径首先,过滤http流量,可以看到有访问webshell后门的路径特征二:UA头均为百度爬虫标识User-Agent:Mozilla/5.0(compatible;Baiduspider......
  • Java毕业设计作品(104):基于thymeleaf前后端分离 校园学校社团网站系统设计与实现
      博主介绍:黄菊华老师《Vue.js入门与商城开发实战》《微信小程序商城开发》图书作者,CSDN博客专家,在线教育专家,CSDN钻石讲师;专注大学生毕业设计教育和辅导。所有项目都配有从入门到精通的基础知识视频课程,学习后应对毕业设计答辩。项目配有对应开发文档、开题报告、任务书......