ant desgin table 合并表格 实现表格一行对多行的效果

首先看需求 使用antd 实现这样一个表格 合并单元格

原理:

  1. 翻阅资料 看官网效果


// 只看合并单元格的原理
const columns = [ 
  {
    title: 'Home phone',
    colSpan: 2,
    dataIndex: 'tel',
    render: (value, row, index) => {
      const obj = {
        children: value,
        props: {},
      };
      if (index === 2) {
        obj.props.rowSpan = 2;    // 当index等于2的时候 表示第3行,rowSpan等于2表示占据2行
      } 
      if (index === 3) {
        obj.props.rowSpan = 0; // 当index等于3的时候 表示第4行,rowSpan等于0表示不渲染,上面那行已把它合并了
      } 
      return obj;
    },
  } 
];
 // 比如我要合并三行 我第一行 就写3 下面两行不渲染就写0
 if (index === 0) {
    obj.props.rowSpan = 3;   
 } 
 if (index === 1) {
    obj.props.rowSpan = 0;  
 } 
 if (index === 2) {
    obj.props.rowSpan = 0;  
 } 
  1. 得出结论:



    看需求:虽然看起来只有3大行,其实这里需要有9条数据,然后日期与日期折扣合并了而已

  2. 看后端返回的数据
const responseData = [
      {
        dateDiscount: 1,
        endDate: "2019-01-02",
        startDate: "2019-01-01",
        timeData:[
          {
            discountPrice: 1,
            endTime: "21:21:21",
            startTime: "20:20:20",
            timeDiscount: 1,
          },
          {
            discountPrice: 2,
            endTime: "21:21:21",
            startTime: "20:20:20",
            timeDiscount: 2,
          },
          {
            discountPrice: 3,
            endTime: "21:21:21",
            startTime: "20:20:20",
            timeDiscount: 3,
          }
        ]
      },
      {
        dateDiscount: 1,
        endDate: "1111-11-11",      // 1111-11-11的时候 展示 其他日期
        startDate: "1111-11-11",
        timeData:[
          {
            discountPrice: 0.012,
            endTime: "00:00:00",
            startTime: "00:00:00",
            timeDiscount: 2,
          },
          {
            discountPrice: 0.013,
            endTime: "00:00:00",
            startTime: "00:00:00",
            timeDiscount: 3,
          }
        ]
      }
    ]
  1. 操作数据
// 两次循环 将2大条数据改成 5条数据 
// 然后把这5条数据各自的rowSpan计算出来放入对象
responseData.map(responseDataItem=>{
      const len = responseDataItem.timeData.length;
      responseDataItem.timeData.map((timeDataItem,index)=>{
        arr = [
          ...arr,
          {
           time:timeDataItem.startTime+'-'+timeDataItem.endTime,
            discountPrice: timeDataItem.discountPrice,
            timeDiscount: timeDataItem.timeDiscount,
            date:responseDataItem.startDate === '1111-11-11' ? '其他日期' : responseDataItem.startDate+'-'+responseDataItem.endDate,
            dateDiscount:responseDataItem.dateDiscount,
            span:index === 0 ? len : 0  // 各自的rowSpan计算出来放入对象
          }
        ]
        return arr 
      })
      return arr  
    })
    const tableData = arr.map((item, index) => {
      item.key = index;
      return item;
    })
  1. 得出数据格式:
const tableData = [
      {
        key:0,
        date:'1111',
        dateDiscount:'0.1',
        createTime:'1111-22222',
        timeDiscount:'0.222',
        discountPrice:'1111',
        span:2,    // 合并占据2行 
      },
      {
        key:1,
        date:'222',
        dateDiscount:'0.1',
        createTime:'55555-66666',
        timeDiscount:'0.3333',
        discountPrice:'2222',
        span:0,  // 被合并 不渲染
      },
      {
        key:2,
        date:'3333',
        dateDiscount:'0.1',
        createTime:'77777-88888',
        timeDiscount:'0.4444',
        discountPrice:'3333',
        span:4,     // 合并占据4行 
      },
      {
        key:3,
        date:'444444',
        dateDiscount:'0.1',
        createTime:'77777-88888',
        timeDiscount:'0.4444',
        discountPrice:'3333',
        span:0, // 被合并 不渲染
      },
      {
        key:4,
        date:'55555',
        dateDiscount:'0.1',
        createTime:'77777-88888',
        timeDiscount:'0.4444',
        discountPrice:'3333',
        span:0, // 被合并 不渲染
      },
      {
        key:5,
        date:'66666',
        dateDiscount:'0.1',
        createTime:'77777-88888',
        timeDiscount:'0.4444',
        discountPrice:'3333',
        span:0, // 被合并 不渲染
      }
    ] 


