Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
How to show the specified table row data based on the filter option in react js

I am new to reactjs. I am showing the json data in the table. I also want to display only specific table row data for the specific filter option. Here I want when noida is selected then the table should display only 2 nd 3rd row of the table. when Moradabad is selected the it should display only first row of the table.

Here I am attaching the image which displays all the rows , please help me in this filtration logic show only on selected city.

The code is below

    import React from 'react';
    import './style.css';
    
    export default class JsonDataDisplay extends React.Component {
     
     constructor(props) {
        super(props);
        this.state = {
          data: [
            {
              id: 1,
              name: 'Akshit',
              city: 'Moradabad',        
            },
    
            {
              id: 2,
              name: 'Nikita',
              city: 'Noida',
            },
    
            {
              id: 3,
              name: 'Deeksha',
              city: 'Noida',
             }
          ],
        };
      }
      render() {
        const displaydata = this.state.data.map((info) => (
          <tr>
            <td>{info.id}</td>
            <td>{info.name}</td>
            <td>{info.city}</td>
          </tr>
        ));
    
        return (
          <>
            <FilterComponent />
            <br />
            <section>
              <table>
                <thead>
                  <tr>
                    <th>id</th>
                    <th>name</th>
                    <th>city</th>  
                  </tr>
                </thead>
                <tbody>{displaydata}</tbody>
              </table>
            </section>
          </>
        );
      }
    }
    
    
    
    function FilterComponent(props) {
      const data = ['All', 'Noida', 'Moradabad'];
      return (
        <div>
          <div>city</div>
          <select>
            {data.map((field) => (
              <option>{field}</option>
            ))}
          </select>
        </div>
      );
    }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Few more things to do,

  1. Define another state variable to keep the selectedCity state
    this.state = {
      data: [
         ...
         ...
      ],
      selectedCity: "All"
    };
  1. Define a onChange handler function to set the selected city
  setSelectedCity = (selectedCity) => {
    this.setState({ selectedCity });
  };
  1. Add a filter for displaydata as below
const displaydata = this.state.data
      .filter(
        ({ city }) =>
          this.state.selectedCity === "All" || this.state.selectedCity === city
      )
      .map((info) => (
        <tr>
          <td>{info.id}</td>
          <td>{info.name}</td>
          <td>{info.city}</td>
        </tr>
      ));
  1. Pass setSelectedCity as a prop to FilterComponent
<FilterComponent setSelectedCity={this.setSelectedCity} />
  1. Update the FilterComponent to set the selectedCity when selection changes.
function FilterComponent({ setSelectedCity }) {
  const data = ["All", "Noida", "Moradabad"];
  return (
    <div>
      <div>city</div>
      <select onChange={(e) => setSelectedCity(e.target.value)}>
        {data.map((field) => (
          <option value={field}>{field}</option>
        ))}
      </select>
    </div>
  );
}

Code Sandbox

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda