1

At the moment, all the available flights that was received from API are successfully loaded on the page. However, I would like to enable the end user to search specific flight, let's say, by flight number and departure date. How can I integrate this searching functionality in the existing codes?

FlightPage.js

 render() {
    return (
      <>
        <h2>Flights</h2>
        {this.props.loading ? (
          <div>Loading...</div>
        ) : (
          <FlightList flights={this.props.flights} />
        )}
      </>
    );
  }
}

As you can see the bellow code, I have used table to present the results.I would like to show only one result or blank table when searching is applied. Can you help me to achieve this?

FlightList.js

const FlightList = ({ flights }) => (
  <table className="table">
    <thead>
      <tr>
        <th />
        <th>Date</th>
        <th>Provider</th>
        <th>Dest</th>
      </tr>
    </thead>
    <tbody>
      {flights.map((f, i) => {
        return (
          <tr key={i}>
            <td>
              <input type="checkbox" name="flightListCheckbox" />
            </td>
            <td>{f.date}</td>
            <td>{f.pnr}</td>
            <td>{f.flightNumber}</td>
          </tr>
        );
      })}
    </tbody>
  </table>
);
2
  • 2
    How about filtering flights before rendering FlightList, what have you tried? Commented Nov 7, 2019 at 13:50
  • I want the results to be shown on the page by default. Then, the user can search
    – Daniel
    Commented Nov 7, 2019 at 13:55

4 Answers 4

2

You could use filter to create a searching functionality like I would at first add an input where I can insert my filter values

FlightPage.js

handleInput: (event) => {
  const { name, value } = event.target

  this.setState({ [name]: value })
}

render () {
  const { filter } = this.state
  return (
    <>
      <input onChange=(this.handleInput) value={filter} name='filter' />
      <FlightList flights={this.props.flights} filterValues={filter} />
    </>
  )
}

Then I would use my state to filter my Object like

FlightList.js


const FlightList = ({ flights, filterValue }) => {

const filterdFlights = flights.filter(flight => Object.values(flight).includes(filterValue))

return (
  <table className="table">
    <thead>
      <tr>
        <th />
        <th>Date</th>
        <th>Provider</th>
        <th>Dest</th>
      </tr>
    </thead>
    <tbody>
      {filterdFlights.map((f, i) => {
        return (
          <tr key={i}>
            <td>
              <input type="checkbox" name="flightListCheckbox" />
            </td>
            <td>{f.date}</td>
            <td>{f.pnr}</td>
            <td>{f.flightNumber}</td>
          </tr>
        );
      })}
    </tbody>
  </table>
)};

1

You need an input for search and filter flights by value of input. Try this

class FlightPage extends React.Component {
  state = {
    keyword: '',
  }

  ...

  getFlights = () => {
    const { keyword } = this.state
    const { flights } = this.props

    return flights.filter(flight => flight.name.includes(keyword)) // name or something else
  }

  onInputChange = e => {
    this.setState({ keyword: e.target.value })
  }

  render () {
    return (
      <>
        <input onChange=(this.onInputChange) value={this.state.keyword} />
        <FlightList flights={this.getFlights()} />
      </>
    )
  }

}
0

You can filter your flights array using flights.filter or sort it using flights.sort.

-2

You could try to use jquery datatable. It adds a lot of funcionality to tables easy to implement.

DataTable doc

1
  • Why should we use another JS Libary when we already use react this is not necessary.
    – Antoni
    Commented Nov 7, 2019 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.