Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding multiple data to a column in react-table

I have a table using react-table but for one of the columns I want to show two pieces of data - name and description.

getInitialState(){
    return {
      data: [{
        id: 1,
        keyword: 'Example Keyword',
        product: [
          name: 'Red Shoe',
          description: 'This is a red shoe.'
        ]
      },{
        id: 2,
        keyword: 'Second Example Keyword',
        product: [
          name: 'blue shirt',
          description: 'This is a blue shirt.'
        ]
      }]
    }
},
render(){
  const { data } = this.state;

  return (
    <div className="app-body">
      <ReactTable
        data={data}
        columns={[{
          columns: [{
              Header: 'Id',
              accessor: id,
              show: false
            }, {
              Header: 'Keyword',
              accessor: 'keyword'
            }, {
              Header: 'Product',
              accessor: 'product'  // <<< here 
            }]
        }]}
      defaultPageSize={10}
      className="-highlight"
    />
    </div>
  )
}

Where the accessor is Product I want to show both the name and description (I'll style them to stack with different font sizes) in the Product column.

I've tried using the Cell: row => attribute for that column and thought I could also try calling a function that lays it out, but I've gotten errors both times.

Any ideas how to do this?

like image 306
user42010 Avatar asked Mar 29 '18 19:03

user42010


People also ask

How do I expand a table in react table?

Expand/ Collapse Table Rows Hierarchy using react-table We can easily implement the expand/collapse functionality in tables created using react-table components. The data collection is created in the form of a parent-child structure with a modified column definition. The useExpanded function is imported to use inside the column definition.

How do I sort a table in react table?

This will render the table with pagination: Sorting on Table Columns using react-table To enable sorting of table columns we’ll import the useSortByfunction and update the useTablefunction to pass it as an argument. On the table, the header adds Arrows to depict the sorted state of a column.

What are the features of react data table Ui?

Basic filtering and sorting options Advanced features in a React data table UI include: Custom sorting and filtering options for columns based on data types (numbers, string, boolean, select input, etc.) Pagination support or infinitely long tables (performance for very large datasets)

How do you render a table in react table V7?

As a headless utility, React Table v7 doesn’t render or supply data table UI elements out of the box. That means you’re responsible for rendering your own table markup using the state and callback of the hooks provided by React Table.


Video Answer


2 Answers

Indeed you should use Cell for this like this:

getInitialState(){
  return {
    data: [
      {
        id: 1,
        keyword: 'Example Keyword',
        product: [
          name: 'Red Shoe',
    description: 'This is a red shoe.'
]
},{
    id: 2,
      keyword: 'Second Example Keyword',
      product: [
      name: 'blue shirt',
      description: 'This is a blue shirt.'
  ]
  }]
}
},
render(){
  const { data } = this.state;

  return (
    <div className="app-body">
      <ReactTable
        data={data}
        columns={[{
          columns: [{
            Header: 'Id',
            accessor: id,
            show: false
          }, {
            Header: 'Keyword',
            accessor: 'keyword'
          }, {
            Header: 'Product',
            accessor: 'product',
            Cell: row => {
              return (
                <div>
                  <span className="class-for-name">{row.row.product.name}</span>
                  <span className="class-for-description">{row.row.product.description}</span>
                </div>
              )
            }
          }]
        }]}
        defaultPageSize={10}
        className="-highlight"
      />
    </div>

  )
}

Another thing I spotted was that product property should be an object not an array, so change this:

product: [
          name: 'blue shirt',
          description: 'This is a blue shirt.'
        ]

to this:

product: {
          name: 'blue shirt',
          description: 'This is a blue shirt.'
        }
like image 129
Shota Avatar answered Oct 30 '22 15:10

Shota


The accepted answer didn't work for me. Here's how I did it:

const [data, setData] = React.useState([
  {
    name: 'My item',
    desc: 'This is a nice item',
  },
]);

const columns = React.useMemo(() => [
  {
    Header: 'Name',
    accessor: 'name',

    Cell: (props) => (
      <>
        <p className="item title">{props.row.original.name}</p>
        <p className="item desc">{props.row.original.desc}</p>
      </>
    ),
  },
]);
like image 31
Soyal7 Avatar answered Oct 30 '22 13:10

Soyal7