Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
Enviar la opción seleccionada al backend

Tengo un menú de selección (con tres opciones) junto con una barra de búsqueda. Todo lo que quiero es guardar la opción seleccionada y el término buscado y enviarlos al back-end. Aquí está mi código:

 import React, { useState, useEffect } from 'react' const Table = () => { const navigate = useNavigate() const [users, setUsers] = useState([]); const [currentUsers, setCurrentUsers] = useState([]); const [search, setSearch] = useState(''); const [column, setColumn] = useState(''); //for saving the selected option useEffect(async () => { try { const response = await getUsers(search); setUsers(response.data.users); } catch (error) { } }, [search]); return ( <input type='text' placeholder='search..' onChange={(e) => setSearch(e.target.value)} value={search} /> <select aria-label=".form-select-sm example"> <option selected value="1">all</option> <option value="2">name</option> <option value="3">phone</option> </select> <table className='w-full border-separate rounded-md'> <thead> <tr className='bg-text-secondary text-white shadow-sm text-center'> <th className='p-2'>name</th> <th className='p-2'>phone</th> </tr> </thead> <tbody> {currentUsers.map((item, index) => <tr key={item.id} className={index % 2 === 0 ? 'bg-white shadow-sm text-center' : 'bg-text bg-opacity-5 shadow-sm text-center'}> <td className='text-text text-sm p-2'>{item.name}</td> <td className='text-text text-sm p-2'>{item.phone}</td> </tr> )} </tbody> </table> ) }

He tenido éxito al recibir el término de búsqueda en el back-end, pero no sé cómo aplicarlo también en la opción seleccionada. Intenté agregar onClick() y onChange() en cada opción y guardar el estado, pero no tuve éxito. ¿Cómo puedo hacer esto?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Su onChange debe estar en la etiqueta de selección. Aquí esta lo que hice.

 import React, { useState, useEffect } from "react"; const Table = () => { const navigate = useNavigate() const [users, setUsers] = useState([]); const [currentUsers, setCurrentUsers] = useState([]); const [search, setSearch] = useState(""); const [column, setColumn] = useState(""); //for saving the selected option useEffect(async () => { try { const response = await getUsers(search); setUsers(response.data.users); } catch (error) { } }, [search]); return ( <> <input type="text" placeholder="search.." onChange={(e) => setSearch(e.target.value)} value={search} /> // added here <select aria-label=".form-select-sm example" onChange={(e) => { setColumn(e.target.value); console.log(e.target.value); }} > <option selected value="1"> all </option> <option value="2">name</option> <option value="3">phone</option> </select> <table className="w-full border-separate rounded-md"> <thead> <tr className="bg-text-secondary text-white shadow-sm text-center"> <th className="p-2">name</th> <th className="p-2">phone</th> </tr> </thead> <tbody> {currentUsers.map((item, index) => ( <tr key={item.id} className={ index % 2 === 0 ? "bg-white shadow-sm text-center" : "bg-text bg-opacity-5 shadow-sm text-center" } > <td className="text-text text-sm p-2">{item.name}</td> <td className="text-text text-sm p-2">{item.phone}</td> </tr> ))} </tbody> </table> </> ); };
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda