Searching input of table’s items messing with table’s pagination

I have a table that divides the its items per 20 lines and, above the table, a searching input to filter items that matches the written words. Both the tools works ok alone. I can navigate through the pages when there are more than 20 items and i can find the items when i type it in the inut.

The problem is: everytime i have more than one page i try to search something, the item appears in the first page and if i am in the second page i can see nothing unless i go back to the first page.

I think that the correct thing to do is to decrease the number of pages to matche the quantity of items that i found, but i don’t know how to do it.

You can take a look in the page and the code below:

The code:

    const [currentPage, setcurrentPage] = useState(1);
    const itemsPerPage = 15;

    // Calculo dos índices para exibir os itens da página atual
    const indexOfLastItem = currentPage * itemsPerPage;
    const indexOfFirstItem = indexOfLastItem - itemsPerPage;
    // const currentItems = produtos.slice(indexOfFirstItem, indexOfLastItem);

    const totalPages = Math.ceil(produtos.length / itemsPerPage);

    const changePage = (newPage) => {
        if(showDetalhes === true){
        setShowDetalhes(false);
        }
        setcurrentPage(newPage);
    };
    

    //jsx return
    <table className="table" id='table-estoque'>
                    <thead>
                        <tr>
                            <th scope="col" id="titulo-tabela-padrao">Categoria</th>
                            <th scope="col" id="titulo-tabela-padrao">Código</th>
                            <th scope="col" id="titulo-tabela-padrao" style={{ width: '40%' }}>Produto</th>
                            <th scope="col" id="titulo-tabela-padrao">Preço</th>
                            <th scope="col" id="titulo-tabela-padrao">Preço Promocional</th>
                            <th scope="col" id="titulo-tabela-padrao" style={{width:'2%', color:'#FFEA00'}}><i className="fa-solid fa-pen fa-sm"></i></th>
                            <th scope="col" id="titulo-tabela-padrao" style={{width:'2%', color:'#eb2632'}}><i className="fa-solid fa-trash fa-sm"></i></th>
                        </tr>
                    </thead>
                    <tbody>

                        {produtos
                            .filter((item) => {
                                // return search.toLowerCase() === '' || item.pro_descricao.toLowerCase().includes(search.toLowerCase()); //se digitar algo aparece na lista
                                const searchWords = search.toLowerCase().split(' '); //atribui à searchWirds tudo que foi digitado dividido por space                        
                                return searchWords.every((word) => //faz a busca para cada palavra digitada mesmo que fora de sequencia
                                item.pro_descricao.toLowerCase().includes(word)
                                ); 
                            })
                            .slice(indexOfFirstItem, indexOfLastItem) //Mudei a logica para paginacao para antes do map e nao la em cima como antes 
                            .map((produto, index) => (
                                <React.Fragment key={index}>
                                <tr>
                                    <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">{produto.cat_descricao}</td>
                                    <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">{produto.pro_codigo}</td>
                                    <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">{produto.pro_descricao}</td>
                                    <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">{new Intl.NumberFormat('pt-BR', {minimumFractionDigits: 2,maximumFractionDigits: 2}).format(produto.pro_preco1)}</td>
                                    {produto.pro_preco_promocao > 0 ? <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">{new Intl.NumberFormat('pt-BR', {minimumFractionDigits: 2,maximumFractionDigits: 2}).format(produto.pro_preco_promocao)}</td>
                                    :
                                    <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">--</td>                                      
                                    }
                                    <td  className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao"><div id="a-editar" onClick={(e) => OpenModalProduto(produto.pro_id)} ><i className="fa-solid fa-pen fa-sm"></i></div></td>
                                    <td  className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao"><div id="a-excluir" onClick={(e) => confirmarExclusao(produto.pro_id)}><i className="fa-solid fa-trash fa-sm"></i></div></td>
                                </tr>
                                {showDetalhes && detalhesIndex === index && (
                                <tr>
                                    <td  className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} colSpan="12" style={{ borderBottom: '1px solid #000' }}>
                                    <div style={{ padding: '15px' }}>
                                        {
                                            jsonOpcionais?.map(item => {
                                                return <div className='row' key={item.poi_id}>
                                                        <div className='col-1 ms-auto me-3'>
                                                        {item.status === 'N' ? <span id='status-itens-op' className='badge bg-secondary ms-2'>Inativo</span> : <span id='status-itens-op' className='badge bg-success ms-2'>Ativo</span>}
                                                        </div>
                                                        <div className='col-4'>
                                                        <span>{item.poi_descricao}</span>
                                                        </div>
                                                        <div className='col-1 me-auto'>
                                                        {item.poi_valor === 0 ? <span id='span-zero'>{new Intl.NumberFormat('pt-BR', {style: 'currency', currency:'BRL'}).format(item.poi_valor)} </span> : <span id='span-maior-zero' key={index}>{new Intl.NumberFormat('pt-BR', {style: 'currency', currency:'BRL'}).format(item.poi_valor)} </span>}
                                                        </div>
                                                    </div>
                                            })
                                        }
                                    </div>
                                    </td>
                                </tr>
                                )}
                            </React.Fragment>
                            ))}
                    </tbody>
                </table>
                {totalPages > 1 && (
                    <div className="pagination">
                        <button className="btn btn-secondary btn-sm ms-auto" onClick={() => changePage(currentPage - 1)} disabled={currentPage === 1}> Anterior</button>
                            <span className="span-pagina ms-1 me-1 ">Página {currentPage} / {totalPages}</span>
                        <button className="btn btn-secondary btn-sm" onClick={() => changePage(currentPage + 1)} disabled={currentPage === totalPages}>Próxima</button>
                    </div>
                )}

