Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the entries in one column clickable in React Table?

Im using React Table(http://react-table.js.org) to display a table in a page and populate it with data called from an API. I want to make the values displayed in one of the columns as links(a hrefs). This particular column contains only URLs. How to implement this in React Table?

    columns: [
            {
              filterable: false,
              Header: 'Click here',
              accessor: 'link',
              render: e => <a href={e.value}> {e.value} </a>,
            },
          ],

Im taking "e" as the data which is being displayed in the table and wrapping it in ahref to convert it as a link. However, this approach is not working.

like image 965
SeaWarrior404 Avatar asked Sep 06 '17 11:09

SeaWarrior404


1 Answers

From the docs; You can set custom components for cells.

Example:

<ReactTable
          data={data}
          columns={[{
      Header: 'Name',
      columns: [{
        Header: 'First Name',
        accessor: 'firstName'
      }, {
        Header: 'Last Name',
        id: 'lastName',
        accessor: d => d.lastName
      }]
    }, {
      Header: 'Info',
      columns: [{
        Header: 'Profile Progress',
        accessor: 'progress',
        Cell: row => (
          <div
            style={{
              width: '100%',
              height: '100%',
              backgroundColor: '#dadada',
              borderRadius: '2px'
            }}
          >
            <div
              style={{
                width: `${row.value}%`,
                height: '100%',
                backgroundColor: row.value > 66 ? '#85cc00'
                  : row.value > 33 ? '#ffbf00'
                  : '#ff2e00',
                borderRadius: '2px',
                transition: 'all .2s ease-out'
              }}
            />
          </div>
        )
      }, {
        Header: 'Status',
        accessor: 'status',
        Cell: row => (
          <span>
            <span style={{
              color: row.value === 'relationship' ? '#ff2e00'
                : row.value === 'complicated' ? '#ffbf00'
                : '#57d500',
              transition: 'all .3s ease'
            }}>
              &#x25cf;
            </span> {
              row.value === 'relationship' ? 'In a relationship'
              : row.value === 'complicated' ? `It's complicated`
              : 'Single'
            }
          </span>
        )
      }]
    }]}
          defaultPageSize={10}
          className="-striped -highlight"
        />
like image 187
bennygenel Avatar answered Sep 20 '22 16:09

bennygenel