Select row on click react-table

后端 未结 7 904
广开言路
广开言路 2020-12-08 06:29

I am trying to find the best table to use with my react apps, and for now, the react-table offers everything I need (pagination, server-side control, filtering, sorting, foo

7条回答
  •  一整个雨季
    2020-12-08 07:00

    I found the solution after a few tries, I hope this can help you. Add the following to your component:

    getTrProps={(state, rowInfo) => {
      if (rowInfo && rowInfo.row) {
        return {
          onClick: (e) => {
            this.setState({
              selected: rowInfo.index
            })
          },
          style: {
            background: rowInfo.index === this.state.selected ? '#00afec' : 'white',
            color: rowInfo.index === this.state.selected ? 'white' : 'black'
          }
        }
      }else{
        return {}
      }
    }
    

    In your state don't forget to add a null selected value, like:

    state = { selected: null }
    

提交回复
热议问题