ReactJS我的搜索功能不能搜索到每一个分页的页面,我该怎么办?

ldioqlga  于 2023-01-25  发布在  React
关注(0)|答案(1)|浏览(129)
  • "我的搜索"功能仅搜索1页上的产品。*
// Pagination source code ==============================================================
    const [currentPage, setCurrentPage] = useState(1)
    const [postsPerPage] = useState(6)

    const indexOfLastPost = currentPage * postsPerPage;
    const indexOfFirstPost = indexOfLastPost - postsPerPage;
    const currentPosts = moviedata.slice(indexOfFirstPost, indexOfLastPost)
    const howManyPages = Math.ceil(moviedata.length/postsPerPage)
<div className='movieslist'>
     <div className='list'>
                                    {currentPosts.filter(u=>u.title.toLocaleLowerCase().includes(query)).map((fd, i) => {
    return <Card  img={fd.img} title={fd.title} quality={fd.quality} rating={fd.rating} price={fd.price} duration={fd.duration} year={fd.year} id={fd.id} allproduct={fd} allwishlist={fd} key={i} />
})}

    </div>

    <Pagination pages = {howManyPages} setCurrentPage={setCurrentPage}/>

</div>
    • 我想搜索所有页面的项目,而不仅仅是一个页面****但它只搜索一个页面上的项目**
nwo49xxi

nwo49xxi1#

1)

乍一看,您似乎在过滤currentPosts
如果你想搜索所有页面的条目,你应该过滤moviedata,而不是在过滤之前切片

2)

当然,它可能需要一点重建其他部分,如果你需要帮助,让我知道!

3)如何重建

如果你的过滤器经常应用,它应该可以解决你的问题
请注意someString.includes('') // always returns true如果查询为空,则将获取所有数据

const [currentPage, setCurrentPage] = useState(1)
    const [postsPerPage] = useState(6)

    const filteredMovieData = moviedata.filter(()=>u=>u.title.toLocaleLowerCase().includes(query)) //new

    const indexOfLastPost = currentPage * postsPerPage;
    const indexOfFirstPost = indexOfLastPost - postsPerPage;
    const currentPosts = filteredMovieData.slice(indexOfFirstPost, indexOfLastPost)
    const howManyPages = Math.ceil(filteredMovieData.length/postsPerPage)

相关问题