2

//replace this

const totalPages = Math.ceil(produtos.length / itemsPerPage); 

//new code

 const totalPages = Math.ceil(
    produtos.filter((item) => {
      // return search.toLowerCase() === '' || item.pro_descricao.toLowerCase().includes(search.toLowerCase()); //se digitar algo aparece na lista
      const searchWords = search.toLowerCase().split(" "); //atribui à searchWirds tudo que foi digitado dividido por space
      return searchWords.every(
        (
          word //faz a busca para cada palavra digitada mesmo que fora de sequencia
        ) => item.pro_descricao.toLowerCase().includes(word)
      );
    }).length / itemsPerPage
  );

secondly try to keep this in parent and pass it as props to table component

const [currentPage, setcurrentPage] = useState(1);
    const itemsPerPage = 15;

    // Calculo dos índices para exibir os itens da página atual
    const indexOfLastItem = currentPage * itemsPerPage;
    const indexOfFirstItem = indexOfLastItem - itemsPerPage;
    // const currentItems = produtos.slice(indexOfFirstItem, indexOfLastItem);

    const totalPages = Math.ceil(produtos.length / itemsPerPage);

and i assume the onChange function of search field is something like this

const handleChange=(e)=>{
   setSearch(e.target.value);
   //add this line below
   setcurrentPage(1)   
}

i found a way:

