切换数组中的特定div标签

时间:2019-01-16 09:37:25

标签: javascript reactjs github-api

我正在构建一个简单的ReactJS应用以获取GitHub用户并显示  有回购。我在数组中显示用户,每个用户都有详细信息  按钮,onClick仅显示特定用户的div标签。一世  能够通过点击传递用户名,但是onClick所有用户的div标签都是  切换。我想在单击详细信息按钮时显示回购。

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import _ from 'lodash';

class SearchResults extends Component {
  constructor(props) {
    super(props);
    this.state = { isOpen: false };
    this.toggleDetails = this.toggleDetails.bind(this);
  }

  toggleDetails(userName) {
    console.log(userName);

    this.setState({ isOpen: !this.state.isOpen });
  }

  render() {
    const { userList, userCount } = this.props;
    const {isOpen} = this.state;
    console.log('in searchResult = ' + this.props.userCount)

    if (userList) {
      var list = _.map(userList, (row, key) => {
        return (
          <div className="d-flex justify-content-center" key={row.id}
            style={{ border: '0px solid red' }}>
            <div className="card shadow-sm rounded" >
              <div className="card-body ">
                <div className="row ">
                  <div className="col-sm-2">
                    <img src={row.avatar_url} style={{ width: '80px', height: '80px' }}
                      className="rounded-circle" />
                  </div>
                  <div className="col-sm-10" style={{ border: '0px solid black' }}>
                    <h5 className="card-title">  {row.login}</h5>
                    <p className="display-6 text-muted"> {row.html_url} </p>
                    <button className="btn btn-outline-primary btn-sm float-right"
                      onClick={() => this.toggleDetails(row.login)}  >
                      Details
                    </button>
                  </div>
                </div>
              </div>
              {
                isOpen && 
                <table class="table table-striped" >
                  <tbody>
                    <tr>
                      <td>Otto</td>
                      <td>@mdo</td>
                    </tr>
                    <tr>
                      <td>Thornton</td>
                      <td>@fat</td>
                    </tr>
                    <tr>
                      <td>the Bird</td>
                      <td>@twitter</td>
                    </tr>
                  </tbody>
                </table>
              }
            </div>
          </div>

        )
      })
    } else {
      var list = (
        <div className="d-flex justify-content-center ">
          <div className="card shadow-sm rounded" >
            <div className="card-body ">
              <div className="row ">
                No results to show
                </div>
            </div>
          </div>
        </div>
      )
    }

    return (
      <div className="user-list">
        {userList ?
          <div className="d-flex justify-content-center">
            <div className="count">
              <h6 className="display-6">Total Results :  {userCount} </h6>
            </div>
          </div> :
          <div className="d-flex justify-content-center">
            <div className="count">
              <h6 className="display-6">Total Results :  {userCount} </h6>
            </div>
          </div>
        }
        {list}
      </div>
    )
  }
}

export default SearchResults;

1 个答案:

答案 0 :(得分:0)

isOpen状态由所有用户共享。因此,您可以尝试添加其他状态以保持当前打开的特定用户名。

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import _ from 'lodash';

class SearchResults extends Component {
  constructor(props) {
    super(props);
    this.state = { isOpen: false, currentUser : '' };
    this.toggleDetails = this.toggleDetails.bind(this);
  }

  toggleDetails(userName) {
    console.log(userName);
    const currentUser = this.state.currentUser == userName ? '' : userName;
    this.setState({ isOpen: !this.state.isOpen, currentUser:userName  });
  }

  render() {
    const { userList, userCount } = this.props;
    const {isOpen, currentUser} = this.state;
    console.log('in searchResult = ' + this.props.userCount)

    if (userList) {
      var list = _.map(userList, (row, key) => {
        return (
          <div className="d-flex justify-content-center" key={row.id}
            style={{ border: '0px solid red' }}>
            <div className="card shadow-sm rounded" >
              <div className="card-body ">
                <div className="row ">
                  <div className="col-sm-2">
                    <img src={row.avatar_url} style={{ width: '80px', height: '80px' }}
                      className="rounded-circle" />
                  </div>
                  <div className="col-sm-10" style={{ border: '0px solid black' }}>
                    <h5 className="card-title">  {row.login}</h5>
                    <p className="display-6 text-muted"> {row.html_url} </p>
                    <button className="btn btn-outline-primary btn-sm float-right"
                      onClick={() => this.toggleDetails(row.login)}  >
                      Details
                    </button>
                  </div>
                </div>
              </div>
              {
                isOpen && currentUser === row.login &&
                <table class="table table-striped" >
                  <tbody>
                    <tr>
                      <td>Otto</td>
                      <td>@mdo</td>
                    </tr>
                    <tr>
                      <td>Thornton</td>
                      <td>@fat</td>
                    </tr>
                    <tr>
                      <td>the Bird</td>
                      <td>@twitter</td>
                    </tr>
                  </tbody>
                </table>
              }
            </div>
          </div>

        )
      })
    } else {
      var list = (
        <div className="d-flex justify-content-center ">
          <div className="card shadow-sm rounded" >
            <div className="card-body ">
              <div className="row ">
                No results to show
                </div>
            </div>
          </div>
        </div>
      )
    }

    return (
      <div className="user-list">
        {userList ?
          <div className="d-flex justify-content-center">
            <div className="count">
              <h6 className="display-6">Total Results :  {userCount} </h6>
            </div>
          </div> :
          <div className="d-flex justify-content-center">
            <div className="count">
              <h6 className="display-6">Total Results :  {userCount} </h6>
            </div>
          </div>
        }
        {list}
      </div>
    )
  }
}

export default SearchResults;