10

I am trying to print the boolean and timestamp values that are returned by some API. I am unable to do the same. Help would be appreciated

column config object:

    <ReactTable
                data={this.state.data}
                columns={[
                            { 
                              Header: 'Name',   
                              accessor: 'name'
                            },
                            { 
                              Header: 'Age>18',   
                              accessor: d => d.isAgeAbove18.toString(); // this does not work
                            },
                            { 
                              Header: 'TimeStamp',   
                              accessor: d => d.timeVal.toString(); // this does not work
                            },
                     ]}

    />
3
  • 1
    Will this work for you d => d.isAgeAbove18 ? "YES" : "NO" ? Commented May 20, 2019 at 10:48
  • @euvs No ! This one did not work Commented May 20, 2019 at 10:55
  • @euvs your suggestion did works, was just passing by and saw vr12 saying no. Btw i had id prop besides Header & accessor. Commented Nov 5, 2020 at 9:13

2 Answers 2

16

Looking at the React tables documentation, it says that you need to provide the id property whenever the accessor type is not a string. So, I the updated format should be this.

columns={[
          { 
           Header: 'Name',   
           accessor: 'name'
          },
          { 
           id:'age'                  // add this
           Header: 'Age>18',   
           accessor: d => d.isAgeAbove18.toString();
          },
          { 
           id: 'timestamp'           // add this
           Header: 'TimeStamp',   
           accessor: d => d.timeVal.toString();
          },
         ]}
Sign up to request clarification or add additional context in comments.

1 Comment

In order to avoid the "Property does not exist on type 'object'" error, you might need to explicitly cast the column object to "any", e.g. accessor: (d: any) => d.isAgeAbove18.toString(); See this React-Table issue and the related PR thread for discussions around this.
3

I think this is the best solution in case you are using filters,

id: 'available',
Header: "Available",
accessor: d => { return d.available ? 'Available' : 'Not available' },
filterable: true,
Filter: () => (
  <select className='form-control' value={this.state.availability_value} onChange={(e) => this.applyFilter(e.target.value)} >
    <option value={`{ "available": "" }`}>Select</option>
    <option value={`{ "available": "" }`}>All</option>
    <option value={`{ "available": "1" }`}>Available</option>
    <option value={`{ "available": "0" }`}>Not available</option>
  </select>
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.