如何使用React更改所选表格行的背景颜色

6

我希望能够在点击表格行时更改其背景颜色,并在点击另一行时恢复背景颜色。

我尝试了以下代码:

index.js

state = {
    color: []    
  }



render(){    
 return (
        <Table>
          <thead>
            <tr>
              <th>name</th>
              <th>age</th>
              <th>address</th>
            </tr>
          </thead>
          <tbody className="tableHover">
            {this.props.students.map((item, i) => {
              return (
                <tr key={i} onClick={this.changeColor(i)}>
                  <td>{item.name}</td>
                  <td>{item.age}</td>
                  <td>{item.address}</td>
                </tr>
              );
            })}
          </tbody>
        </Table>
    );

    changeColor = (selectedRow) => e => {
      if (selectedRow){
       this.setState({color: 'blue'})
      }
    }
}

style.css

.tableHover :hover {
  color: white;
  background-color: blue;
}

预先感谢您!


当组件再次渲染时,将您在状态中设置的索引与映射函数的索引进行比较。如果它们相同,则添加一个样式对象或类,通过CSS添加颜色。 - mccambridge
2个回答

4
您可以在状态中维护一个selectedRow,并根据匹配索引为行添加类名。
className={this.state.selectedRow === i ? "tableSelected" : "" }

Full working code below

class App extends React.Component {
  state = {
    selectedRow: -1
  };

  render() {
    return (
      <table>
        <thead>
          <tr>
            <th>name</th>
            <th>age</th>
            <th>address</th>
          </tr>
        </thead>
        <tbody className="tableHover">
          {this.props.students.map((item, i) => {
            return (
              <tr key={i} onClick={this.changeColor(i)} className={this.state.selectedRow === i ? "tableSelected" : "" }>
                <td>{item.name}</td>
                <td>{item.age}</td>
                <td>{item.address}</td>
              </tr>
            );
          })}
        </tbody>
      </table>
    );
  }

  changeColor = selectedRow => e => {
    if (selectedRow !== undefined) {
      this.setState({ selectedRow  });
    }
  };
}

ReactDOM.render(<App students={[{name: "a"}, {name: "b"}]}/>, document.getElementById("app"));
.tableHover :hover {
  color: white;
  background-color: blue;
}

.tableSelected {
  background-color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>


1
您可以将索引设置为您的状态,如果索引等于所设置的值,则可以像下面这样添加您的颜色:
class YourComponent extends Component {
  state = {
    isActive: null
  };

  toggleActive = i => {
    //Remove the if statement if you don't want to unselect an already selected item
    if (i === this.state.isActive) {
      this.setState({
        isActive: null
      });
    } else {
      this.setState({
        isActive: i
      });
    }
  };

  render() {
    return (
      <Table>
        <thead>
          <tr>
            <th>name</th>
            <th>age</th>
            <th>address</th>
          </tr>
        </thead>
        <tbody className="tableHover">
          {this.props.students.map((item, i) => {
            return (
              <tr
                style={
                  this.state.isActive === i
                    ? { background: 'green' }
                    : { background: 'yellow' }
                }
                key={i}
                onClick={() => this.toggleActive(i)}
              >
                <td>{item.name}</td>
                <td>{item.age}</td>
                <td>{item.address}</td>
              </tr>
            );
          })}
        </tbody>
      </Table>
    );
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接