Rumah > Soal Jawab > teks badan
Saya mempunyai projek API yang berfungsi termasuk Pagination.First、Pagination.Prev、Pagination.Item、Pagination.Next 和 Pagination.Last
.
Saya mahu menambah Pagination.Ellipsis
padanya tetapi saya tidak tahu cara memasukkannya ke dalam kod saya.
Lihat dokumentasi penomboran react-bootstrap.github.io
Ini adalah contoh perkara yang saya cuba capai menggunakan kotak pasir ellipsis penomboranEllipses.
Berikut ialah kod saya, yang juga merupakan kod kotak pasir https://codesandbox.io/.
Movielist.js
import { React, useState, useEffect } from "react"; import { Col, Card, Row, Pagination } from "react-bootstrap"; import MovieListPagination from "./MovieListPagination"; const MovieList = () => { const [error, setError] = useState(null); const [isLoaded, setIsLoaded] = useState(false); const [items, setItems] = useState([]); // Default current pages and posts const [currentPage, setCurrentPage] = useState(1); const [postsPerPage] = useState(4); // Get current posts const indexOfLastPost = currentPage * postsPerPage; const indexOfFirstPost = indexOfLastPost - postsPerPage; const currentPosts = items.slice(indexOfFirstPost, indexOfLastPost); // Change page const paginate = (pageNumber) => setCurrentPage(pageNumber); useEffect(() => { fetch("https://jsonplaceholder.typicode.com/posts") .then((res) => res.json()) .then( (result) => { setIsLoaded(true); setItems(result); //console.log(result.results); }, (error) => { setIsLoaded(true); setError(error); } ); }, []); if (error) { return <div className="p-3">Error: {error.message}</div>; } else if (!isLoaded) { return <div className="p-3">Loading...</div>; } else { return ( <> <Row className="justify-content-center"> {currentPosts.map((item) => ( <Col sm={6} lg={4} xl={3} className="py-3" key={item.id}> <Card bg="dark" text="light" className="text-center h-100"> <Card.Body> <Card.Title>{item.title}</Card.Title> <Card.Text>{item.body}</Card.Text> </Card.Body> </Card> </Col> ))} <Pagination className="justify-content-center"> <MovieListPagination currentPage={currentPage} postsPerPage={postsPerPage} totalPosts={items.length} paginate={paginate} /> </Pagination> </Row> </> ); } }; export default MovieList;
MovielistPagination.js
import { React } from "react"; import { Pagination } from "react-bootstrap"; const MovieListPagination = ({ currentPage, postsPerPage, totalPosts, paginate }) => { const pageNumbers = []; const pagesCount = Math.ceil(totalPosts / postsPerPage); const isCurrentPageFirst = currentPage === 1; const isCurrentPageLast = currentPage === pagesCount; for (let i = 1; i <= pagesCount; i++) { pageNumbers.push(i); } return ( <> <Pagination.First onClick={() => paginate(1)} /> <Pagination.Prev onClick={() => paginate(currentPage - 1)} disabled={isCurrentPageFirst} /> {pageNumbers.map((number) => ( <Pagination.Item key={number} className={`${currentPage === number ? "active" : ""}`} onClick={() => { paginate(number); }} > {number} </Pagination.Item> ))} <Pagination.Next onClick={() => paginate(currentPage + 1)} disabled={isCurrentPageLast} /> <Pagination.Last onClick={() => paginate(pagesCount)} /> </> ); }; export default MovieListPagination;
Saya mencuba beberapa penyelesaian seperti <Pagination.Ellipsis key={pageNumbers} className="muted" />
dan kod ff...tetapi ia tidak berkesan.
// Add ellipses if there are more pages to display if (pageNumbers[0] > 1) { pageNumbers.unshift(<Pagination.Ellipsis key="ellipsis-start" />); } if (pageNumbers[pageNumbers.length - 1] < totalPosts) { pageNumbers.push(<Pagination.Ellipsis key="ellipsis-end" />); }
P粉4929595992024-02-26 09:22:30
Jawapan semasa saya untuk soalan ialah menggunakan react-paginate. Anda boleh mencari jawapan bercabang saya di kotak dan kotak.
Saya akan teruskan soalan saya, mungkin seseorang boleh membantu. Terima kasih!