elementUI table合并列处理函数

elementUI中的合并列处理函数
做个笔记

处理函数
delArraySpanMethod(data){
//创建合并规则
this.spanZbArr= [];
this.spanZmArr = [];
//一个for循环就是一个合并的规则
  for (let i = 0; i < data.length; i++) {
    if (i === 0) {
      this.spanZbArr.push(1);
        this.index = 0;
    } else {
      //在if写你要合并的列的规则
      if (data[i].zbChannelName === data[i - 1].zbChannelName && data[i].engName === data[i - 1].engName) {
        this.spanZbArr[this.index] += 1;
        this.spanZbArr.push(0);
      } else {
        // 不相等push 1
       this.spanZbArr.push(1);
       this.index = i;
      }
    }
  }
  for (let i = 0; i < data.length; i++) {
    if (i === 0) {
      this.spanZmArr.push(1);
      this.index = 0;
    } else {
    //如果别的列跟上面的规则不同就多加一个
        if (data[i].engName === data[i - 1].engName) {
          this.spanZmArr[this.index] += 1;
          this.spanZmArr.push(0);
        } else {
          // 不相等push 1
          this.spanZmArr.push(1);
          this.index = i;
        }
    }
  }
}
table合并函数
// table合并行 row当前行  column当前列  rowIndex当前行号  columnIndex当前列号
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
    // 指定行使用刚刚创建的合并规则
      if (columnIndex === 0 || columnIndex === 1 || columnIndex === 2 || columnIndex === 3 || columnIndex === 4 || columnIndex === 13) {
        for (let i = 0; i < this.spanZmArr.length; i++) {
          if (rowIndex === i) {
            return {
              rowspan: this.spanZmArr[i],
              colspan: 1
            };
          }
        }
      } else if (columnIndex === 5 || columnIndex === 6) {
        for (let i = 0; i < this.spanZbArr.length; i++) {
          if (rowIndex === i) {
            return {
              rowspan: this.spanZbArr[i],
              colspan: 1
            };
          }
        }
      }
    },
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ElementUI Table合并相同部门的单元格可以通过自定义 Table 的 slot-scope 和 template 实现。具体操作如下: 1. 通过设置 `rowspan` 属性来合并单元格。在模板中定义一个变量 `currentDepartment` 来保存当前的部门名称。 ```html <template slot-scope="{ row, $index }"> <td v-if="$index === 0 || row.department !== currentDepartment" :rowspan="getDepartmentRowCount(row)"> {{ row.department }} </td> <td>{{ row.name }}</td> <td>{{ row.age }}</td> </template> ``` 2. 在 methods 中定义一个函数 `getDepartmentRowCount(row)` 来获取当前部门的数,用于设置 `rowspan` 属性。 ```javascript methods: { getDepartmentRowCount(row) { let count = 1; if (row.department === this.currentDepartment) { count = 0; } else { this.currentDepartment = row.department; } for (let i = this.tableData.indexOf(row) + 1; i < this.tableData.length; i++) { if (this.tableData[i].department === row.department) { count++; } else { break; } } return count; } } ``` 完整代码示例如下: ```html <template> <el-table :data="tableData" style="width: 100%"> <el-table-column label="姓名" prop="name"></el-table-column> <el-table-column label="年龄" prop="age"></el-table-column> <el-table-column label="部门"> <template slot-scope="{ row, $index }"> <td v-if="$index === 0 || row.department !== currentDepartment" :rowspan="getDepartmentRowCount(row)"> {{ row.department }} </td> <td>{{ row.name }}</td> <td>{{ row.age }}</td> </template> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18, department: '技术部' }, { name: '李四', age: 20, department: '技术部' }, { name: '王五', age: 22, department: '市场部' }, { name: '赵六', age: 24, department: '市场部' } ], currentDepartment: '' }; }, methods: { getDepartmentRowCount(row) { let count = 1; if (row.department === this.currentDepartment) { count = 0; } else { this.currentDepartment = row.department; } for (let i = this.tableData.indexOf(row) + 1; i < this.tableData.length; i++) { if (this.tableData[i].department === row.department) { count++; } else { break; } } return count; } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值