//最后 放入antd table 
setTableColumns([
      {
        title: '日期',
        dataIndex: 'date',
        key: 'date',
        width: 140,
        render: (value, row, index) => {    // 渲染tableData每一行的数据 设置各自的rowSpan 
          return {
            children: value,
            props: {rowSpan:row.span},
          };
        },
      },
      {
        title: '日期折扣',
        dataIndex: 'dateDiscount',
        key: 'dateDiscount',
        width: 140,
        render: (value, row) => {  
          return {
            children: value,
            props: {rowSpan:row.span},
          };
        },
      },
      {
        title: '时间',
        dataIndex: 'createTime',
        key: 'createTime',
        width: 140
      },
      {
        title: '时间折扣',
        dataIndex: 'timeDiscount',
        key: 'timeDiscount',
        width: 140
      },
      {
        title: '价格(元)',
        dataIndex: 'discountPrice',
        key: 'discountPrice',
        width: 140
      }
    ])

   <Table columns={tableColumns} dataSource={tableData} bordered />
  1. 最后效果


  2. 完整代码

import React, { useState, useEffect,useCallback } from 'react';
import './index.less';
import { Modal, Table } from 'antd'; 
import moment from 'moment';
import { config_discount_list } from '../../../services/api'; 

function DiscountsDetailModal(props) {
  const [tableData, setTableData] = useState([]);               // list数据
  const [visible,setVisible ] = useState(false);
  const [tableColumns,setTableColumns] = useState([]);
  const [filterParams, setFilterParams] = useState({});  // 查询表单的参数
  // const [updateList, setUpdateList] = useState([]); // 回显
  const [serveConfigId, setServeConfigId] = useState();  // 是否修改状态 
  // const format = 'HH:mm';
  // const dateFormat = 'YYYY/MM/DD';


  // 后端返回的数据
  // [
  //   {
  //     dateDiscount: 1,
  //     endDate: "2019-01-02",
  //     startDate: "2019-01-01",
  //     timeData:[
  //       {
  //         discountPrice: 1,
  //         endTime: "21:21:21",
  //         startTime: "20:20:20",
  //         timeDiscount: 1,
  //       },
  //       {
  //         discountPrice: 2,
  //         endTime: "21:21:21",
  //         startTime: "20:20:20",
  //         timeDiscount: 2,
  //       },
  //       {
  //         discountPrice: 3,
  //         endTime: "21:21:21",
  //         startTime: "20:20:20",
  //         timeDiscount: 3,
  //       }
  //     ]
  //   },
  //   {
  //     dateDiscount: 1,
  //     endDate: "1111-11-11",
  //     startDate: "1111-11-11",
  //     timeData:[
  //       {
  //         discountPrice: 0.012,
  //         endTime: "00:00:00",
  //         startTime: "00:00:00",
  //         timeDiscount: 2,
  //       },
  //       {
  //         discountPrice: 0.013,
  //         endTime: "00:00:00",
  //         startTime: "00:00:00",
  //         timeDiscount: 3,
  //       }
  //     ]
  //   }
  // ]


  
  // 我操作以后得出的数据
  //  [
  //     {
  //       key:0,
  //       date:'1111',
  //       dateDiscount:'0.1',
  //       createTime:'1111-22222',
  //       timeDiscount:'0.222',
  //       discountPrice:'1111',
  //       span:2,
  //     },
  //     {
  //       key:1,
  //       date:'222',
  //       dateDiscount:'0.1',
  //       createTime:'55555-66666',
  //       timeDiscount:'0.3333',
  //       discountPrice:'2222',
  //       span:0,
  //     },
  //     {
  //       key:2,
  //       date:'3333',
  //       dateDiscount:'0.1',
  //       createTime:'77777-88888',
  //       timeDiscount:'0.4444',
  //       discountPrice:'3333',
  //       span:4,
  //     },
  //     {
  //       key:3,
  //       date:'444444',
  //       dateDiscount:'0.1',
  //       createTime:'77777-88888',
  //       timeDiscount:'0.4444',
  //       discountPrice:'3333',
  //       span:0,
  //     },
  //     {
  //       key:4,
  //       date:'55555',
  //       dateDiscount:'0.1',
  //       createTime:'77777-88888',
  //       timeDiscount:'0.4444',
  //       discountPrice:'3333',
  //       span:0,
  //     },
  //     {
  //       key:5,
  //       date:'66666',
  //       dateDiscount:'0.1',
  //       createTime:'77777-88888',
  //       timeDiscount:'0.4444',
  //       discountPrice:'3333',
  //       span:0,
  //     }
  //   ]


  const requestList = useCallback(async () => { 
    let res = await config_discount_list({ serveConfigId});
    if (res.data.responseCode) return
    let responseData = res.data.responseData;
    setVisible(true)
    let arr = [];
    responseData.map(responseDataItem=>{
      const len = responseDataItem.timeData.length;
      responseDataItem.timeData.map((timeDataItem,index)=>{
        arr = [
          ...arr,
          {
            time:timeDataItem.startTime+'-'+timeDataItem.endTime,
            discountPrice: timeDataItem.discountPrice,
            timeDiscount: timeDataItem.timeDiscount,
            date:responseDataItem.startDate === '1111-11-11' ? '其他日期' : responseDataItem.startDate+'-'+responseDataItem.endDate,
            dateDiscount:responseDataItem.dateDiscount,
            span:index === 0 ? len : 0
          }
        ]
        return arr 
      })
      return arr  
    })
    const tableData = arr.map((item, index) => {
      item.key = index;
      return item;
    })
    setTableData(tableData);
  })

  useEffect(()=>{
    setVisible(props.visible);
    setServeConfigId(props.serveConfigId);
  }, [props.visible, props.serveConfigId])

  useEffect(()=>{
    if(serveConfigId && serveConfigId !== -1){
      requestList()
    }
  }, [serveConfigId])
  
  useEffect(()=>{
    setTableColumns([
      {
        title: '日期',
        dataIndex: 'date',
        key: 'date',
        width: 140,
        render: (value, row, index) => {
          return {
            children: value,
            props: {rowSpan:row.span},
          };
        },
      },
      {
        title: '日期折扣',
        dataIndex: 'dateDiscount',
        key: 'dateDiscount',
        width: 140,
        render: (value, row) => {  
          return {
            children: value,
            props: {rowSpan:row.span},
          };
        },
      },
      {
        title: '时间',
        dataIndex: 'createTime',
        key: 'createTime',
        width: 140
      },
      {
        title: '时间折扣',
        dataIndex: 'timeDiscount',
        key: 'timeDiscount',
        width: 140
      },
      {
        title: '价格(元)',
        dataIndex: 'discountPrice',
        key: 'discountPrice',
        width: 140
      }
    ])
  }, [])

  const onCancel = () => {
    setVisible(false)
    props.close(false)
  }

  return (
      <Modal
        destroyOnClose={true}
        className="discountsDetailModal"
        title="优惠详情"
        centered
        visible={ visible }
        onCancel={ onCancel }
        okText="确定"
        cancelText="取消"
        maskClosable={ false }
        width={600}
      >
         <Table columns={tableColumns} dataSource={tableData} bordered />
      </Modal>
  )
}

export default DiscountsDetailModal;

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,064评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,606评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,011评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,550评论 1 269
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,465评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,919评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,428评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,075评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,208评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,185评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,191评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,914评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,482评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,585评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,825评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,194评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,703评论 2 339

推荐阅读更多精彩内容

  • A day of specialized courses I lost sleep the last night,...
    油条一阅读 90评论 0 0
  • 我家有一只可爱的小狗。请您叫它小桃桃,每天给它洗澡喂食,还有散步和穿衣服,以前我非常害怕他,现在我不害怕他...
    芒果_8ef8阅读 223评论 0 0