el-tabel合并指定行、列单元格数据;合并列后不见了一列数据问题

部署运行你感兴趣的模型镜像
 <el-table :span-method="cellMerge" :key="tableKey" v-loading="tyyyLoading" :data="tyyyData" border :header-cell-style="rowClass" :cell-style="cellStyle">
          <el-table-column v-for="(item, index) in tyyyTableList" :key="index" align="center" :label="item.label">
            <template slot-scope="scope">
              <span :title="scope.row[`${item.prop}`]" style="overflow: hidden; white-space: nowrap; text-overflow: ellipsis">{{ scope.row[item.prop] }}</span>
            </template>
          </el-table-column>
        </el-table>

2、写法

   // 获取相同名称的个数 tableData: 表格的数据, projectName: 确定相同的参数
    cellMerge({ row, column, rowIndex, columnIndex }) {
      if ( (rowIndex === 0 || rowIndex == 5) && columnIndex === 0) {
        return [1, 2];
      } else if ((rowIndex === 0 || rowIndex == 5) && columnIndex === 1) {
      return [0,2]; // 再把前面合并了的那一列删了,这样错位的再错回来,往前一列又对了
    } else if (columnIndex === 0) {
        const _row = this.spanArr[rowIndex]// 处理除了第一行和第五行外的单元格和合并,但是也只是合并第一列的产品类型相同的数据
        const _col = 1
        return {
          rowspan: _row,
          colspan: _col,
        }
    } else {
      return [1, 1];//其他单元格都独立显示
    }
    },
    // 单元格的处理方法 当前行row、当前列column、当前行号rowIndex、当前列号columnIndex
    getSpanArr(data) {
    for (let i = 0; i < data.length; i++) {
      if (i === 0) {
        this.spanArr = [];
        this.spanArr.push(1);
        this.pos = 0;
      } else {
        if (data[i].productTypeDesc === data[i - 1].productTypeDesc) {
          this.spanArr[this.pos] += 1;
          this.spanArr.push(0);
        } else {
          this.spanArr.push(1);
          this.pos = i;
        }
      }
    }
    return this.spanArr; // 返回合并信息数组
  },

3 、最终效果

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

要在 Element UI 的 `el-table` 中合并相同数据单元格,主要依赖于 `span-method` 属性。该属性允许开发者自定义单元格的跨或跨显示逻辑。以下是详细的解决方案: ### 实现步骤 #### 1. 准备数据 假设我们有一组如下所示的数据: ```javascript tableData: [ { id: '1', name: '王小虎', amount1: '165', amount2: '3.2', amount3: 10 }, { id: '1', name: '王小虎', amount1: '162', amount2: '4.43', amount3: 12 }, { id: '1', name: '王we虎', amount1: '621', amount2: '1.9', amount3: 9 }, { id: '2', name: '王we虎', amount1: '621', amount2: '2.2', amount3: 17 }, { id: '3', name: '王小虎', amount1: '621', amount2: '4.1', amount3: 15 } ], ``` 为了实现合并功能,我们需要遍历数据计算每一列中连续相同的值的数量。 --- #### 2. 计算合并规则 创建一个方法用于生成合并所需的跨度数组。以下是一个示例函数: ```javascript methods: { getSpanArr(data) { let spanArr = []; data.forEach((item, index) => { if (index === 0) { spanArr.push(1); } else { if (data[index].id === data[index - 1].id && item.name === data[index - 1].name) { spanArr[spanArr.length - 1]++; spanArr.push(0); } else { spanArr.push(1); } } }); return spanArr; } } ``` 在此基础上,我们可以扩展到其他字段(如 `amount1`, `amount2` 等)。对于每都需要类似的处理逻辑。 --- #### 3. 定义 `spanMethod` 在 `<el-table>` 组件中绑定 `span-method` 属性,传入一个函数来控制单元格合并为。 ```html <el-table :data="tableData" border style="width: 100%" :span-method="objectSpanMethod"> </el-table> ``` 对应的 JavaScript 方法如下: ```javascript computed: { objectSpanMethod() { const spanArrId = this.getSpanArr(this.tableData.map(item => ({ id: item.id, name: item.name }))); const spanArrAmount1 = this.getSpanArr(this.tableData.map(item => item.amount1)); return function({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0 || columnIndex === 1) { // 假设第0和第1为需要合并的部分 const _row = spanArrId[rowIndex]; const _col = _row > 0 ? 1 : 0; return { rowspan: _row, colspan: _col }; } else if (columnIndex === 2) { // 假设第2为另一个需要合并的部分 const _row = spanArrAmount1[rowIndex]; const _col = _row > 0 ? 1 : 0; return { rowspan: _row, colspan: _col }; } }; } }, ``` 在这里,`getSpanArr` 被多次调用以分别针对不同的生成合并规则。最终通过 `objectSpanMethod` 返回具体的跨越配置。 --- #### 4. 渲染表格 按照常规方式渲染表头即可。例如: ```html <el-table-column prop="id" label="ID" width="80"></el-table-column> <el-table-column prop="name" label="姓名" width="120"></el-table-column> <el-table-column prop="amount1" label="金额1" width="120"></el-table-column> <el-table-column prop="amount2" label="金额2" width="120"></el-table-column> <el-table-column prop="amount3" label="金额3" width="120"></el-table-column> ``` --- ### 示例代码总结 完整的 Vue 部分代码如下: ```javascript export default { data() { return { tableData: [ { id: '1', name: '王小虎', amount1: '165', amount2: '3.2', amount3: 10 }, { id: '1', name: '王小虎', amount1: '162', amount2: '4.43', amount3: 12 }, { id: '1', name: '王we虎', amount1: '621', amount2: '1.9', amount3: 9 }, { id: '2', name: '王we虎', amount1: '621', amount2: '2.2', amount3: 17 }, { id: '3', name: '王小虎', amount1: '621', amount2: '4.1', amount3: 15 } ] }; }, methods: { getSpanArr(data) { let spanArr = []; data.forEach((item, index) => { if (index === 0) { spanArr.push(1); } else { if (JSON.stringify(data[index]) === JSON.stringify(data[index - 1])) { spanArr[spanArr.length - 1]++; spanArr.push(0); } else { spanArr.push(1); } } }); return spanArr; } }, computed: { objectSpanMethod() { const spanArrIdName = this.getSpanArr( this.tableData.map(item => ({ id: item.id, name: item.name })) ); const spanArrAmount1 = this.getSpanArr(this.tableData.map(item => item.amount1)); return function({ row, column, rowIndex, columnIndex }) { if ([0, 1].includes(columnIndex)) { const _row = spanArrIdName[rowIndex]; const _col = _row > 0 ? 1 : 0; return { rowspan: _row, colspan: _col }; } else if (columnIndex === 2) { const _row = spanArrAmount1[rowIndex]; const _col = _row > 0 ? 1 : 0; return { rowspan: _row, colspan: _col }; } }; } } }; ``` --- ### 注意事项 - 数据需按一定顺序排才能正确合并。如果原始数据无序,则应先对其进排序。 - 当涉及多个字段时,可能需要额外的逻辑来区分哪些字段应该参与合并[^1]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值