Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display boolean and timestamp values inside react-table : React Table+ React+Typescript

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
                            },
                     ]}

    />
like image 985
joy08 Avatar asked May 20 '19 10:05

joy08


2 Answers

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();
          },
         ]}
like image 155
Himanshu Mishra Avatar answered Oct 21 '22 14:10

Himanshu Mishra


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>
)
like image 3
nacho Avatar answered Oct 21 '22 13:10

nacho