简体   繁体   English

使用 map 函数在 react 中渲染表体

[英]Render a table body in react using the map function

I am new to the react.我是react.新手react. Here I have a data that is in the array.Now I want to render that table data.using the map function, what I tried is ,这里我有一个数组中的数据。现在我想呈现该表数据。使用 map 函数,我尝试的是,

    <tbody>
                { this.props.jobList.length > 0 &&  this.props.jobList.content.map(function (item, key) {
                  return (
                    <tr key={key}>
                      <td>1</td>
                      <td>2</td>
                      <td>3</td>
                      <td>4</td>
                      <td>5</td>
                      <td>6</td>
                      <td>7</td>
                    </tr>
                  )
                })}
</tbody>

{
    "content": [{
        "id": "5b7d4a566c5fd00507501051",
        "hrmsJdId": null,
        "companyId": null}]
}

Here, I do have data in the jobList but still it does not render that td content.在这里,我确实在 jobList 中有数据,但它仍然没有呈现该 td 内容。 Can any one explain me How can I do this?任何人都可以解释我我该怎么做? Or where I am doing wrong ?或者我哪里做错了?

You should map your array data ( this.props.jobList ) instead of object.您应该映射数组数据( this.props.jobList )而不是对象。 render should be like this: render应该是这样的:

<tbody>
    {this.props.jobList && this.props.jobList.content && this.props.jobList.content.length > 0 && this.props.jobList.content.map((item, key) => {
        return (
            <tr key={key}>
                <td>1</td>
                <td>2</td>
                <td>3</td>
                <td>4</td>
                <td>5</td>
                <td>6</td>
                <td>7</td>
            </tr>
        )
    })}
</tbody>

I think you should do like this:我认为你应该这样做:

var {jobList} = this.props;
var result = null;
if(jobList.length >0){
   result = jobList.map(value,key){
       return (
          <tr key={key}>
             <td>{value.content.id}</td>
             <td>2</td>
             <td>3</td>
             <td>4</td>
             <td>5</td>
             <td>6</td>
             <td>7</td>
          </tr>
       );
   }
}
return (
    <tbody>
        {result}
    </tbody>
);

Based on your comments and the data that you provided, it seems that you need to render contents within jobList for which you need to make use of nested map like根据您的评论和您提供的数据,您似乎需要在 jobList 中呈现需要使用嵌套地图的内容,例如

<tbody>
    {this.props.jobList && this.props.jobList.length > 0 && this.props.jobList.map((item, key) => {
        return (
          <React.Fragment>
             {item.content.map(job => (
                  <tr key={job.id}>
                     <td>id: {job.id}</td>
                     <td>company: {job.company}</td>
                   </tr>
             ))}
          </React.Fragment>
        )
    })}
</tbody>

Here is a solution that I tested inside a class based component.这是我在基于类的组件中测试的解决方案。

render()
{
    return(
        <tbody>
            {this.props.jobList && this.props.jobList.content && this.props.jobList.content.length > 0 && this.props.jobList.content.map((item, key) => {
                    return(
                    <tr key={key}>
                            <td>1</td>
                            <td>2</td>
                            <td>3</td>
                            <td>4</td>
                            <td>5</td>
                            <td>6</td>
                            <td>7</td>
                    </tr>
                )

            })}


        </tbody>    
    
    )

}

Looks interesting too, especially for people just starting in react.看起来也很有趣,特别是对于刚开始反应的人。 Makes the organized syntax a bit more comprehensive.使有组织的语法更加全面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM