[英]display Number of page in React Js using material-ui Pagination
look at the code below in my App.js Component:查看我的 App.js 组件中的以下代码:
const App = () => {
const [workstations, setWorkstations] = useState([]);
let [page, setPage] = useState(1);
const PER_PAGE = 1;
useEffect(() => {
loadWorkstations();
}, []);
const loadWorkstations = async () => {
const request = await getWorkstations();
const result = request.data;
setWorkstations(result);
};
const count = Math.ceil(workstations.length / PER_PAGE);
const _DATA = usePagination(workstations, PER_PAGE);
const handleChange = (e, p) => {
setPage(p);
_DATA.jump(p);
};
return (
<Pagination
count={count}
size="large"
page={page}
variant="outlined"
color="primary"
onChange={handleChange}
/>
);
};
now the result that i got form this implementation is like this:现在我从这个实现中得到的结果是这样的:
but what im looking for should be look like this:但我要找的应该是这样的:
i mean i don't want to see all my pages on the UI and show the rest with "..."我的意思是我不想在 UI 上看到我的所有页面并用“...”显示其余页面
Note: im using @material-ui/lab注意:我使用@material-ui/lab
您的代码似乎没问题,但是,您需要从 @mui 导入 Pagination 以实现所需的行为
import Pagination from '@mui/material/Pagination';
const App = () => {
const [workstations, setWorkstations] = useState([]);
const [page, setPage] = useState(1);
const [postsPerPage, setPostsPerPage] = useState(10);
const [totalPosts, setTotalPosts] = useState();
const lastPostIndex = page * postsPerPage;
const firstPostIndex = lastPostIndex - postsPerPage;
const currentPosts = workstations.slice(firstPostIndex,lastPostIndex);
useEffect(() => {
loadWorkstations();
}, []);
const loadWorkstations = async () => {
const request = await getWorkstations();
const result = request.data;
setWorkstations(result);
setPage(result.length);
};
return (
<Pagination
count={count}
size="large"
page={page}
variant="outlined"
color="primary"
numberOfPages={totalPages}
totalPosts={currentPosts.length}
postsPerPage={postsPerPage}
setPage={page}
/>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.