请问我有问题。下面的代码使用加载更多按钮来显示记录。 我将限制设置为2.问题是它显示前2条记录,在下一次点击时,它显示剩余的6条记录,而不是以用户点击为基础的限制显示在两个(2)中。 有人可以帮我解决这个问题。加载更多按钮与Reactjs无法正常工作
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<style>
.pic{
background:blue; color:white;}
</style>
<script src="build/react.min.js"></script>
<script src="build/react-dom.min.js"></script>
<script src="build/browser.min.js"></script>
<script src="build/jquery.min.js"></script>
<div id="app"></div>
<script type="text/babel">
class Application extends React.Component {
constructor(props) {
super(props)
this.state = {rec : [
{ "name" : "Tony", "Age" : "18"},
{ "name" : "John", "Age" : "21" },
{ "name" : "Luke", "Age" : "78" },
{ "name" : "Mark", "Age" : "90" },
{ "name" : "Jame", "Age" : "87" },
{ "name" : "Franco", "Age" : "34" },
{ "name" : "Biggard", "Age" : "19" },
{ "name" : "tom", "Age" : "89" },
],
limit : 2};
this.loadMore = this.loadMore.bind(this);
}
loadMore() {
let recLength = this.state.rec.length;
this.setState({limit:recLength});
// show more entries
// switch to "show less"
}
render() {
return <div className="container">
<div>
<h3>List of Records</h3>
<ul>
{this.state.rec.slice(0,this.state.limit).map((obj, i) =>
<li key={i}>{obj.name} - {obj.Age}</li>
)}
</ul>
</div>
<p>
<a className="pic" onClick={this.loadMore}>Load more Button</a>.
<br />
//display image spinner as user clicks on load more button
</p>
</div>;
}
}
ReactDOM.render(<Application />, document.getElementById('app'));
</script>
</body>
</html>
您不必担心限制大于'rec.length'。它会给出一个超出范围的索引,因此它只会返回所有索引(这将是数组中的所有内容)。 –
@Ramzi C.你的解决方案是好的,但如果没有更多的记录显示,我如何打印消息“没有更多的记录加载”。我可以在用户点击加载更多按钮时显示加载图像。谢谢 – briandenny
为您的评论添加了一些更改。 –