table合并列

import React, { useState, useEffect, useRef } from 'react';
import {
  Table, Button, Tooltip, Modal,
} from 'antd';
const Index = () => {
  const listData = [{
    skuId: '143621',
    skuName: '无线产品7',
    storeName: 'pb顺丰仓',
    options: [{
      tax_included: 29.75,
      tax_excluded: 26.33,
    }, {
      tax_included: 28.75,
      tax_excluded: 25.33,
    }, {
      tax_included: 27.75,
      tax_excluded: 24.33,
    }]
  }, {
    skuId: '143621',
    skuName: '无线产品7',
    storeName: 'pb来广营仓',
    options: [{
      tax_included: 29.75,
      tax_excluded: 26.33,
    }]
  }]
  const handleDataSource = (listData) => {
    let newArr = [];
    listData.forEach(listDataItem => {
      const len = listDataItem.options.length;
      listDataItem.options.forEach((optionsitem, index) => {
        newArr.push({
          ...listDataItem,
          ...optionsitem,
          rowSpan: index === 0 ? len : 0  // 各自的rowSpan计算出来放入对象
        })
      })
    })
    return newArr;
  };
  const columns = [
    {
      title: '序号',
      dataIndex: 'skuId',
      key: 'skuId',
      render: (text, record, index) => {
        return {
          children: text,
          props: {
            rowSpan: record.rowSpan
          }
        };
      }
    },
    {
      title: 'sku名称',
      dataIndex: 'skuName',
      key: 'skuName',
      render: (text, record, index) => {
        return {
          children: text,
          props: {
            rowSpan: record.rowSpan
          }
        };
      }
    },
    {
      title: '仓库名称',
      dataIndex: 'storeName',
      key: 'storeName',
      render: (text, record, index) => {
        return {
          children: text,
          props: {
            rowSpan: record.rowSpan
          }
        };
      }
    },
    {
      title: '含税',
      dataIndex: 'tax_included',
      key: 'tax_included',
    },
    {
      title: '不含税',
      dataIndex: 'tax_excluded',
      key: 'tax_excluded',
    },
  ]]
  return (
    <div className="order-list">
      <div className="table-box">
        <Table
          bordered
          rowKey="issueId"
          columns={columns}
          dataSource={handleDataSource(listData)}
        />
      </div>
    </div>
  );
};

export default Index;

 

antd table 合并可以通过设置表格的 column 属性中的 render 函数来实现。具体步骤如下: 1. 在 column 中设置需要合并的 dataIndex 和 render 函数。 2. 在 render 函数中,判断当前单元格是否需要合并,如果需要,则返回一个包含合并信息的对象,否则返回单元格的值。 3. 在表格的 rowSpan 和 colSpan 属性中设置需要合并的行数和数。 4. 如果需要在合并后的单元格中显示图片,可以在 render 函数中返回一个包含图片的 JSX 元素。 下面是一个示例代码,用于将表格中的第一和第二合并,并在合并后的单元格中显示图片: ``` import { Table } from 'antd'; const dataSource = [ { key: '1', name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', image: 'https://example.com/image1.png', }, { key: '2', name: 'Jim Green', age: 42, address: 'London No. 1 Lake Park', image: 'https://example.com/image2.png', }, { key: '3', name: 'Joe Black', age: 32, address: 'Sidney No. 1 Lake Park', image: 'https://example.com/image3.png', }, ]; const columns = [ { title: 'Name', dataIndex: 'name', key: 'name', render: (text, record, index) => { const obj = { children: text, props: {}, }; if (index === 0) { obj.props.rowSpan = 2; } if (index === 1) { obj.props.rowSpan = 0; } return obj; }, }, { title: 'Age', dataIndex: 'age', key: 'age', render: (text, record, index) => { const obj = { children: text, props: {}, }; if (index === 0) { obj.props.rowSpan = 2; } if (index === 1) { obj.props.rowSpan = 0; } return obj; }, }, { title: 'Address', dataIndex: 'address', key: 'address', }, { title: 'Image', dataIndex: 'image', key: 'image', render: (text, record, index) => { if (index === 0) { return <img src={text} alt="image" />; } return null; }, }, ]; const DemoTable = () => { return <Table dataSource={dataSource} columns={columns} />;}; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值