lundi 23 décembre 2019

How to Show and Hide with id when the checkbox is checked

I have a problem, I would like to know how to display data thanks to their id when the chebkbox is checked.

For example when the first checkbox is checked I just want the address named "0" and hidde the rest of the others adress I already try to do but I'm stuck and I would like to learn how to do .

I'm using firebase as db :

enter image description here

import React, { Component } from "react";
import * as firebase from "firebase";
import { Table, InputGroup } from "react-bootstrap";
class ListClients extends React.Component {
  state = {
    loading: true,
    selectedOption: "option0",
    selectedStyle : {display : 'none'},
    selectedId : ""
  };

  componentWillMount() {
    const ref = firebase
      .database()
      .ref("listClients")
      .orderByChild("id");
    ref.on("value", snapshot => {
      this.setState({
        listClients: snapshot.val(),
        loading: false
      });

    });
  }

  handleOptionChange = changeEvent => {

    this.setState({
      selectedOption: changeEvent.target.value,
      selectedStyle : {display : 'block'},
      selectedId: 1
    });


  };

  render() {
    let selectedStyle = this.state.selectedStyle

    if (this.state.loading) {
      return <h1>Chargement...</h1>;
    }
    const clients = this.state.listClients.map((client, i) => (
      <tr>
        <td>
          <input
            key={client + i}
            id={"checkbox" }
            type="checkbox"
            value={"option"+i}

            onChange={this.handleOptionChange}
            checked={this.state.selectedOption === "option"+i}
          />
        </td>
        <td>{client.nom}</td>
        <td>{client.prenom}</td>
      </tr>
    ));

    const clientAdr = this.state.listClients.map((client, i) => (
      <tr key={i}>
        <td id={"myDIV" + i}   value={"option"+i} onChange={this.handleOptionChange} style={selectedStyle}>
          {client.adresse}
        </td>
      </tr>
    ));




    return (
      <>
        <Table className="ContentDesign" striped bordered hover>
          <thead>
            <tr>
              <th></th>

              <th>First Name</th>
              <th>Last Name</th>
            </tr>
          </thead>
          <tbody>{clients}</tbody>
        </Table>

        <Table className="ContentDesign" striped bordered hover>
          <thead>
            <tr>
              <th>Adresse : </th>
            </tr>
          </thead>

          <tbody>{clientAdr}</tbody>
        </Table>
      </>
    );
  }
}

export default ListClients;

What I have for the moment :

enter image description here




Aucun commentaire:

Enregistrer un commentaire