javascript - 选中复选框时如何显示和隐藏 id

标签 javascript reactjs firebase firebase-realtime-database checkbox

我有一个问题,我想知道在检查 chebkbox 时如何根据其 id 显示数据。

例如,当选中第一个复选框时,我只想要名为“0”的地址,并隐藏我已经尝试过的其余其他地址,但我陷入困境,我想了解如何做。

我使用 firebase 作为数据库:

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;

我现在拥有的:

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 : "",
    cliAdress  : null
  };

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

    });
  }

  handleOptionChange = changeEvent => {

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

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



  };

  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>
    ));


    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>{this.state.cliAdress}</tbody>
        </Table>
      </>
    );
  }
}

可能有一些编译错误,因为没有编译和测试,

关于javascript - 选中复选框时如何显示和隐藏 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59457111/

相关文章:

javascript - 如何从指令中的 templateUrl 运行 $scope 函数?

javascript - jQuery 设置<输入类型 ="range"> 值

reactjs - 在React应用程序中使用全局配置对象

javascript - 在 html img 标签中显示来自 firebase 存储的图像

javascript - 如何从 Chrome devtools 扩展 "before"页面加载中公开函数?

javascript - 从 img 标签获取源图像文件

html - 在其中添加图像时,Div 高度未填满屏幕大小

javascript - 获取用于 React 路由的 Electron 应用程序的路径

ios - 对来自 firebase 的 tableView 数据进行排序

java - 最近的 Android Studio 无法连接到 Firebase?