gpt4 book ai didi

javascript - 如何使用 Material UI 表分页在每页显示正确的行

转载 作者:行者123 更新时间:2023-12-05 09:11:19 25 4
gpt4 key购买 nike

我正在尝试使用 Material UI 表格分页来显示每页 5 行,但它会显示页面上的所有项目并且不会更改页面。无法找出我犯错的地方。我已经在沙盒中重现了我的问题,请你检查一下:https://codesandbox.io/s/magical-davinci-t2nq7

 state = {
rowsPerPage: 5,
page: 0
};

handleChangePage = (event, page) => {
this.setState({ page });
};

handleChangeRowsPerPage = event => {
this.setState({ rowsPerPage: event.target.value });
};


<TablePagination
rowsPerPageOptions={[5, 10, 25]}
component="div"
count={data.length}
rowsPerPage={this.state.rowsPerPage}
page={this.state.page}
onChangePage={this.handleChangePage}
onChangeRowsPerPage={this.handleChangeRowsPerPage}
/>

最佳答案

最好使用后端来获取分页数据,因为您可以将 OffsetLimit 传递给 API 并从中获取数据,但现在我只是修复你的问题是这样的:

import React, { Component } from "react";
import data from "./data.js";
import "./styles.css";
import Table from "@material-ui/core/Table";
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableContainer from "@material-ui/core/TableContainer";
import TableHead from "@material-ui/core/TableHead";
import TableRow from "@material-ui/core/TableRow";
import TablePagination from "@material-ui/core/TablePagination";
import Paper from "@material-ui/core/Paper";

class App extends Component {
state = {
rowsPerPage: 5,
page: 0,
Offset: 0,
tempData: []
};

componentDidMount() {
this.getData();
}

handleChangePage = (event, page) => {
let { rowsPerPage, page: currentPage } = this.state;
if (currentPage < page) {
return this.setState(
prevState => {
return {
Offset: prevState.Offset + rowsPerPage,
page
};
},
() => this.getData()
);
} else {
return this.setState(
prevState => {
return {
Offset: prevState.Offset - rowsPerPage,
page
};
},
() => this.getData()
);
}
};

handleChangeRowsPerPage = event => {
this.setState({ rowsPerPage: event.target.value } , () => this.getData());
};

getData = () => {
let { Offset, rowsPerPage: Limit } = this.state;
let tempArr = [];
for (let i = Offset; i < Offset + Limit; i++) {
tempArr.push(data[i]);
}

return this.setState({ tempData: tempArr });
};

render() {
console.log(this.state);
return (
<div className="App">
<TableContainer component={Paper} elevation={0}>
<Table aria-label="simple table">
<TableHead>
<TableRow>
<TableCell>Name</TableCell>
<TableCell>Age</TableCell>
<TableCell>ID</TableCell>
</TableRow>
</TableHead>
<TableBody>
{this.state.tempData.map((rows, idx) => {
return (
<TableRow key={idx} hover={true}>
<TableCell>{rows.name}</TableCell>
<TableCell>{rows.age}</TableCell>
<TableCell>{rows.id}</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>

<TablePagination
rowsPerPageOptions={[5, 10, 25]}
component="div"
count={data.length}
rowsPerPage={this.state.rowsPerPage}
page={this.state.page}
onChangePage={this.handleChangePage}
onChangeRowsPerPage={this.handleChangeRowsPerPage}
/>
</TableContainer>
</div>
);
}
}

export default App;


通过这种方式,您将 OffsetLimit 传递给您的获取数据方法,它将为您呈现过滤后的数据

但你可以从这里看到演示:https://codesandbox.io/s/friendly-williams-xxjf7

关于javascript - 如何使用 Material UI 表分页在每页显示正确的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60293750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com