【发布时间】:2020-05-12 06:10:39
【问题描述】:
我在使用 React Js 时遇到了问题。 我的数据表已成功填充,但第一行仍显示“表中没有可用数据” https://imgur.com/ApQPoO9
这是显示的html截图 https://imgur.com/Zk25ksS
另外,当我使用排序和搜索时,我的所有数据都将消失,只显示“表中没有可用数据”
网络结果:https://imgur.com/k5VqJIB 我的代码显示来自数据库的数据
class Uptime extends Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
data: []
}
}
componentDidMount() {
fetch('/uptime')
.then(res => res.json())
// .then(res => res.text()) // convert to plain text
// .then(text => console.log(text))
.then(
(result) => {
this.setState({
isLoaded: true,
data: result.data
})
},
(error) => {
this.setState({
isLoaded: true,
error
})
}
)
}
render() {
const {error, isLoaded, data} = this.state;
// if (error) {
// return <div>Error: {error.message}</div>;
// } else if (!isLoaded) {
// return (
// <div className="spinner-border text-primary" role="status">
// <span className="sr-only">Loading...</span>
// </div>
// )
// } else { WHEN I ADD THIS CODE, THE SEARCH FUNCTION AND SORT FUNCtION IS GONE , SO I JUST REMOVE IT
return (
<div id="content-wrapper" className="d-flex flex-column">
<div id="content">
<div className="container-fluid">
<div className="card shadow mb-4 mt-5">
<div className="card-header py-3 text-center">
<h6 className="m-0 font-weight-bold text-primary">DataTables Example</h6>
</div>
<div className="card-body">
<div className="table-responsive">
<table className="table table-bordered" id="dataTable" width="100%"
cellSpacing="0">
<thead>
<tr>
<th width="10%">Name</th>
<th width="40%">Url</th>
<th width="10%">Downtime</th>
<th width="20%">date</th>
<th width="10%">Status Code</th>
</tr>
</thead>
<tbody>
{data.map(item => (
<tr key={item[0]}>
<td>{item[1]}</td>
<td>{item[2]}</td>
<td>{item[5]}</td>
<td>{item[4]}</td>
<td>{item[3]}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<footer className="sticky-footer bg-white">
<div className="container my-auto">
<div className="copyright text-center my-auto">
<span>Copyright © Your Website 2019</span>
</div>
</div>
</footer>
</div>
)
}
}
【问题讨论】:
-
您好,请提供Minimal, Complete, and Reproducible代码示例,即获取、处理和呈现数据的组件代码。
-
请分享完整代码,以便我们在这里为您提供帮助。
-
我更新了我的代码,请检查
-
看起来“表中没有可用数据”是映射到 DOM 的数据的一部分。您还可以提供来自网络的回复吗?不太可能,但我想知道这是否是一个反应关键问题。
-
@DrewReese,不,“表中没有可用数据”不是我的数据的一部分
标签: reactjs