2017-05-17 100 views
2

我是reactjs的新手,因为我想知道如何呈现表中的项目列表。在reactjs中迭代对象的数组

我的对象的样本阵列是:

[ 
{description:"test leave" 
end_date:"2017-05-16" 
id:1 
name:"new year" 
start_date:"2017-05-16"}, 

{description:"Diwali eve" 
end_date:"2017-10-22" 
id:2 
name:"diwali" 
start_date:"2017-10-22"} 
] 

我的代码是:

import React from 'react'; 
import { Router, Link , Route, Switch ,browserHistory } from 'react-router'; 
import {holidaydetails} from '../actions/index.jsx' 
import {holidaydetailreducer} from '../reducers/holidaydetails.jsx' 
import thunk from 'redux-thunk'; 
import ReduxPromise from 'redux-promise'; 
import {connect} from 'react-redux'; 
import {bindActionCreators} from 'redux'; 


class HolidaysList extends React.Component{ 
    constructor(props) { 
     super(props); 
     this.state = { 
      HolidayList: [] 
     }; 
    } 

    componentDidMount() 
    { 
      this.props.holidaydetails(); 
      this.setState({HolidayList:this.props.holidaydetails()}) 

    } 


    render(){ 
      const rows = this.props.HolidayList.holidaylist_data; 
      console.log(rows,'rows implementation!!!!!'); 

     return(
      <div className="container"> 
       <div className="row"> 
        <div className="margin"> 
         <div className="col-md-12"> 
          <div className="main_content"> 
           <div className="row"> 
            <div className="col-md-12"> 
             <div className="row"> 
              <div className="col-sm-12" data-offset="0"> 
               <div className="panel panel-info"> 
                <div className="panel-heading"> 
                 <div className="panel-title"> 
                  <strong>List of All Events</strong> 
                 </div> 
                </div> 
                <table className="table table-bordered table-hover" id="dataTables-example"> 
                 <thead> 
                  <tr> 
                   <th>SL.NO</th> 
                   <th className="col-sm-3">Event Name</th> 
                   <th className="col-sm-1">Start Date</th> 
                   <th className="col-sm-1">End Date</th> 
                   <th>Description</th> 
                   <th className="col-sm-1">Action</th> 
                  </tr> 
                 </thead> 
                 <tbody> 

                 </tbody> 
                </table> 
               </div> 
              </div> 
             </div> 
            </div> 
           </div> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 
     ); 
    } 
} 

function mapStateToProps(state,props) { 
    console.log(state,'state in holidaylist') 
    return { 
    HolidayList: state 
    } 
} 

function mapDispatchToProps(dispatch) { 
    return bindActionCreators({ 
    holidaydetails: holidaydetails}, dispatch); 
} 



export default connect(mapStateToProps, mapDispatchToProps)(HolidaysList); 

在上面的代码中,我有行对象包含在阵列中的所有对象列表。

在此先感谢,
任何帮助表示赞赏。

+0

的[环路内作出反应JSX(http://stackoverflow.com/questions/22876978/loop-inside-react-jsx) – Chris

+0

可能的复制@克里斯它不工作。谢谢你的帮助 – priya

回答

0

您需要映射在你的数据,如

<tbody> 

{rows && rows.map((holiday) => { 
    return <tr key={holiday.id}> 
       <td>{holiday.id}</td> 
       <td>{holiday.name}</td> 
       <td>{holiday.start_date}</td> 
       <td>{holiday.end_date}</td> 
       <td>{holiday.description}</td> 
       <td><button onClick={() => this.someAction(param)}>Action</button></td> 
      </tr> 
})} 
</tbody> 
+0

我越来越无法读取属性'地图'的未定义(... )同时试着用你的代码 – priya

+0

priya,你能告诉哪个数据变量,是否来自this.state.HolidayList,如果没有,你可以使用相应的数据变量 –

+0

我正在获取组件中this.props.HolidayList.holidaylist_data中的数据。因为我通过尝试const row = this.props.HolidayList.holidaylist_data得到了控制台组件中的内容 – priya

0

试试这个

render() { 

      return (
     <table> 
      <tbody>{this.state.todos.map((todo, i) => <Todo key={i} todos= {todo} />)}</tbody> 
     </table> 
    ); 
     } 

藤组件

类藤扩展React.Component {

render() { 

     return (
      <tr> 
      <td>{this.props.todos.id}</td> 
      <td>{this.props.todos.name}</td> 
      <td>{this.props.todos.company}</td> 
     </tr> 
    ); 
    } 
} 

出口默认待办事项;

+0

它不是一个待办事项,当你回答你应该修改你的答案根据OP需要或指向适当的链接 –