el-table利用type=“expand”根据数据条件埋伏睁开按钮

[复制链接]
发表于 2026-1-27 12:32:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
一:添加className
  1. <el-table :data="tableData" border :loading="loading" :row-class-name="getRowClass" @expand-change="expandchange">
  2.    <el-table-column type="expand">
  3.       <template #default="props">
  4.         {{ props.row.expanded ? '收起' : '展开' }}
  5.       </template>
  6.    </el-table>
  7. // 使用getRowClass针对每一行添加类由于我的是根据内容的pcObj长度只有一个隐藏,多个显示。根据自己条件进行更改
  8. const getRowClass = (row: any) => {
  9.   console.log(row);
  10.   let data = row.row;
  11.   let res = [];
  12.   if (data.pcObj.length > 1) {
  13.     res.push('row-expand-has');
  14.     return res;
  15.   } else {
  16.     res.push('row-expand-unhas');
  17.     return res;
  18.   }
  19. }
复制代码
利用完在控制台可以发现row-expand-unhas已添加乐成


二:添加CSS样式埋伏row-expand-unhas的按钮
  1. .row-expand-unhas .el-table__expand-column {
  2.     pointer-events: none;
  3. }
  4. .row-expand-unhas .el-table__expand-column .el-icon {
  5.     visibility: hidden;
  6. }
复制代码
大概在项目中会存在添加样式不起作用,这时就须要用到样式穿透 :deep()
  1. :deep(.row-expand-unhas .el-table__expand-column) {
  2.   pointer-events: none;
  3. }
  4. :deep(.row-expand-unhas .el-table__expand-column .el-icon) {
  5.   visibility: hidden;
  6. }
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表