如何在Reactjs中映射表格行?

huangapple go评论67阅读模式
英文:

How to map table rows in reactjs?

问题

在我尝试映射ID标题下的id时,它是水平映射的,但我希望垂直映射。当我尝试将名称映射到名称标题下的value.user_name时,相同的情况发生 - 它水平映射,但我想要垂直映射它们。

function App() {
  let response = [
    {
      id: 134,
      user_name: 'abc',
      phone_number: 1234567890,
      children: [
        { id: 659, user_name: 'ghi', phone_number: 9834763467 },
        { id: 659, user_name: 'ghi', phone_number: 9834763467 },
      ],
    },
    {
      id: 948,
      user_name: 'def',
      phone_number: 9823437483,
      children: [
        { id: 659, user_name: 'ghi', phone_number: 9834763467 },
        { id: 659, user_name: 'ghi', phone_number: 9834763467 },
      ],
    },
    {
      id: 865,
      user_name: 'abc',
      phone_number: 1234567890,
      children: [
        { id: 659, user_name: 'ghi', phone_number: 9834763467 },
        { id: 659, user_name: 'ghi', phone_number: 9834763467 },
      ],
    },
  ];

  console.log('response', response);
  return (
    <div className="App">
      <table>
        <thead>
          <tr>
            <th>Id</th>
            <th>Name</th>
            <th>phone number</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            {response.map((value, index) => {
              return [<td>{value.id}</td>, <td>{value.user_name}</td>];
            })}
          </tr>
        </tbody>
      </table>
    </div>
  );
}

export default App;
英文:

When I am trying to map ids below ID header it is mapping horizontally but I wanted to map in vertically. The same thing happens with value.user_name when I am trying to map names below Name header — it maps horizontally but I want to map them vertically.

function App() {
let response = [
{
id: 134,
user_name: &#39;abc&#39;,
phone_number: 1234567890,
children: [
{ id: 659, user_name: &#39;ghi&#39;, phone_number: 9834763467 },
{ id: 659, user_name: &#39;ghi&#39;, phone_number: 9834763467 },
],
},
{
id: 948,
user_name: &#39;def&#39;,
phone_number: 9823437483,
children: [
{ id: 659, user_name: &#39;ghi&#39;, phone_number: 9834763467 },
{ id: 659, user_name: &#39;ghi&#39;, phone_number: 9834763467 },
],
},
{
id: 865,
user_name: &#39;abc&#39;,
phone_number: 1234567890,
children: [
{ id: 659, user_name: &#39;ghi&#39;, phone_number: 9834763467 },
{ id: 659, user_name: &#39;ghi&#39;, phone_number: 9834763467 },
],
},
];
console.log(&#39;response&#39;, response);
return (
&lt;div className=&quot;App&quot;&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Id&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;phone number&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
{response.map((value, index) =&gt; {
return [&lt;td&gt;{value.id}&lt;/td&gt;, &lt;td&gt;{value.user_name}&lt;/td&gt;];
})}
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
);
}
export default App;

答案1

得分: 3

你需要在map函数内部包含&lt;tr&gt;,就像这样:

&lt;tbody&gt;
{response.map((value) =&gt; {
return (
&lt;tr key={value.id}&gt;
&lt;td&gt;{value.id}&lt;/td&gt;
&lt;td&gt;{value.user_name}&lt;/td&gt;
&lt;td&gt;{value.phone_number}&lt;/td&gt;
&lt;/tr&gt;
);
})}
&lt;/tbody&gt;

现在的情况是,你的表格中只有一个tr,这导致所有的td元素都在同一行中渲染。

英文:

You need to include &lt;tr&gt; inside the map function like this:

&lt;tbody&gt;
{response.map((value) =&gt; {
return (
&lt;tr key={value.id}&gt;
&lt;td&gt;{value.id}&lt;/td&gt;
&lt;td&gt;{value.user_name}&lt;/td&gt;
&lt;td&gt;{value.phone_number}&lt;/td&gt;
&lt;/tr&gt;
);
})}
&lt;/tbody&gt;

What happens right now is you only have one tr in the table, and that made all the td elements to render in a single row.

huangapple
  • 本文由 发表于 2023年3月9日 23:44:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定