首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >切换数组中的特定div标记

切换数组中的特定div标记
EN

Stack Overflow用户
提问于 2019-01-16 17:37:25
回答 1查看 30关注 0票数 0

我正在建立一个简单的ReactJS应用程序,以获取GitHub用户和显示有回购。我在一个数组中显示用户,每个用户都有一个详细信息按钮,onClick只显示必须显示的特定用户div标记。我可以通过点击用户名,但onClick所有用户的div标签都在切换。我想在点击details按钮时显示repos。

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-16 17:57:11

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

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54214077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档