elementui表格单元格合并及表头单元格合并以及表头斜线分隔

elementui表格单元格合并及表头单元格合并

页面效果如下

在这里插入图片描述

在这里插入图片描述

代码
html部分

 <base-table
          default-height="true"
          class="numTable sorTable noTableBorder"
          :border-style="true"
          :columns="column"
          :data-source="numTable"
          :loading="loading"
          :layout="layout ? layout : `total, prev, pager, next, jumper`"
          :span-method="cellMerge"
          :header-cell-style="headMerge"
          :cell-style="cellStyleMerge"
        >
        </base-table>

js部分

// 在获取导数据后调用该方法
getSpanArr(data) {
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr.push(1);
          this.pos = 0;
        } else {
          // 判断当前元素与上一个元素是否相同
          if (data[i].productType === data[i - 1].productType) {
            this.spanArr[this.pos] += 1;
            this.spanArr.push(0);
          } else {
            this.spanArr.push(1);
            this.pos = i;
          }
        }
      }
    },

    cellMerge({ row, column, rowIndex, columnIndex }) {
      let isMerge = -1;
      if (row.productType === row.column) {
        isMerge = (rowIndex) ;
      }
      if (columnIndex === 0) {
        // 第一行和第二行 的第一列合并第二列
        if (
          rowIndex === Number(isMerge)
          // rowIndex === this.qmTable.length - 1
        ) {
          return {
            rowspan: 1,
            colspan: 2
          };
        } else {
          const _row = this.spanArr[rowIndex];
          const _col = _row > 0 ? 1 : 0;
          return {
            rowspan: _row,
            colspan: _col
          };
        }
      } else if (
        columnIndex === 1 &&
        rowIndex === Number(isMerge)
      ) {
        // 对应第二列的第一行和第二行消失
        return {
          rowspan: 0,
          colspan: 0
        };
      }
    },

表头的合并处理


    headMerge({ row, column, rowIndex, columnIndex }) {
if (rowIndex === 1) {
        if (columnIndex === 0 || columnIndex === 1) {
          return { display: 'none' };
        }
      }
      if (rowIndex === 0) {
        if (columnIndex === 0) {
          this.$nextTick(() => {
            if (document.getElementsByClassName(column.id).length !== 0) {
              document
                .getElementsByClassName(column.id)[0]
                .setAttribute('rowSpan', 3);
              return false;
            }
          });
          return column;
        }
      }

在这里插入图片描述

表头斜线分隔

在这里插入图片描述

在这里插入图片描述


.el-table thead.is-group tr:first-of-type th:first-of-type {
      border-bottom: none;
    }
    .el-table thead.is-group tr:first-of-type th:first-of-type::before {
      position: absolute;
      top: 0;
      left: 0;
      display: block;
      width: 1px;
      height: 106px; /* 据td的宽度和高度 */
      content: '';
      background-color: #f2f2f2;
      // opacity: 0.3;
      transform: rotate(-66deg); /* 根据线的位置 */
      transform-origin: top;
    }
    .el-table thead.is-group tr:last-of-type th:first-of-type::before {
      position: absolute;
      right: 0;
      bottom: 0;
      display: block;
      width: 1px;
      height: 105px; /* 根据td的宽度和高度 */
      content: '';
      background-color: #f2f2f2;
      // opacity: 0.3;
      transform: rotate(-68deg); /* 根据线的位置 */
      transform-origin: bottom;
      // background:red;
    }
``
  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
elementui表格纵向合并动态表头可以通过设置`span-method`属性和编写`objectSpanMethod`方法来实现。具体步骤如下: 1.在表格组件中设置`span-method`属性为`objectSpanMethod`方法名。 2.编写`objectSpanMethod`方法,该方法接收两个参数:当前单元格的行数据和列数据。在该方法中,可以根据需要合并单元格并返回合并后的单元格信息。 下面是一个示例代码,演示如何在elementui表格中纵向合并动态表头: ```html <template> <el-table :data="tableData" :span-method="objectSpanMethod"> <el-table-column v-for="column in columns" :key="column.prop" :label="column.label"> </el-table-column> </el-table> </template> <script> export default { data() { return { columns: [ { label: '姓名', prop: 'name' }, { label: '年龄', prop: 'age' }, { label: '性别', prop: 'gender' }, { label: '国籍', prop: 'country' }, { label: '城市', prop: 'city' } ], tableData: [ { name: '张三', age: 18, gender: '男', country: '中国', city: '北京' }, { name: '李四', age: 20, gender: '女', country: '中国', city: '上海' }, { name: 'John', age: 22, gender: '男', country: '美国', city: '纽约' }, { name: 'Lucy', age: 25, gender: '女', country: '美国', city: '洛杉矶' } ] } }, methods: { objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0) { if (rowIndex % 2 === 0) { return { rowspan: 2, colspan: 1 } } else { return { rowspan: 0, colspan: 0 } } } } } } </script> ``` 在上面的示例代码中,我们设置了一个包含5个列的表格,并且在`objectSpanMethod`方法中,我们根据需要合并了第一列的单元格。具体来说,我们让第一列的奇数行单元格不显示,偶数行单元格合并为两行。这样就实现了纵向合并动态表头的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值