chobo2
chobo2

Reputation: 85865

Reactjs-tables. How to join multiple values into 1 cell?

I am using react-table but I have 2 values that I want to join into one cell. Does anyone know how to do this?

Say I have this sample code, right now it has "name" which as the first and last name combined.

What happens if in my db I have it separate by first name and last. How could I join them together in the ui here(I know I could do this at the db level but this just an example)

import ReactTable from 'react-table'

render() {
  const data = [{
    name: 'Tanner Linsley',
    age: 26,
    friend: {
      name: 'Jason Maurer',
      age: 23,
    }
  },{
    ...
  }]

  const columns = [{
    Header: 'Name',
    accessor: 'name' // String-based value accessors!
  }, {
    Header: 'Age',
    accessor: 'age',
    Cell: props => <span className='number'>{props.value}</span> // Custom cell components!
  }, {
    id: 'friendName', // Required because our accessor is not a string
    Header: 'Friend Name',
    accessor: d => d.friend.name // Custom value accessors!
  }, {
    Header: props => <span>Friend Age</span>, // Custom header components!
    accessor: 'friend.age'
  }]

  <ReactTable
    data={data}
    columns={columns}
  />
}

Upvotes: 3

Views: 12515

Answers (2)

Kim Stacks
Kim Stacks

Reputation: 10832

In case you want to sort by one of the values

{
    Header: "Price",
    accessor : "unit_price", // matters for grouping and sorting
    Cell     : props => <span>
      {props.original.currency} {Numeral(props.original.unit_price).format('0,0.00')}
    </span>
  },

Upvotes: 2

Jee Mok
Jee Mok

Reputation: 6566

would this work?

const columns = [
  {
    Header: 'Full Name',
    accessor: d => `${d.firstName} ${d.lastName}`
  }
]

Upvotes: 11

Related Questions