蚂蚁框架之动态合并行

在web开发中,表格的行合并是一个很常见的需求。蚂蚁框架也提供了这种功能,但是文档中的示例代码却和我想象中的相差甚远,如下所示,它是通过一种很笨的办法实现的行合并。这样当表格的数据源是动态获取的,我们就没有办法动态的合并行。

官方代码

蚂蚁框架的官方文档代码,我的代码见最后


import { Table } from 'antd';

// In the fifth row, other columns are merged into first column
// by setting it's colSpan to be 0
const renderContent = (value, row, index) => {
  const obj = {
    children: value,
    props: {},
  };
  if (index === 4) {
    obj.props.colSpan = 0;
  }
  return obj;
};

const columns = [{
  title: 'Name',
  dataIndex: 'name',
  render: (text, row, index) => {
    if (index < 4) {
      return <a href="javascript:;">{text}</a>;
    }
    return {
      children: <a href="javascript:;">{text}</a>,
      props: {
        colSpan: 5,
      },
    };
  },
}, {
  title: 'Age',
  dataIndex: 'age',
  render: renderContent,
}, {
  title: 'Home phone',
  colSpan: 2,
  dataIndex: 'tel',
  render: (value, row, index) => {
    const obj = {
      children: value,
      props: {},
    };
    if (index === 2) {
      obj.props.rowSpan = 2;
    }
    // These two are merged into above cell
    if (index === 3) {
      obj.props.rowSpan = 0;
    }
    if (index === 4) {
      obj.props.colSpan = 0;
    }
    return obj;
  },
}, {
  title: 'Phone',
  colSpan: 0,
  dataIndex: 'phone',
  render: renderContent,
}, {
  title: 'Address',
  dataIndex: 'address',
  render: renderContent,
}];

const data = [{
  key: '1',
  name: 'John Brown',
  age: 32,
  tel: '0571-22098909',
  phone: 18889898989,
  address: 'New York No. 1 Lake Park',
}, {
  key: '2',
  name: 'Jim Green',
  tel: '0571-22098333',
  phone: 18889898888,
  age: 42,
  address: 'London No. 1 Lake Park',
}, {
  key: '3',
  name: 'Joe Black',
  age: 32,
  tel: '0575-22098909',
  phone: 18900010002,
  address: 'Sidney No. 1 Lake Park',
}, {
  key: '4',
  name: 'Jim Red',
  age: 18,
  tel: '0575-22098909',
  phone: 18900010002,
  address: 'London No. 2 Lake Park',
}, {
  key: '5',
  name: 'Jake White',
  age: 18,
  tel: '0575-22098909',
  phone: 18900010002,
  address: 'Dublin No. 2 Lake Park',
}];

// 因为hexo 格式问题,暂时注释
<!-- ReactDOM.render(<Table columns={columns} dataSource={data} bordered />,mountNode); -->

动态合并行代码示例

因为在网上也没有找到合适的解决方案,所以把自己的实现分享出来,希望对大家有所帮助。

在线演示

表格数据中被合并的列应该是经过排序的,保证所有相同的行都呆在一起能够被合并


const { Table } = antd;

// In the fifth row, other columns are merged into first column
// by setting it's colSpan to be 0
const renderContent = (value, row, index) => {
  const obj = {
    children: value,
    props: {},
  };
  return obj;
};


const data = [{
  key: '1',
  name: 'A',
  age: 32,
  tel: '0571-22098909'
}, {
  key: '2',
  name: 'A',
  age:12,
  tel: '0571-22098333'
}, {
  key: '3',
  name: 'B',
  age: 32,
  tel: '0575-22098909'
}, {
  key: '4',
  name: 'B',
  age: 18,
  tel: '0575-22098909'
}, {
  key: '5',
  name: 'B',
  age: 18,
  tel: '0575-5'
}, {
  key: '6',
  name: 'C',
  tel: '0571-6',
  age:12
}, {
  key: '7',
  name: 'C',
  tel: '0571-7',
  age:19
}];

var myArray=new Array(data.length);

console.error(data.length);


var func =(data)=>{
  //保存上一个name
  var x = "";
  //相同name出现的次数
  var count = 0;
  //该name第一次出现的位置
  var startindex=0;

  for(var i = 0;i<data.length;i++){
    //这里是合并name列,根据各自情况大家可以自己完善
    var val = data[(i)].name;
    if(i==0){
      x=val;
      count=1;
      myArray[0]=1
    }else{
      if(val==x){
        count++;
        myArray[startindex]=count;
        myArray[i]=0
      }else{
        count = 1;
        x=val;
        startindex=i;
        myArray[i]=1
      }
    }
  }
}

func(data)
console.table(myArray)

const columns = [{
  title: 'Name',
  dataIndex: 'name',
  render: (value, row, index) => {
    const obj = {
      children: value,
      props: {},
    };
    obj.props.rowSpan = myArray[index];
    return obj
  },
}, {
  title: 'Age',
  dataIndex: 'age',
  render: renderContent,
}, {
  title: 'Home phone',
  dataIndex: 'tel',
  render: renderContent,
}];

// 因为hexo 格式问题,暂时注释 ReactDOM.render(...)
<!-- ReactDOM.render(<Table columns={columns} dataSource={data} bordered />,mountNode); -->