我正在尝试构建一个从PHP脚本的输出数组动态生成的表。执行componentWillMount后,得到以下输出。但是我试图使用这些数据(前4行是一行)并建立一个表。但是我无法获得如何使用此数组数据并将其动态转换为表的方法。任何输入都受到高度赞赏。
2016-08-24 20:24:06 2016-08-24 20:24:06 test01 20 2016-08-19 08:14:25 2016-08-19 08:14:25 test02 10 componentWillMount: function () { $.ajax({ type: "GET", crossDomain: true, url: "http://localhost:8080/myscript.php", success: function(data){ alert(data); this.setState({testRows: data}); }.bind(this), error:function(data) { alert("Data sending failed"); } }); }, return( <tbody> {this.state.testRows.map(function(row, i) { return ( <tr key={i}> {row.map(function(col, j) { return <td key={j}>{col}</td>; })} </tr> ); })} </tbody> )
我要做的是创建一个二维数组,并将其传递给List组件。如果您知道从php脚本获得的数组中每4个一组都是一行,那么您就可以使用这样的for循环。(phpResponse是php脚本的响应)
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
return( <tbody> {tableData.map((row, index) => { return ( <tr key={"row_" + index}> {row.map((cell, index) => { return ( <td key={"cell_" + index}>{cell}</td> ); })} </tr> ); })} </tbody> );