修改element-ui table组件展开/收起图标、支持点击行展开/收起、隐藏不可展开行得图标

Element中table默认支持的,展开和收起功能,如下:

针对表格的展开收起,本文改造的主要有3点:

1、修改展开/收起的图标;

2、对于不支持展开/收起的行,隐藏图标;

3、点击行,就可以展开/收起对应的内容。

1、修改图标

这个单纯的样式还是挺好改的。

javascript 复制代码
<el-table class="table-wrap"></el-table>

<style lang="less">
.table-wrap {
  .el-table__expand-icon {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  .el-table__expand-icon .el-icon-arrow-right:before {
    content: "\e6d9";
    border: 1px solid #ccc;
    padding: 1px;
  }
  .el-table__expand-icon--expanded .el-icon-arrow-right:before {
    content: "\e6d8";
  }
}
</style>

效果如下:

2、不可展开行,隐藏图标

这个实现是通过给el-table中不可展开的行,增加一个样式,利用el-table行的 className 的回调方法row-class-name

javascript 复制代码
<el-table :row-class-name="getRowClass"></el-table>

methods: {
    getRowClass({ row }) {
      if (!row.children) return "hide-expand";
    }
}

<style>
  // 没有展开行时,隐藏展开/折叠操作按钮
  .hide-expand {
    td:first-child .cell {
      visibility: hidden;
    }
  }
</style>

效果如下:

3、点击行,展开/收起

利用el-table的row-click事件和toggleRowExpansion方法。

同时可以进行限制只能展开一行,还是都可以展开。

本文完整代码:

javascript 复制代码
<template>
  <div>
    <el-button type="primary" @click="openAll = true">可展开全部</el-button>
    <el-button type="primary" @click="openAll = false">只能展开一个</el-button>
    <el-table
      :data="tableData"
      :row-class-name="getRowClass"
      :border="true"
      :stripe="true"
      @row-click="clickRow"
      class="table-wrap"
      ref="tableRef"
    >
      <el-table-column type="expand" align="center" width="80">
        <template slot-scope="{ row }">
          展开行childre: {{ row.children }}
        </template>
      </el-table-column>
      <el-table-column
        v-for="item in header"
        :key="item.key"
        :label="item.value"
        :prop="item.key"
        :sortable="item.sortable ? item.sortable : false"
        align="center"
      >
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      openAll: true,
      header: [
        {
          key: "id",
          value: "ID"
        },
        {
          key: "title",
          value: "名称"
        },
        {
          key: "tag",
          value: "标签"
        },
        {
          key: "typeText",
          value: "方式"
        },
        {
          key: "cycle",
          value: "周期"
        },
        {
          key: "status",
          value: "状态"
        }
      ],
      tableData: [
        {
          id: 1,
          title: "test111122222",
          tag: "",
          typeText: "定时任务",
          cycle: "每天 9:00",
          status: "草稿",
          sortable: true,
          children: [
            {
              id: 11,
              title: "test1-1",
              tag: "",
              typeText: "定时任务",
              cycle: "每天 9:00",
              status: "草稿"
            }
          ]
        },
        {
          id: 2,
          title:
            "名称很长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长",
          tag: "hhh",
          typeText: "定时任务",
          cycle: "每天 12:00",
          status: "草稿",
          children: [
            {
              id: 21,
              title: "test1-1",
              tag: "",
              typeText: "定时任务",
              cycle: "每天 9:00",
              status: "草稿"
            }
          ]
        },
        {
          id: 3,
          title: "测试测试测试",
          tag: "hhh",
          typeText: "定时任务",
          cycle: "每天 00:00",
          status: "草稿",
          children: [
            {
              id: 31,
              title: "test1-1",
              tag: "",
              typeText: "定时任务",
              cycle: "每天 9:00",
              status: "草稿"
            },
            {
              id: 32,
              title: "test3-2",
              tag: "",
              typeText: "定时任务",
              cycle: "每天 9:00",
              status: "草稿"
            }
          ]
        },
        {
          id: 4,
          title: "嗯嗯嗯",
          tag: "hhh",
          typeText: "定时任务",
          cycle: "每天 10:00",
          status: "草稿",
          children: []
        },
        {
          id: 5,
          title: "test",
          tag: "null",
          typeText: "循环任务",
          cycle: "每天 19:00",
          status: "正式"
        }
      ]
    };
  },
  methods: {
    getRowClass({ row }) {
      if (!row.children || !Array.isArray(row.children) || !row.children.length) return "hide-expand";
    },
    clickRow(row, index, e) {
      if (row.children) {
        const $table = this.$refs.tableRef;
        this.tableData.map(item => {
          // 可以全部都展开
          if (this.openAll) {
            item.expanded = !item.expanded;
          } else {
            // 同一时间只能展开一个
            if (row.id != index.id) {
              $table.toggleRowExpansion(item, false);
              item.expanded = false;
            } else {
              item.expanded = !item.expanded;
            }
          }
        });
        $table.toggleRowExpansion(row);
      }
    }
  }
};
</script>

<style lang="less">
.table-wrap {
  .el-table__expand-icon {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  .el-table__expand-icon .el-icon-arrow-right:before {
    content: "\e6d9";
    border: 1px solid #ccc;
    padding: 1px;
  }
  .el-table__expand-icon--expanded .el-icon-arrow-right:before {
    content: "\e6d8";
  }
  // 没有展开行时,隐藏展开/折叠操作按钮
  .hide-expand {
    td:first-child .cell {
      visibility: hidden;
    }
  }
}
</style>

完整效果:

相关推荐
Autumn_ing8 小时前
Design.md生成UI怎么验收?举例运营后台的颜色、状态和数据核对
前端·ui·ai·设计系统·paico·ai生成ui·design.md
特立独行的猫a13 小时前
仓颉语言UI开发实战:我用仓颉写了个桌面版力扣
算法·leetcode·ui·tauri·仓颉·cj-tarui
Wang's Blog13 小时前
Java 项目实战: 交友项目 - JWT 概述:为什么要用 Token 换掉 Session
java·ui·交友
骑士雄师18 小时前
食堂的节点和提示词的关系
ui·节点和提示词
兰亭妙微UI设计公司20 小时前
兰亭妙微交互设计公司:从4个方面聊聊UI设计规范
ui·设计规范
空堂与归1 天前
Gemini 4 Argon 发布:物理推理模型控机器人?
人工智能·gpt·ui·ai·机器人
兰亭妙微UI设计公司2 天前
兰亭妙微用户体验设计公司的设计细节:关于提示的那些事
ui·ux·ue
UWA2 天前
UGUI性能排查实战:隐藏血条后耗时下降,减少顶点为何没改善
ui·性能优化·cpu·游戏开发
技术人生0012 天前
Selenium4 Python 完整教程(适合网页自动化、页面数据采集、UI自动化测试)
python·ui·自动化