Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dataIndex from ant table accepting 2 incoming data

Tags:

reactjs

antd

I wanted my first columns title name to accept 2 data from my graphql query. "rocket.rocket_name" and "links.wikipedia" to be displayed on the render href. but it seems like dataIndex only accept one string.

const { Table, Divider, Tag } = antd;

const columns = [{
  title: 'Name',
  dataIndex: 'rocket.rocket_name',
  key: 'name',
  render: text => <a href="www.google.com">{text}</a>,
}, {
  title: 'Description',
  dataIndex: 'details',
  key: 'age',
}];

const data =  [
      {
        "id": "77",
        "rocket": {
          "rocket_name": "Falcon Heavy",
          "rocket": {
            "active": true
          }
        },
        "links": {
          "wikipedia": "https://en.wikipedia.org/wiki/Arabsat-6A"
        },
        "details": "SpaceX will launch Arabsat 6A to a geostationary transfer orbit from SLC-39A, KSC. The satellite is a geostationary telecommunications satellite built by Lockheed Martin for the Saudi Arabian company Arabsat. This will be the first operational flight of Falcon Heavy, and also the first Block 5 Falcon Heavy. All three cores will be new Block 5 cores. The side cores are expected to land at LZ-1 and LZ-2, and the center core is expected to land on OCISLY."
      }
    ]
ReactDOM.render(<Table columns={columns} dataSource={data} />, mountNode);
like image 441
Claude Avatar asked Dec 07 '22 12:12

Claude


2 Answers

If you want to create a cell using combined values of 2 columns, use second argument of column's render function.

Excerpt from antd documentation:

Function(text, record, index) {}

Here, text is a value of column specified by dataIndex, and record will be the object with the row (values of all columns).

const columns = [
  {
    title: 'Name',
    // dataIndex: 'rocket.rocket_name', // antd v3
    dataIndex: ['rocket', 'rocket_name'], // antd v4
    key: 'name',
    render: (text, record) => <a href={record.links.wikipedia}>{text}</a>,
  },
  {
    title: 'Description',
    dataIndex: 'details',
    key: 'age',
  }
];
like image 95
maktel Avatar answered Dec 11 '22 08:12

maktel


you can try this

    {
        title: 'İd',
        dataIndex: ['id',"symbol"],
        key: 'id',
        render: (text,row) => <a title={row["symbol"]}>{row["id"]}</a>,
    },
like image 31
Ali Bulut Avatar answered Dec 11 '22 09:12

Ali Bulut