eltable 合计行添加tooltip

news/2024/7/10 2:57:06 标签: vue, elementui

eltable 合计行添加tooltip

  • 问题描述:
    eltable 合计行单元格内容过长会换行,需求要求合计行数据超长显示 … ,鼠标 hover 时显示提示信息。
    在这里插入图片描述

  • 解决方案:eltable合计行没有对外的修改接口,想法是 自己实现一个tooltip, 为合计行单元添加鼠标移入移出事件,移入显示tooltip,移出隐藏tooltip,tooltip的定位和内容通过移入时拿到单元格位置和内容。

  • 实现代码 (最后有优化代码)

<template>
  <div class="content">
    <el-table show-summary :data="data">
      <el-table-column
        v-for="item in header"
        v-bind="item"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
    </el-table>

    <!-- 自定义tooltip -->
    <Transition name="el-fade-in">
      <div v-show="toolTipVisble" id="customTooltip" ref="customTooltip">
        {{ tipMsg }}
        <div class="popper__arrow"></div>
      </div>
    </Transition>
  </div>
</template>
<script>
export default {
  components: {},

  data() {
    return {
      //合计行提示
      toolTipVisble: false,
      tipMsg: "",

      header: [
        { label: "列1", prop: "col1", width: "70px" },
        { label: "列2", prop: "col2", width: "70px" },
        { label: "列3", prop: "col3", width: "70px" },
        { label: "列4", prop: "col4", minWidth: "70px" },
      ],
      data: [
        {
          col1: "23333333333333",
          col2: "2345679",
          col3: "66666666666666",
          col4: "4",
        },
        { col1: "2", col2: "2", col3: "3", col4: "4" },
        { col1: "2", col2: "2", col3: "3", col4: "4" },
        { col1: "2", col2: "2", col3: "3", col4: "4" },
        { col1: "2", col2: "2", col3: "3", col4: "4" },
      ],
    };
  },

  mounted() {
    this.setSummaryListener();
  },
  methods: {
    setSummaryListener() {
      let that = this;
      let table = document.querySelector(".el-table__footer-wrapper>table");

      this.$nextTick(() => {
        for (let rowIndex = 0; rowIndex < table.rows.length; rowIndex++) {
          let row = table.rows[rowIndex].cells;
          for (let colIndex = 0; colIndex < row.length; colIndex++) {
            let col = row[colIndex];
            let cells = col.getElementsByClassName("cell");

            if (cells && cells.length > 0) {
              let cell = cells[0];
              if (cell.scrollWidth > cell.offsetWidth) {
                cell.onmouseenter = function () {
                  that.setTooltip(true, rowIndex, colIndex, cell);
                };
                cell.onmouseleave = function () {
                  that.setTooltip(false, rowIndex, colIndex, cell);
                };
              }
            }
          }
        }
      });
    },
    setTooltip(isShow, rowIndex, columnIndex, colEl) {
      this.toolTipVisble = isShow;
      if (isShow) {
        this.tipMsg = colEl.innerText || colEl.textContent;

        let toolTip = this.$refs.customTooltip;
        let rect = colEl.getBoundingClientRect();
        //向上偏移量
        const offsetTop = 50;
        toolTip.style.top = rect.top - offsetTop + "px";
        this.$nextTick(() => {
          const cellBorderWidth = 1;

          toolTip.style.left =
            rect.left -
            (toolTip.offsetWidth / 2 -
              (colEl.offsetWidth + cellBorderWidth * 2) / 2) +
            "px";
        });
      }
    },
  },
};
</script>
<style>
/* 合计行单元格样式 */
.el-table__footer-wrapper .el-table__footer .el-table__cell .cell {
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
  white-space: nowrap;
}
</style>

<style lang="scss" scoped>
#customTooltip {
  position: absolute;
  transform-origin: center bottom;
  background: #303133;
  color: #fff;
  border-radius: 4px;
  padding: 10px;
  font-size: 12px;
  line-height: 1.2;
  word-wrap: break-word;

  .popper__arrow {
    position: absolute;
    display: block;
    width: 0px;
    height: 0px;
    bottom: -12px;
    left: 42%;

    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
    border-bottom: 6px solid transparent;
    border-top: 6px solid #303133;
  }
}
.content {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 500px;
}
</style>
  • 实现效果
    在这里插入图片描述
  • 瞅瞅源码
    eltable 数据行单元格提示信息show-overflow-tooltip源码实现思路跟上面差不多。
    单元格的提示信息也是绑定鼠标移入移出事件,提示信息用的el-tooltip。
    el-tooltip:这里el-tooltip标签里面没有内容,之后通过鼠标移入事件绑定。
    在这里插入图片描述
    单元格绑定鼠标事件
    在这里插入图片描述
    referenceElm 绑定目标对象(提示信息定位对象)。
    在这里插入图片描述
  • 优化一下我自己写的tooltip,用el-tooltip实现。
vue"><template>
  <div class="all-overview-content">
    <el-table show-summary :data="data">
      <el-table-column
        v-for="item in header"
        v-bind="item"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
    </el-table>

    <!-- 自定义tooltip -->
    <!-- <Transition name="el-fade-in">
      <div v-show="toolTipVisble" id="customTooltip" ref="customTooltip">
        {{ tipMsg }}
        <div class="popper__arrow"></div>
      </div>
    </Transition> -->

    <el-tooltip
      placement="top"
      ref="tooltip"
      :content="tooltipContent"
    ></el-tooltip>
  </div>
</template>

