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

Element中table默认支持的,展开和收起功能,如下:
在这里插入图片描述

针对表格的展开收起,本文改造的主要有3点:
1、修改展开/收起的图标;
2、对于不支持展开/收起的行,隐藏图标;
3、点击行,就可以展开/收起对应的内容。

1、修改图标

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

<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

<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方法。

同时可以进行限制只能展开一行,还是都可以展开。
在这里插入图片描述
本文完整代码:

<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>

完整效果:
请添加图片描述

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

英子的搬砖日志

您的鼓励是我创作的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值