Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在reactjs中以卡片的形式显示循环数据_Javascript_Loops_Reactjs_Rendering - Fatal编程技术网

Javascript 如何在reactjs中以卡片的形式显示循环数据

Javascript 如何在reactjs中以卡片的形式显示循环数据,javascript,loops,reactjs,rendering,Javascript,Loops,Reactjs,Rendering,componentDidMount(){ var分量=此; 取('http://localhost:8080/UIServices/rest/dataService/getUserDetails?userName=SIVASO') .然后(功能(响应){ 返回response.json() }).then(函数(json){ var data=JSON.stringify(JSON); var卡=[]; 对于(变量i=0;i{ 返回response.json() })。然后((json)=>{

componentDidMount(){
var分量=此;
取('http://localhost:8080/UIServices/rest/dataService/getUserDetails?userName=SIVASO')
.然后(功能(响应){
返回response.json()
}).then(函数(json){
var data=JSON.stringify(JSON);
var卡=[];
对于(变量i=0;i
  • {data[i].dataConnectionType} {data[i].dataConnectionId} {data[i].description}

    ); } 组件设置状态({ 数据:json }) }) }
    在渲染中 返回{cards}

    我从具有多个连接的服务呼叫中获取数据,我需要卡中的每个连接。我需要循环一下

    但是我无法以卡片的形式填充数据。在我发布的{cards}回复中,有人能帮我吗
    显示未定义的卡片

    而不是在
    componentDidMount
    方法中循环数据,通过
    setState
    状态
    变量中设置响应数据,并从render方法调用一个方法,并在其中创建卡片ui

    所有ui创建部分应仅在
    render
    方法中

    我建议您使用而不是
    for loop

    这样写:

    componentDidMount() {
        fetch('http://localhost:8080/UIServices/rest/dataService/getUserDetails?userName=SIVASO')
          .then((response) => {
              return response.json()
          }).then((json) => {
              this.setState({data: json})
          })
    }
    
    
    
    _createCardsUI(){
        let cards = [], data = this.state.data;
        for (var i = 0; i < data.length; i++) {
          cards.push(<div className="col-md-3">
            <div className="panel panel-default datasource_panel">
                <div className="panel-heading">
                    <h5 className="panel_title"><i className="fa fa-database"></i> &nbsp; {data[i].dataConnectionName}</h5>
                    <div className="panel_actions">
                        <ul className=" list-unstyled">
                            <li className="dropdown">
                                <a className="dropdown-toggle" data-toggle="dropdown" data-target="#">
                                  <i className="fa fa-ellipsis-v"></i>
                                </a>
                                <ul className="dropdown-menu dropdown-menu-right">
                                  <li><a href="javascript:void(0)"><i className="fa fa-share-alt"></i> &nbsp; Share</a></li>
                                  <li><a href="javascript:void(0)"><i className="fa fa-pencil"></i> &nbsp; Edit</a></li>
                                  <li><a href="javascript:void(0)"><i className="fa fa-copy"></i> &nbsp; Duplicate</a></li>
                                  <li className="divider"></li>
                                  <li><a href="javascript:void(0)"> <i className=" fa fa-trash"></i> &nbsp; Delete</a></li>
                                </ul>
                            </li>
                        </ul>
                    </div>                          
                </div>
                <div className="panel-body">
                    <div className="datasource_txt text-center">
                      <h4>{data[i].dataConnectionType}</h4>
                      <h6>{data[i].dataConnectionSid}</h6>
                      <p>{data[i].description}</p>
                    </div>          
                </div> 
            </div></div>);
        }
        return cards;
    }
    
    
    render(){
       return (
          <div>
              {this._createCardsUI()}
          </div>
       )
    }
    
    componentDidMount(){
    取('http://localhost:8080/UIServices/rest/dataService/getUserDetails?userName=SIVASO')
    。然后((响应)=>{
    返回response.json()
    })。然后((json)=>{
    this.setState({data:json})
    })
    }
    _createCardsUI(){
    让卡片=[],数据=this.state.data;
    对于(变量i=0;i
    
  • {data[i].dataConnectionType} {data[i].dataConnectionId} {data[i].description}

    ); } 回程卡; } render(){ 返回( {这个。_createCardsUI()} ) }
    更新:

    使用映射而不是for循环:

    _createCardsUI(){
        var data = this.state.data;
        return data.map(el => (
            <div className="col-md-3">
                <div className="panel panel-default datasource_panel">
                    <div className="panel-heading">
                        <h5 className="panel_title"><i className="fa fa-database"></i> &nbsp; {el.dataConnectionName}</h5>
                        <div className="panel_actions">
                            <ul className=" list-unstyled">
                                <li className="dropdown">
                                    <a className="dropdown-toggle" data-toggle="dropdown" data-target="#">
                                      <i className="fa fa-ellipsis-v"></i>
                                    </a>
                                    <ul className="dropdown-menu dropdown-menu-right">
                                      <li><a href="javascript:void(0)"><i className="fa fa-share-alt"></i> &nbsp; Share</a></li>
                                      <li><a href="javascript:void(0)"><i className="fa fa-pencil"></i> &nbsp; Edit</a></li>
                                      <li><a href="javascript:void(0)"><i className="fa fa-copy"></i> &nbsp; Duplicate</a></li>
                                      <li className="divider"></li>
                                      <li><a href="javascript:void(0)"> <i className=" fa fa-trash"></i> &nbsp; Delete</a></li>
                                    </ul>
                                </li>
                            </ul>
                        </div>                          
                    </div>
                    <div className="panel-body">
                        <div className="datasource_txt text-center">
                          <h4>{el.dataConnectionType}</h4>
                          <h6>{el.dataConnectionSid}</h6>
                          <p>{el.description}</p>
                        </div>          
                    </div> 
                </div>
            </div>)
        );
    }
    
    \u createCardsUI(){
    var data=this.state.data;
    返回data.map(el=>(
    {el.dataConnectionName}
    
    {el.dataConnectionType} {el.dataConnectionSid} {el.description}

    ) ); }
    for(var i=0;i
    应该是
    for(var i=0;i
    这是使用
    .map

    render(){ 
      return (
        <div>
          {this.state.data.map((val, idx) => (
             <div className="col-md-3">
                 ... rest of card ...
             </div>
          ))} 
        </div>
      )
    }
    
    render(){
    返回(
    {this.state.data.map((val,idx)=>(
    …卡片的其余部分。。。
    ))} 
    )
    }
    
    如何在reactjs中显示卡中的数据

     this.state = {
          approvedCount:"",
          disapprovedCount:"",
          pendingCount:"",
          bigChartData: "data1",
    axios.get(`http://192.168.1.153:8080/vlock/poll/status`, headers, data)
          //  console.log("header")
    
          .then((res) => {
            // console.log("RESPONSE = ", res.data);
            this.setState
            console.log(res.message);
          });
    ({ pulls: res.data });
    
                  <Card  className="mb-4" style={{ color: "" }}>
                 
                    <CardBody>
                    
                      <br />
                      <center>
                        {" "}
                        <FaPoll style={{ color: "blue" }} />
                      </center>
                      <center>
                        {" "}
                        <CardTitle>Polls Approved</CardTitle>
                      </center>
                      <center>
                        {" "}
                 <p style={{ color: "blue" }}>{this.state.approvedCount}</p> 
    
                      </center>
                    </CardBody>
                  
                  </Card>
    
    this.state={
    批准数量:“,
    不批准计数:“”,
    挂起计数:“”,
    bigChartData:“数据1”,
    axios.get(`http://192.168.1.153:8080/vlock/poll/status`,标题,数据)
    //console.log(“头”)
    。然后((res)=>{
    //console.log(“RESPONSE=”,res.data);
    这是我的国家
    console.log(res.message);
    });
    ({lalls:res.data});
    
    {" "} {" "} 民调通过 {" "}

    {this.state.approvedCount}

     this.state = {
          approvedCount:"",
          disapprovedCount:"",
          pendingCount:"",
          bigChartData: "data1",
    axios.get(`http://192.168.1.153:8080/vlock/poll/status`, headers, data)
          //  console.log("header")
    
          .then((res) => {
            // console.log("RESPONSE = ", res.data);
            this.setState
            console.log(res.message);
          });
    ({ pulls: res.data });
    
                  <Card  className="mb-4" style={{ color: "" }}>
                 
                    <CardBody>
                    
                      <br />
                      <center>
                        {" "}
                        <FaPoll style={{ color: "blue" }} />
                      </center>
                      <center>
                        {" "}
                        <CardTitle>Polls Approved</CardTitle>
                      </center>
                      <center>
                        {" "}
                 <p style={{ color: "blue" }}>{this.state.approvedCount}</p> 
    
                      </center>
                    </CardBody>
                  
                  </Card>