el-table实现合并特定列的所有行

el-table实现合并特定列的所有行

示例:
在这里插入图片描述
在这里插入图片描述

const objectSpanMethod = ({ row, column, rowIndex, columnIndex }) => {
   if (columnIndex === 5 || columnIndex === 7) {
        // 就是只保留第一行,其他直接不要,然后行数是列表长度
        if (rowIndex === 0) {
          let rowspan = tableData.value.length
          return {
            rowspan,
            colspan: 1
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      }
};
const tableData = ref([
  {
    id: '12987122',
    shopName: 'xxxxxxxxxxx',
    sommoditySpecificate: '090909090909090909',
    shopPrice: '100.00',
    pruchaseCount: '100',
    purchaseContract: '2024年第一季度采购合同计划',
    totalPrice: '1000',
    attachement: 'img1.png'
  },
 {
    id: '12987122',
    shopName: 'xxxxxxxxxxx',
    sommoditySpecificate: '090909090909090909',
    shopPrice: '100.00',
    pruchaseCount: '100',
    purchaseContract: '2024年第一季度采购合同计划',
    totalPrice: '1000',
    attachement: 'img1.png'
  },
 {
    id: '12987122',
    shopName: 'xxxxxxxxxxx',
    sommoditySpecificate: '090909090909090909',
    shopPrice: '100.00',
    pruchaseCount: '100',
    purchaseContract: '',
    totalPrice: '1000',
    attachement: 'img1.png'
  },
  {
    id: '12987122',
    shopName: 'xxxxxxxxxxx',
    sommoditySpecificate: '090909090909090909',
    shopPrice: '100.00',
    pruchaseCount: '100',
    purchaseContract: '',
    totalPrice: '1000',
    attachement: 'img1.png'
  },
  {
    id: '12987122',
    shopName: 'xxxxxxxxxxx',
    sommoditySpecificate: '090909090909090909',
    shopPrice: '100.00',
    pruchaseCount: '100',
    purchaseContract: '',
    totalPrice: '1000',
    attachement: 'img1.png'
  },


])
<el-table
      :data="tableData"
      :span-method="objectSpanMethod"
      stripe
      class="isTop"
      :header-row-style="{height:'50px',backgroundColor: 'rgba(250,250,252,1)',color: '#0D0D2E'}"
    >
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="shopName" label="商品名称" />
      <el-table-column prop="sommoditySpecificate" label="商品规格" />
      <el-table-column prop="shopPrice" label="商品单价(元)">
        <template #default="scope">
          <div>
            ¥{{ scope.row.shopPrice ? parseFloat(scope.row.shopPrice).toFixed(2) : 'N/A' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="pruchaseCount" label="采购数量(个)" />
      <el-table-column prop="purchaseContract" label="采购合同" />
      <el-table-column label="总价">
        <template #default="scope">
          <span>¥{{(Number(scope.row.pruchaseCount) * Number(scope.row.shopPrice).toFixed(2))}}</span>
        </template>
      </el-table-column>
      <el-table-column label="总金额(元)">
        <template #default="scope">
          <span v-if="false">{{scope.row}}</span>
          <span class="priceColor">¥{{ calculateTotalPrice() }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="attachement" label="附件" v-if="isShopType == 1" />
      
      
    </el-table>
在这里插入代码片
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-tableElement UI框架中的一个表格组件,可以用于展示和编辑数据。要实现Excel中的合并合并功能,可以使用el-table特定属性和方法。 要实现合并,可以使用el-table的span-method属性。该属性接受一个函数,用于计算每个单元格的合并数。在函数中,可以根据当前单元格的值和相邻单元格的值来判断是否需要合并,并返回合并数。 要实现合并,可以使用el-table-column的prop属性。该属性指定了当前对应数据对象中的字段名。如果多个的prop属性相同,那么这些将会合并一列。 下面是一个示例代码,演示了如何使用el-table实现合并合并: ```html <template> <el-table :data="tableData" :span-method="handleSpanMethod"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="gender" label="性别"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18, gender: '男' }, { name: '李四', age: 20, gender: '女' }, { name: '王五', age: 18, gender: '男' }, { name: '赵六', age: 22, gender: '女' }, ], }; }, methods: { handleSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0) { // 判断当前是否为第一列 if (rowIndex === 0 || row.name !== this.tableData[rowIndex - 1].name) { // 判断当前是否为第一或者与上一的name字段值不同 const rowspan = this.getRowspan(row.name, columnIndex); if (rowspan > 1) { return { rowspan, colspan: 1, }; } } } }, getRowspan(name, columnIndex) { let rowspan = 1; for (let i = 1; i < this.tableData.length; i++) { if (this.tableData[i].name === name) { rowspan++; } else { break; } } return rowspan; }, }, }; </script> ``` 在上述代码中,handleSpanMethod函数用于计算每个单元格的合并数。getRowspan函数用于计算每个姓名相同的单元格的合并数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值