【发布时间】:2015-12-10 04:30:54
【问题描述】:
这是我的 React 代码,我想使用 React JS 以表格格式打印 JSON 格式代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Initial Data Via AJAX</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/jsx">
var ImageCollect = React.createClass({
getInitialState: function() {
return {
phpData: []
};
},
componentDidMount: function() {
$.get(this.props.source, function(result) {
var collection = result;//when i print this by using console.log the collection is getting the JSON code
if (this.isMounted()) {
this.setState({
phpData : collection
});
}
}.bind(this));
},
render:function()
{
DBdata = this.state.phpData || [];
return (
<div>
<table>
<th>name</th>
<th>email</th>
{DBdata.map(function(d){
return(
<tr>
<td>{d.name}</td>
<td><d.email</td>
</tr>
)}
)}
</table>
</div>
)}
});
React.render(
<ImageCollect source="http://localhost/PHP-React-Demo/index.php" />,
document.getElementById('example')
);
</script>
</body>
</html>
以下是我从 index.php 文件中获取的 JSON 格式代码,输出只有姓名和电子邮件
{"data":[{"id":"1","name":"param","email":"param@nadsoft.com","phone":"9890245130","marks":"65"},{"id":"2","name":"andy","email":"andy@gmail.com","phone":"9665815566","marks":"78"},{"id":"3","name":"asdf","email":"dfgfd@fgdfgd.com","phone":"2132323232","marks":"23"},{"id":"34","name":"rteret","email":"fdg@dfsdf","phone":"21312323232","marks":"23"},{"id":"4236","name":"kjhkj","email":"opiuio","phone":"9890245130","marks":"7"},{"id":"4237","name":"","email":"","phone":"","marks":"0"}]}
我可以更改 JSON 代码或反应吗?我应该在哪里改变,这个反应代码只返回姓名和电子邮件而不是数据。
【问题讨论】:
-
应该改成
var collection = result.data吗?
标签: javascript json reactjs