且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 JSX/React 中使用 PHP 生成的数组数据构建动态表

更新时间:2022-06-12 03:47:14

我所做的是创建一个二维数组,并将其传递给 List 组件.如果您知道从 php 脚本获得的数组中的每组 4 是一行,那么您只需使用这样的 for 循环.(phpResponse 是来自 php 脚本的响应)

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 = [];
    }
}

然后你像这样使用 tableData

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>
);