function Produtos() {

// Calculo dos índices para exibir os itens da página atual
const indexOfLastItem = currentPage * itemsPerPage;
const indexOfFirstItem = indexOfLastItem - itemsPerPage;
// const currentItems = produtos.slice(indexOfFirstItem, indexOfLastItem);

let totalPages = Math.ceil(produtos.length / itemsPerPage);

const changePage = (newPage) => {
    if(showDetalhes === true){
    setShowDetalhes(false);
    }
    setcurrentPage(newPage);
};

useEffect(() => {
    setcurrentPage(1);
},[search]);


return <>
<Navbar tela='cardapio'/> 
        
<ProdutoModal isOpen={isProdutoOpen}
              onRequestClose={CloseModalProduto}
              categorias={categorias}
              dadosProduto={dadosProduto}
              onClickSalvar={ListarCardapio}
/>

<ProdutoOpcionalModal isOpen={isOpcionalOpen}
                      onRequestClose={CloseModalOpcional}
                      id_pro={id_pro}
                      pro_descricao={pro_descricao}
/>
<LoadingScreen isLoading={isLoading} />

<div className='container-fluid corpo-pagina'>

    <div className="d-flex table-buttons mt-4">
        <button id='style-outline-button' onClick={() => OpenModalProduto(0)} className='btn ms-2' >
            <i className='fas fa-plus'></i>Adicionar Produto
        </button>
        <button onClick={exportToExcel} className="button-exportar">Exportar</button>

        <div className="box-input">
            <i className="fa fa-search fa-sm" aria-hidden="true"></i>
            <input onChange={(e) => setSearch(e.target.value)} type="text" placeholder="Localizar" id="input-localizar-intern"/>
        </div>
    </div>

    <div className='m-2 mt-4'>
        {(() => {
            const filteredProdutos = produtos.filter((item) => {
                const searchWords = search.toLowerCase().split(' ');
                return searchWords.every((word) => item.pro_descricao.toLowerCase().includes(word));
            });

            totalPages = Math.ceil(filteredProdutos.length / 15);

            return (
                <>
                    <table className="table" id='table-estoque'>
                        <thead>
                            <tr>
                                <th scope="col" id="titulo-tabela-padrao">Categoria</th>
                                <th scope="col" id="titulo-tabela-padrao">Código</th>
                                <th scope="col" id="titulo-tabela-padrao" style={{ width: '40%' }}>Produto</th>
                                <th scope="col" id="titulo-tabela-padrao">Preço</th>
                                <th scope="col" id="titulo-tabela-padrao">Preço Promocional</th>
                                <th scope="col" id="titulo-tabela-padrao" style={{width:'2%', color:'#FFEA00'}}>
                                    <i className="fa-solid fa-pen fa-sm"></i>
                                </th>
                                <th scope="col" id="titulo-tabela-padrao" style={{width:'2%', color:'#eb2632'}}>
                                    <i className="fa-solid fa-trash fa-sm"></i>
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            {filteredProdutos
                                .slice(indexOfFirstItem, indexOfLastItem)
                                .map((produto, index) => (
                                    <React.Fragment key={index}>
                                        <tr>
                                            <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">{produto.cat_descricao}</td>
                                            <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">{produto.pro_codigo}</td>
                                            <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">{produto.pro_descricao}</td>
                                            <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">{new Intl.NumberFormat('pt-BR', {minimumFractionDigits: 2,maximumFractionDigits: 2}).format(produto.pro_preco1)}</td>
                                            {produto.pro_preco_promocao > 0 ? <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">{new Intl.NumberFormat('pt-BR', {minimumFractionDigits: 2,maximumFractionDigits: 2}).format(produto.pro_preco_promocao)}</td>
                                            :
                                            <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">--</td>                                      
                                            }
                                            <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">
                                                <div id="a-editar" onClick={() => OpenModalProduto(produto.pro_id)} >
                                                    <i className="fa-solid fa-pen fa-sm"></i>
                                                </div>
                                            </td>
                                            <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} id="item-tabela-padrao">
                                                <div id="a-excluir" onClick={() => confirmarExclusao(produto.pro_id)}>
                                                    <i className="fa-solid fa-trash fa-sm"></i>
                                                </div>
                                            </td>
                                        </tr>
                                        {showDetalhes && detalhesIndex === index && (
                                        <tr>
                                            <td className={index % 2 === 0 ? 'td-item-par' : 'td-item-impar'} colSpan="12" style={{ borderBottom: '1px solid #000' }}>
                                                <div style={{ padding: '15px' }}>
                                                    {jsonOpcionais?.map(item => (
                                                        <div className='row' key={item.poi_id}>
                                                            <div className='col-1 ms-auto me-3'>
                                                                {item.status === 'N' ? <span id='status-itens-op' className='badge bg-secondary ms-2'>Inativo</span> : <span id='status-itens-op' className='badge bg-success ms-2'>Ativo</span>}
                                                            </div>
                                                            <div className='col-4'>
                                                                <span>{item.poi_descricao}</span>
                                                            </div>
                                                            <div className='col-1 me-auto'>
                                                                {item.poi_valor === 0 ? <span id='span-zero'>{new Intl.NumberFormat('pt-BR', {style: 'currency', currency:'BRL'}).format(item.poi_valor)} </span> : <span id='span-maior-zero' key={index}>{new Intl.NumberFormat('pt-BR', {style: 'currency', currency:'BRL'}).format(item.poi_valor)} </span>}
                                                            </div>
                                                        </div>
                                                    ))}
                                                </div>
                                            </td>
                                        </tr>
                                        )}
                                    </React.Fragment>
                                ))}
                        </tbody>
                    </table>
                    {totalPages > 1 && (
                        <div className="pagination">
                            <button className="btn btn-secondary btn-sm ms-auto" onClick={() => changePage(currentPage - 1)} disabled={currentPage === 1}> Anterior</button>
                            <span className="span-pagina ms-1 me-1 ">Página {currentPage} / {totalPages}</span>
                            <button className="btn btn-secondary btn-sm" onClick={() => changePage(currentPage + 1)} disabled={currentPage === totalPages}>Próxima</button>
                        </div>
                    )}
                </>
            );
        })()}
    </div>
</div>

Set current page as 1 everytime i type in search input and totalPages = Math.ceil(filteredProdutos.length / 15);
after filtering

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật