【问题标题】:Turn 2 dimensional excel table to filtered dropdown将二维 excel 表转为过滤下拉列表
【发布时间】:2021-05-11 05:26:41
【问题描述】:

你能帮我弄清楚如何使用 reactjs 将下面的表格变成过滤的下拉列表。 我需要按年龄过滤结果(假设用户是 33 岁)并向他展示他的年龄组的可用选项(覆盖的资本和与之相关的每月保费)然后捕获所选数据。

这是我迄今为止所取得的成就:

1 - 将 Excel 表转换为 CSV,然后使用此网站 https://shancarter.github.io/mr-data-converter/ 我将其更改为 JSON ARRAY。 这是结果的sn-p:

[["111.000 €","25 €","27 €","28 €","31 €","34 €"  ],["138.000 €","32 €","33€","35€","39 €","42 €"  ].... ]

2 - 从出生日期计算年龄,然后连接一个函数来识别用户所属的类别。注意我所说的索引是列表项的索引 例如: ["111.000 €","25 €","27 €","28 €","31 €","34 €" ] 列表从 0 , 1, 2 ... 对应于excel表格上的列。

getIndex = (age) => {
    let index = null;
    if (age < 25 ) {
        index = 1
    }
    if (age < 30 && age > 24) {
        index = 2
    }
    if (age < 40 && age > 29) {
        index = 3
    }
    if (age < 45 && age > 39) {
        index = 4
    }
    if (age > 45) {
        index = 5
    }
    this.setState({
        age_index : index
    })
};

3- 我使用 forEach 从主列表中获取单独的项目并生成一个新列表,其中仅包含与索引值匹配的参数。

createNewArrayLife = (listitem) => {
        if (this.state.age_index) {
           listitem.forEach((item) => {
               let data =  {
                   capital: item[0],
                   premium: item[this.state.age_index]
               };
               // Something is wrong from Here! ....
               this.setState({
                   life_insurance: [data, ...this.state.life_insurance]
               });
           });
        }
};

由于某种原因,我遇到了一些障碍,状态值仅显示最后一项(就像列表没有被填充一样)

有什么想法吗?

Life_Insurance_price_table.jpg

【问题讨论】:

  • 嗨!您能否展示一下您已经尝试过的内容?
  • @FarrukhNormuradov 我已将表格转换为 JSON 数组,然后计算年龄,然后使用带有条件语句的函数来识别用户属于哪个类别,然后根据年龄条件分配索引匹配 。接下来,我使用 foreach 创建一个新数组,其中仅包含与年龄匹配的参数。并将其插入下拉列表,我离得更近了吗?
  • 太棒了,你为什么不把你当前的解决方案放在这里,这样其他人就会用它来找出缺失的部分?
  • @FarrukhNormuradov 我发布了一些代码你能看看并告诉我我在这里缺少什么吗?
  • 您有机会尝试我的解决方案吗?

标签: javascript reactjs sorting multidimensional-array dropdown


【解决方案1】:

这是基于@FarrukhNormuradov 帮助的更新函数。

createNewArrayLife = (listitem) => {
    const userPricingList = [];
    if (this.state.age_index) {
       listitem.forEach((item) => {
           const data = {
                capital: item[0],
                premium: item[this.state.age_index]
           };
           userPricingList.push(data);
       });
    }
    this.setState({
        life_insurance: userPricingList
    });
};

【讨论】:

    【解决方案2】:

    在第二次阅读时,我意识到您想要存档的内容。
    这是我的解决方案。
    Here 是环境。

    import "./styles.css";
    import { Component } from "react";
    
    const insuranceTarifs = [
      ["111.000 €", "25 €", "27 €", "28 €", "31 €", "34 €"],
      ["138.000 €", "32 €", "33€", "35€", "39 €", "42 €"],
      ["238.000 €", "37 €", "39€", "41€", "43 €", "46 €"]
    ];
    
    const persons = [
      { id: 1, name: "Igor", age: 11 },
      { id: 2, name: "Timur", age: 22 },
      { id: 3, name: "Dilshod", age: 35 },
      { id: 4, name: "Uktam", age: 43 },
      { id: 5, name: "Zarnigor", age: 56 }
    ];
    
    export default class App extends Component {
      state = {
        life_insurance: []
      };
    
      handleOnAgeMatch = (calculation) => {
        if (
          !this.state.life_insurance.find(
            (element) => element.id === calculation.id
          )
        ) {
          this.setState({
            life_insurance: [calculation, ...this.state.life_insurance]
          });
        }
      };
    
      render() {
        const ageElements = persons.map((person) => (
          <li key={person.id}>
            <Person person={person} onAgeMatchHandler={this.handleOnAgeMatch} />
          </li>
        ));
    
        const pricePickers = this.state.life_insurance.map((list) => (
          <PricePicker userPricingList={list} />
        ));
    
        return (
          <div className="App">
            <div className="item">
              <ul>{ageElements}</ul>
            </div>
            <div className="item">
              <ul>{pricePickers}</ul>
            </div>
          </div>
        );
      }
    }
    
    const PricePicker = ({ userPricingList }) => {
      const options = userPricingList.customPricingList.map((item) => (
        <option key={item.premium} value={item.premium}>
          {item.premium}
        </option>
      ));
      return (
        <li>
          <select id={userPricingList.id}>{options}</select>
        </li>
      );
    };
    
    const Person = ({ person, onAgeMatchHandler }) => {
      let index = null;
      const { age, id, name } = person;
      if (age < 25) {
        index = 1;
      }
      if (age < 30 && age > 24) {
        index = 2;
      }
      if (age < 40 && age > 29) {
        index = 3;
      }
      if (age < 45 && age > 39) {
        index = 4;
      }
      if (age > 45) {
        index = 5;
      }
    
      const userPricingList = [];
      insuranceTarifs.forEach((tarif) => {
        const data = {
          capital: tarif[0],
          premium: tarif[index]
        };
        userPricingList.push(data);
      });
    
      onAgeMatchHandler({ id, customPricingList: userPricingList });
    
      return (
        <div>
          {name} {age} y.o.
        </div>
      );
    };
    
    

    【讨论】:

    • 感谢您的帮助。您的答案很棒,尽管它与我的设置 100% 不匹配,因为我使用的设置稍微复杂一些。您对 forEach 的处理非常完美。我用工作功能更新了我的问题。非常感谢你的帮助 。你是最棒的
    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2021-12-18
    • 2015-02-24
    相关资源
    最近更新 更多