Build a dynamic table using array data generated from PHP in JSX/React

[亡魂溺海] 提交于 2019-12-02 01:56:31

What I do is that i create a 2-dimensional array which i pass to the List component. If you know that every group of 4 in the array you get from the php script is a row then you just use a for loop like this.(phpResponse is the response from the php script)

var tableData = [];
var tableRow = [];
for(var x = 1; x <= phpResponse.length; x++) {
    tableRow.push(phpResponse[x]);
    if (x % 4 == 0) {
        tableData.push(tableRow);
        tableRow = [];
    }
}

And then you use tableData like this

return(
    <tbody>
         {tableData.map((row, index) => {
             return (
                 <tr key={"row_" + index}>
                     {row.map((cell, index) => {
                         return (
                             <td key={"cell_" + index}>{cell}</td>
                         );
                     })}
                 </tr>
             );
         })}
    </tbody>
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!