【问题标题】:React JS - How can I show 2 dropdowns and show data from static jsonReact JS - 如何显示 2 个下拉列表并显示来自静态 json 的数据
【发布时间】:2018-09-27 08:33:14
【问题描述】:

我是 React JS 的新手 我有 2 个下拉列表:国家和城市。 我想在静态 json 的帮助下显示基于国家的城市 有人可以帮我看看吗?

 [
  {
    "id": "1",
    "country": "India",
    "cities": [
      {
        "id": "01",
        "cityName": "Delhi"
      },
      {
        "id": "02",
        "cityName": "Mumbai"
      },
      {
        "id": "03",
        "cityName": "Pune"
      }
    ]
  },
  {
    "id": "2",
    "country": "Canada",
    "cities": [
      {
        "id": "01",
        "cityName": "Toronto"
      },
      {
        "id": "02",
        "cityName": "Ottawa"
      },
      {
        "id": "03",
        "cityName": "Winnipeg"
      }
    ]
  }
]

import * as React from 'react'; import * as styles from './App.module.scss'; const App = () => { return ( <div className={styles.app}> <div> <span>Country: </span> <select> <option>Select Country</option> </select> </div> <div> <span>City: </span> <select> <option>Select Country</option> </select> </div> </div> ); }; export default App;

【问题讨论】:

  • import * as React from 'react';从 './App.module.scss' 导入 * 作为样式; const App = () => { return (
    国家:
    城市:
    ); };导出默认应用;
  • 我没有时间写完整的代码,但这是你应该做的。首先将json字符串转换为JObject,然后处理该对象以创建Touple,其键值为(国家为键),然后(城市列表为值),然后将它们设置为状态。然后将该状态值绑定到组件并在更改国家/地区选择火处理程序时更新状态。

标签: json reactjs static dropdown


【解决方案1】:

只需为dropdown 国家/地区设置一个onchange 方法,然后在该方法中按所选国家/地区过滤城市

const data = "your JSon"
@computed get countries(){
.map(item => {
return <option key ={item.country.id}> {item.country.name} </option>
}
}
let cities : [];

@computed get cities(){
if(this.cities == null || this.cities == undefined)
return null;

this.cities.map(item => {
return <option key ={item.id}> {item.name} </option>
}
}

countryChange(){
this.cities = data.filter(item => {return item.country === "your value"})[0].cities
}

<select onchange= {this.countryChange}> {this.countries} </select>
<select onchange= > {this.cities } </select>

【讨论】:

    【解决方案2】:

    尝试这样的事情并将数据作为道具传递。您应该根据需要进行重构并使其更通用。

    class SelectComponent extends Component {
    
      state = {country: 0, cityName: 0};
    
      clicked = () => { console.log("clicked") };
    
      handleChange = (key, ev) => {
        let newState = { [key]: parseInt(ev.target.value) };
    
        //reset city to 0 if country changes
        if(key !== "cityName") {
            newState.cityName = 0;
        }
    
        this.setState(newState);
      };
    
      render() {
    
        const { data } = this.props;
    
        const select = (title, data, fieldName) => {
            return (
                <div>
                    <span>{title}:</span>
                    <select onChange={(event) => this.handleChange(fieldName, event)}>
                        {data.map((value, key) =>
                          <option value={key}
                                  selected={this.state[fieldName] === key}
                          >
                            {value[fieldName]}
                          </option>)
                        }
                    </select>
                </div>);
      };
    
        return (
            <div className={styles.app}>
                {
                    [select("Country", data, "country"), select("City", data[this.state.country].cities, "cityName")]
                }
            </div>
        );
      }
    };
    

    Live Preview

    【讨论】:

    • [ts] 类型 '{ [x: number]: number; 上不存在属性 'cityName' }'。任何 - 出现此错误
    猜你喜欢
    • 1970-01-01
    • 2021-06-28
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 2017-10-25
    • 1970-01-01
    相关资源
    最近更新 更多