如何根据下拉选择更新反应表

如何解决如何根据下拉选择更新反应表

我有一个表,其中包含从 json 文件加载的数据。我需要找到一种方法来根据从下拉菜单中选择的选定货币来更改 {tableDetails.price} 的值。我正在使用 react-select 节点包,因为我认为它会更容易。表格如下所示:

{TableData.map((tableDetails) => {
                                return <div>
                                    <table className="table-fixed w-full">
                                    <tbody>
                                        <tr>
                                            <td id="tablebody1">{tableDetails.Sample_Postcode}</td>
                                            <td id="tablebody2">{tableDetails.Australia}</td>
                                            <td id="price1">{tableDetails.Price1}</td>
                                            <td>{tableDetails.Price2}</td>
                                            <td>{tableDetails.Price3}</td>
                                            <td>{tableDetails.Price4}</td>
                                            <td>{tableDetails.Price5}</td>
                                            <td>{tableDetails.Price6}</td>
                                            <td>{tableDetails.Price7}</td>
                                            <td>{tableDetails.Price8}</td>
                                            <td>{tableDetails.Price9}</td>
                                            <td>{tableDetails.Price10}</td>
                                        </tr>
                                    </tbody>
                                </table>
                                    
                                
                                
                                </div>
            })}

react-select 选项如下所示:

const options = [
        { value: 'USD',label: 'USD' },{ value: 'GBP',label: 'GBP' },{ value: 'EUR',label: 'EUR' }
      ]

任何帮助将不胜感激,干杯

解决方法

对于功能组件,您可以执行以下操作

import Select from "react-select";
import { useState } from "react";

// Assuming only one element with random price
const TableData = [
  {
    Price1: 100,Price2: 230,Price3: 500,Price4: 780,Price5: 101,Price6: 567,Price7: 4356,Price8: 1234,Price9: 8790,Price10: 3242
  }
];

const options = [
  { value: "USD",label: "USD" },{ value: "GBP",label: "GBP" },{ value: "EUR",label: "EUR" }
];

export default function App() {
  // Initialize the state variable with the a currency (USD in this case)
  const [currency,setCurrency] = useState(options[0]);

  const getConvertedPrice = (price) => {
    // assuming your prices TableData are in USD
    // I just pulled conversions from google for USD to currency ABC
    const { value } = currency;
    if (value === "USD") {
      return price.toFixed(2);
    }
    if (value === "GBP") {
      return (price * 0.72).toFixed(2);
    }
    if (value === "EUR") {
      return (price * 0.84).toFixed(2);
    }
  };

  return (
    <div className="App">
      <Select
        className="basic-single"
        isSearchable
        options={options}
        onChange={setCurrency}
        value={currency}
      />
      <h2>My Table with prices</h2>
      <br />
      <br />
      <br />
      <br />
      {TableData.map((tableDetails) => {
        return (
          <div>
            <table className="table-fixed w-full">
              <tbody>
                <tr>
                  <td id="tablebody1">{tableDetails.Sample_Postcode}</td>
                  <td id="tablebody2">{tableDetails.Australia}</td>
                  <td id="price1">{getConvertedPrice(tableDetails.Price1)}</td>
                  <td>{getConvertedPrice(tableDetails.Price2)}</td>
                  <td>{getConvertedPrice(tableDetails.Price3)}</td>
                  <td>{getConvertedPrice(tableDetails.Price4)}</td>
                  <td>{getConvertedPrice(tableDetails.Price5)}</td>
                  <td>{getConvertedPrice(tableDetails.Price6)}</td>
                  <td>{getConvertedPrice(tableDetails.Price7)}</td>
                  <td>{getConvertedPrice(tableDetails.Price8)}</td>
                  <td>{getConvertedPrice(tableDetails.Price9)}</td>
                  <td>{getConvertedPrice(tableDetails.Price10)}</td>
                </tr>
              </tbody>
            </table>
          </div>
        );
      })}
    </div>
  );
}

对于基于类的组件

import Select from "react-select";
import { Component } from "react";

const TableData = [
  {
    Price1: 100,label: "EUR" }
];

export default class App extends Component {
  // Initialize the state variable with the a currency (USD in this case)
  state = {
    currency: options[0]
  }

  getConvertedPrice = (price) => {
    // assuming your prices TableData are in USD
    // I just pulled conversions from google for USD to currency ABC
    const { value } = this.state.currency;
    if (value === "USD") {
      return price.toFixed(2);
    }
    if (value === "GBP") {
      return (price * 0.72).toFixed(2);
    }
    if (value === "EUR") {
      return (price * 0.84).toFixed(2);
    }
  };

  handleCurrencyChange = (changedCurrency) => {
    this.setState({currency: changedCurrency})
  }

  render() {

    return (
      <div className="App">
        <Select
          className="basic-single"
          isSearchable
          options={options}
          onChange={this.handleCurrencyChange}
          value={this.state.currency}
        />
        <h2>My Table with prices</h2>
        <br />
        <br />
        <br />
        <br />
        {TableData.map((tableDetails) => {
          return (
            <div>
              <table className="table-fixed w-full">
                <tbody>
                  <tr>
                    <td id="tablebody1">{tableDetails.Sample_Postcode}</td>
                    <td id="tablebody2">{tableDetails.Australia}</td>
                    <td id="price1">{this.getConvertedPrice(tableDetails.Price1)}</td>
                    <td>{this.getConvertedPrice(tableDetails.Price2)}</td>
                    <td>{this.getConvertedPrice(tableDetails.Price3)}</td>
                    <td>{this.getConvertedPrice(tableDetails.Price4)}</td>
                    <td>{this.getConvertedPrice(tableDetails.Price5)}</td>
                    <td>{this.getConvertedPrice(tableDetails.Price6)}</td>
                    <td>{this.getConvertedPrice(tableDetails.Price7)}</td>
                    <td>{this.getConvertedPrice(tableDetails.Price8)}</td>
                    <td>{this.getConvertedPrice(tableDetails.Price9)}</td>
                    <td>{this.getConvertedPrice(tableDetails.Price10)}</td>
                  </tr>
                </tbody>
              </table>
            </div>
          );
        })}
      </div>
    );
  }
    
}

在 Select 组件引起的状态更改时,您的表格将使用更新后的价格重新呈现。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?