Vue中HTML的table标签表格合并

<div id="app">
    <!-- 
      需求: 规格商品: 同一种商品可能有不同规格,需要将规格商品的'运费'和'名称'列在行方向上合并
     -->
   <table border="1" cellpadding="0" cellspacing="0">
     <thead>
       <tr>
        <th>商品名称</th>
        <th>商品编号</th>
        <th>商品规格</th>
        <th>商品单价</th>
        <th>商品数量</th>
        <th>运费</th>
      </tr>
     </thead>
     <tbody>
       <tr v-for="(item,index) in productList" :key="index">
         <td v-if="spanArr[index]" :rowspan="spanArr[index]" :colspan="1">{{item.name}}</td>
         <td>{{item.code}}</td>
         <td>{{item.attrs}}</td>
         <td>{{item.price}}</td>
         <td>{{item.num}}</td>
         <td v-if="spanArr[index]" :rowspan="spanArr[index]" :colspan="1">{{item.freight}}</td>
       </tr>
     </tbody>
   </table>
  </div>

<script>
    let vue = new Vue({
      el:'#app',
      data() {
        return {
          productList:[],
          spanArr: [],  //记录当前单元格向下合并的行数,为0的话说明该单元格在行方向上已被合并不用显示
          pos: 0  // 记录当前单元格所处的行下标
        }
      },
      methods: {
        // 初始化商品数据
        init() {
          this.productList = [
          {
              id: '0001',
              code: 'X10001',
              name:'葡萄',
              attrs:'红色;500g',
              price: 50,
              num: 1,
              freight: 15
            },
            {
              id: '0001',
              code: 'X10002',
              name:'葡萄',
              attrs:'绿色;500g',
              price: 30,
              num: 1,
              freight: 15
            },
            {
              id: '0001',
              code: 'X10003',
              name:'葡萄',
              attrs:'白色;500g',
              price: 30,
              num: 1,
              freight: 15
            },
            {
              id: '0002',
              code: 'X20001',
              name:'芒果',
              attrs:'黄色;500g',
              price: 50,
              num: 1,
              freight: 15
            }
          ]
          this.getRows()
        },
        // 初始化需要合并的数据数组,计算合并后需要展示的行数
        // 以数组中每条数据的商品id来判断,如果商品id相同说明是同一种商品的不同规格,则需要合并
        getRows() {
          let list = this.productList;
          list.forEach((ele,i)=>{
            if(i===0) {
              // 第一行,一定展示不需要合并
              this.spanArr.push(1)
              this.pos = 0;
            }else {
              // 非第一行
              if(ele.id === list[i-1].id) {
                // 当前行的id和上一行的id相同了,则当前行需要和上一行的进行合并,当前行的该单元格不显示
                this.spanArr[this.pos] +=1        //需要向下合并的行数加1
                this.spanArr.push(0)
              }else {
                this.spanArr.push(1)
                this.pos = i
              }
            }
          })
          console.log('合并的数据',this.spanArr);
        }
      },
      created() {
        this.init()
      },
    })
</script> 

合并后的表格效果如下图所示:

Ant Design Vue的a-table组件默认不支持单元格合并,但是可以通过自定义渲染函数来实现单元格合并。以下是一个简单的示例代码: ```html <template> <a-table :columns="columns" :data-source="dataSource"></a-table> </template> <script> export default { data() { return { columns: [ { title: "姓名", dataIndex: "name", customRender: (text, row, index) => { const rowspan = row.age < 30 ? 2 : 1; // 根据条件设定行合并数 if (index % rowspan === 0) { return { children: text, attrs: { rowSpan: rowspan } }; } else { return { children: "", attrs: { rowSpan: 0 } }; } } }, { title: "年龄", dataIndex: "age" }, { title: "地址", dataIndex: "address" } ], dataSource: [ { name: "张三", age: 28, address: "北京市海淀区" }, { name: "李四", age: 32, address: "上海市浦东新区" }, { name: "王五", age: 25, address: "广州市天河区" }, { name: "赵六", age: 31, address: "深圳市福田区" } ] }; } }; </script> ``` 在上面的代码,我们通过自定义渲染函数 `customRender` 来实现单元格的合并。在渲染姓名这一列时,根据条件设定行合并数,然后判断当前行是否是合并行的第一行,如果是就返回一个包含 `children` 和 `attrs` 属性的对象,其 `children` 属性设置单元格显示的文本,`attrs` 属性设置单元格的 `rowspan` 属性。如果不是合并行的第一行,就返回一个空字符串和 `rowspan` 为 0 的 `attrs` 属性,表示该单元格不需要显示。 这样就能实现 Ant Design Vue 的 a-table 表格单元格合并了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wen_文文

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值