【发布时间】:2018-03-23 21:35:31
【问题描述】:
我只想打印从 firebase 获得的数组。您可以在图像中看到数组数据。
在其中我很容易将 json 对象数据转换为数组,但我无法在表中打印数组数据。 如果我当时在标签上运行 console.log 命令,它会打印控制台数据,但不打印我从 firebase 获得的数组值,或者你可以说从 firebase 获取。 我想在表格中打印这些数据。
class Contact extends React.Component {
constructor() {
super();
this.state = {
arr: [],
inner_key:[],
Address:'',
printabledata:[],
singledata:{}
};
self = this;
this.printAllRecord = this.printAllRecord.bind(this);
}
componentWillMount() {
var formdata2 = firebase.database().ref("vishal-aaede/");
formdata2.on("value", function (snap) {
data = snap.val();
self.setState({arr: data});
});
}
componentDidUpdate(){
var data=this.state.arr,
headerArray=[];
for (var key in data){
headerArray.push(key);
}console.log(headerArray);
headerArray.forEach(function(val){
self.printAllRecord(data[val])
console.log(data[val].Name);
} );
self.state.singledata=data[headerArray[0]]
;
}
printAllRecord(param,index){
self.state.printabledata.push(<tr>
<td>{param.Name}</td>
<td>{param.Round}</td>
<td>{param.Email}</td>
<td>{param.Date}</td>
<td>{param.Phone}</td>
<td>{param.Address}</td>
<td>{param.Fresher}</td>
<td>{param.Time}</td>
</tr>);
}
render() {
return (
<div>
<h2>Candidate RECORD Coming Soon!!!!</h2>
<p>Write Filter Code here
</p>
<div>
<div className="row">
<div className="col-sm-2"></div>
<div className="col-sm-8">
<div className = "container-fluid table-responsive">
<table className = "table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Interview Round</th>
<th>Email</th>
<th>Date</th>
<th>Phone Number</th>
<th>Address</th>
<th>Gender</th>
<th>Experience</th>
<th>Time</th>
</tr>
</thead>
<tbody>
{this.state.printabledata}
</tbody>
</table>
</div>
</div>
<div className="col-sm-2"></div>
</div>
</div>
</div>
)
}
}
【问题讨论】:
-
你不应该直接操纵
state,使用this.setState,检查this。
标签: javascript arrays reactjs firebase-realtime-database