<script>
export default {
  components: {},

  data() {
    return {
      tooltipContent: "",

      header: [
        { label: "列1", prop: "col1", width: "70px" },
        { label: "列2", prop: "col2", width: "70px" },
        { label: "列3", prop: "col3", width: "70px" },
        { label: "列4", prop: "col4", minWidth: "500px" },
      ],
      data: [
        {
          col1: "23333333333333",
          col2: "2345679",
          col3: "66666666666666",
          col4: "4",
        },
        { col1: "2", col2: "2", col3: "3", col4: "4" },
        { col1: "2", col2: "2", col3: "3", col4: "4" },
        { col1: "2", col2: "2", col3: "3", col4: "4" },
        { col1: "2", col2: "2", col3: "3", col4: "4" },
      ],
    };
  },

  mounted() {
    this.setSummaryListener();
  },
  methods: {
    setSummaryListener() {
      let that = this;
      let table = document.querySelector(".el-table__footer-wrapper>table");

      this.$nextTick(() => {
        for (let rowIndex = 0; rowIndex < table.rows.length; rowIndex++) {
          let row = table.rows[rowIndex].cells;
          for (let colIndex = 0; colIndex < row.length; colIndex++) {
            let col = row[colIndex];
            let cells = col.getElementsByClassName("cell");

            if (cells && cells.length > 0) {
              let cell = cells[0];
              if (cell.scrollWidth > cell.offsetWidth) {
                cell.onmouseenter = function () {
                  that.setTooltip(true, rowIndex, colIndex, cell);
                };
                cell.onmouseleave = function () {
                  that.setTooltip(false, rowIndex, colIndex, cell);
                };
              }
            }
          }
        }
      });
    },
    setTooltip(isShow, rowIndex, columnIndex, colEl) {
      const tooltip = this.$refs.tooltip;
      if (isShow) {
        this.tooltipContent = colEl.innerText || colEl.textContent;
        tooltip.referenceElm = colEl;
        tooltip.$refs.popper && (tooltip.$refs.popper.style.display = "none");
        tooltip.doDestroy();
        tooltip.setExpectedState(true);
        tooltip.handleShowPopper();
      } else {
        tooltip.setExpectedState(false);
        tooltip.handleClosePopper();
      }
    },
  },
};
</script>

<style>
/* 合计行单元格样式 */
.el-table__footer-wrapper .el-table__footer .el-table__cell .cell {
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all;
  white-space: nowrap;
}
</style>

<style lang="scss" scoped>
.all-overview-content {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 500px;
}
</style>

http://www.niftyadmin.cn/n/5397157.html

相关文章

LeetCode_Java_移除链表元素(题目+思路+代码)

203.移除链表元素 给你一个链表的头节点 head 和一个整数 val &#xff0c;请你删除链表中所有满足 Node.val val 的节点&#xff0c;并返回 新的头节点 。 示例 1&#xff1a; 输入&#xff1a;head [1,2,6,3,4,5,6], val 6 输出&#xff1a;[1,2,3,4,5]思路&#xff1a;…

【数仓】基本概念、知识普及、核心技术

一、数仓基本概念 数仓的定义&#xff1a; 数据仓库&#xff08;Data Warehouse&#xff0c;简称DW或DWH&#xff09;是一个面向主题的、集成的、相对稳定的、反映历史变化的数据集合&#xff0c;用于支持管理决策。简言之&#xff0c;它是一个大型存储库&#xff0c;用于存储来…

Python matplotlib

目录 1、安装 matplotlib 2、绘制折线图 修改标签文字和线条粗细 校正图形 3、绘制散点图 绘制单点 绘制一系列点 自动计算数据 删除数据点的轮廓 自定义颜色 使用颜色映射 自动保存图表 4、随机漫步 创建 RandomWalk() 类 选择方向 绘制随机漫步图 给点着色 …

【JS逆向学习】猿人学第二题-动态cookie

声明 本文章中所有内容仅供学习交流使用&#xff0c;不用于其他任何目的&#xff0c;不提供完整代码&#xff0c;抓包内容、敏感网址、数据接口等均已做脱敏处理&#xff0c;严禁用于商业用途和非法用途&#xff0c;否则由此产生的一切后果均与作者无关&#xff01; 本文章未…

Hive UDF 札记

低版本的udf就不说了&#xff0c;太老了&#xff0c;说现在主流的。 1&#xff1a;initialize 方法的进一步理解&#xff1a; 在Apache Hive中&#xff0c;用户自定义函数&#xff08;UDF&#xff09;的initialize方法是一个可选的方法&#xff0c;它属于Hive UDF的生命周期…

基于springboot+vue的共享汽车管理系统(前后端分离)

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、阿里云专家博主、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战&#xff0c;欢迎高校老师\讲师\同行交流合作 ​主要内容&#xff1a;毕业设计(Javaweb项目|小程序|Pyt…

为什么会对猫毛过敏?如何缓解?浮毛克星—宠物空气净化器推荐

猫咪过敏通常是因为它们身上的Fel d1蛋白质导致的&#xff0c;这些蛋白质附着在猫咪的皮屑上。猫咪舔毛的过程会带出这些蛋白质&#xff0c;一旦接触就可能引发过敏症状&#xff0c;比如打喷嚏等。因此&#xff0c;减少空气中的浮毛数量有助于减轻过敏现象。猫用空气净化器可以…

如何在启用Secure Boot的Ubuntu 22.04电脑中安装使用VirtualBox 6.1

我使用的是华为Matebook X Pro笔记本电脑&#xff0c;默认开启了UEFI安全引导&#xff08;UEFI Secure Boot&#xff09;&#xff0c;安装了Windows和Ubuntu双操作系统&#xff0c;平时基本上都是使用Ubuntu 22.04&#xff08;Linux Mint 21.3&#xff09;&#xff0c;使用上也…