element table 动态标题 备注超出三行hover显示


提示:动态表头

一、使用步骤

table:需要匹配一下remark

 		<el-tooltip
                placement="top"
                v-model="scope.row.showTooltip"
                :open-delay="500"
                effect="dark"
                :ref="`tooltip-${scope.row.invoiceId}`"
                :disabled="!scope.row.showTooltip"
              >
                <div slot="content">{{ scope.row.remark }}</div>
                <div @mouseenter="showTips($event, scope.row)" class="myProjectSummary">
                  {{ scope.row.remark }}
                </div>
              </el-tooltip>

js:

 showTips(obj, row) {
      // console.log(row);
      /*obj为鼠标移入时的事件对象*/
      /*currentWidth 为文本在页面中所占的宽度,创建标签,加入到页面,获取currentWidth ,最后在移除*/
      let TemporaryTag = document.createElement("span");
      TemporaryTag.innerText = row.remark;
      TemporaryTag.className = "getTextWidth";
      document.querySelector("body").appendChild(TemporaryTag);
      let currentWidth = document.querySelector(".getTextWidth").offsetWidth;
      document.querySelector(".getTextWidth").remove();
      /*cellWidth为表格容器的宽度*/
      const cellWidth = obj.target.offsetWidth;
      /*当文本宽度小于||等于容器宽度两倍时,代表文本显示未超过两行*/
      currentWidth <= 4 * cellWidth ? (row.showTooltip = false) : (row.showTooltip = true);
      const key = "tooltip-" + row.invoiceId;
      this.$nextTick(() => {
        /* 当表格中有固定列时 一行会出现多个popper 需要隐藏其中一个
        当表头数据是通过循环出来的  需要指定[1]  如果是写死的 去掉[1]即可
        可打印this.$refs[key] 查看结构
        */
        if (this.$refs[key][1].$refs.popper.previousSibling) {
          this.$refs[key][1].$refs.popper.previousSibling.style.display = "none";
        }
        // this.$refs[key].$refs.popper.previousSibling.style.display = 'none'
      });
    },

css:

<style lang="less" scoped>
/deep/.el-tree-node__content {
  height: 50px;
}
.css_pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 10px;
}
.myProjectSummary {
  display: -webkit-box;
  text-overflow: ellipsis;
  overflow: hidden;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
</style>
<style lang="less">
.el-tooltip__popper {
  max-width: 50%;
}
</style>

动态表头匹配
动态

Element Plus tablehover 方法可以通过设置 CSS 样式来实现。具体步骤如下: 1. 添加 hover 样式 在 CSS 样式表中添加以下样式: ``` .el-table__row:hover { background-color: #f5f7fa; cursor: pointer; } ``` 这样当鼠标悬停在表格行上,会将该行背景颜色改变为灰色,并将鼠标指针改变为手型。 2. 设置 table-row-class-name 属性 在使用 Element Plus table 组件,可以通过设置 table-row-class-name 属性来指定表格行的类名。可以将该属性设置为一个函数,该函数返回一个字符串,该字符串为表格行的类名。 在该函数中判断当前行是否被选中或悬停,如果是,则返回对应的类名。示例代码如下: ``` <el-table :data="tableData" :row-class-name="tableRowClassName"> <!-- 表格列 --> </el-table> <script> export default { data() { return { // 表格数据 tableData: [], // 当前悬停的行 hoverRow: null, // 当前选中的行 selectedRow: null }; }, methods: { // 判断行是否被选中或悬停 tableRowClassName({ row, rowIndex }) { if (row === this.selectedRow) { return 'selected-row'; } else if (row === this.hoverRow) { return 'hover-row'; } else { return ''; } } } }; </script> ``` 在上述代码中,tableRowClassName 函数根据当前行是否被选中或悬停,返回对应的类名。在模板中,可以使用该函数来设置 table-row-class-name 属性。 3. 监听 mouseenter 和 mouseleave 事件 在组件中监听表格行的 mouseenter 和 mouseleave 事件,以更新当前悬停的行。示例代码如下: ``` <el-table :data="tableData" :row-class-name="tableRowClassName"> <!-- 表格列 --> </el-table> <script> export default { data() { return { // 表格数据 tableData: [], // 当前悬停的行 hoverRow: null, // 当前选中的行 selectedRow: null }; }, methods: { // 判断行是否被选中或悬停 tableRowClassName({ row, rowIndex }) { if (row === this.selectedRow) { return 'selected-row'; } else if (row === this.hoverRow) { return 'hover-row'; } else { return ''; } }, // 监听 mouseenter 事件 handleMouseEnter(row) { this.hoverRow = row; }, // 监听 mouseleave 事件 handleMouseLeave(row) { this.hoverRow = null; } } }; </script> ``` 在上述代码中,handleMouseEnter 和 handleMouseLeave 方法分别监听 mouseenter 和 mouseleave 事件,以更新当前悬停的行。模板中可以使用 v-on 指令来绑定这两个事件。 ``` <el-table :data="tableData" :row-class-name="tableRowClassName"> <!-- 表格列 --> <template v-slot:row="{row}"> <tr @mouseenter="handleMouseEnter(row)" @mouseleave="handleMouseLeave(row)"> <!-- 表格单元格 --> </tr> </template> </el-table> ``` 在上述代码中,通过 v-slot 来自定义表格行,然后在 tr 元素上绑定 mouseenter 和 mouseleave 事件,以更新当前悬停的行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值