解决elementUI中表格的行(cell)设置固定的高度后,垂直居中失效问题

在这里插入图片描述
在上图中,我们可以看到elementUI中表格的cell设置固定高度为40后,再设置垂直居样式后,不起作用。
在这里插入图片描述
当你改变display的属性值后,你会发现设置的高度40就没用,他会随着内容的高度而去撑开。这样也不满足需求。
这个需求是继上一篇博客https://blog.csdn.net/CuiCui_web/article/details/106531997设置固定高度,根据高度超出显示气泡之后,新增加了需要垂直居中显示。
下面介绍一下如何显示垂直居中:
利用插槽的方式进行设置:

<template slot-scope="scope">
          <span class="verticalAlign">{{ scope.row.id }}</span>
        </template>

样式设置:

.el-table. td>.cell{
    height: 92px;
  }
.verticalAlign {
    height:92px; // 高度与固定的cell高度一致
    display: table-cell; // 这里需要设置为table-cell才行
    vertical-align: middle; // 设置垂直居中样式即可
  }

如果原本表格中设置了插槽,那么只需要增加verticalAlign类名即可。

完整的 结构代码如下:

<el-table
      class="testHeightTip"
      :data="tableData"
      @cell-mouse-enter="handleCellMouseEnter"
      style="width: 100%" border>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column label="序号"
                       align="center"
                       fixed="left"
                       width="64">
        <template slot-scope="scope">
          <span class="verticalAlign">{{scope.$index  }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="商品 ID"
        prop="id">
        <template slot-scope="scope">
          <span class="verticalAlign">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="商品名称"
        align="left"
        prop="name">
        <template slot-scope="scope">
          <el-tooltip v-if="scope.column.show===true"  popper-class="preTooltip"
                      effect="dark"
                      placement="top">
            <span slot="content" v-html="scope.row.name"></span>
            <span class="verticalAlign"  v-html="scope.row.name"></span>
          </el-tooltip>
          <span class="verticalAlign" v-else v-html="scope.row.name"></span>
        </template>
      </el-table-column>
      <el-table-column
        label="描述"
        prop="desc">
        <template slot-scope="scope">
          <span class="verticalAlign">{{ scope.row.desc }}</span>
        </template>
      </el-table-column>
      <el-table-column label="快捷操作" width="90">
        <template slot-scope="scope" >
          <div class="verticalAlign" >
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <br>
            <el-button
              style="margin-top:15px;"
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

没有垂直居中的图:
在这里插入图片描述
设置样式之后的效果图:
在这里插入图片描述

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值