React Native Picker: 筛选特定且去重的项目

3

我正在使用React Native Picker

我调用了一个API,它返回一个StateCity列表。

  • 目前有两个State: Kuala Lumpur (state: 14) 和 Selangor (state: 10)

  • state: 14有两个City: Kuala Lumpur (city: 262) 和 Sungai Besi (city: 263)

  • state: 10有一个City: Puchong (city: 256)

JSON数据如下:

{
    "data": [
        {
            "city": "262",
            "state": "14",
            "city_desc": "Kuala Lumpur",
            "state_desc": "Kuala Lumpur"
        },
        {
            "city": "263",
            "state": "14",
            "city_desc": "Sungai Besi",
            "state_desc": "Kuala Lumpur"
        },
        {
            "city": "256",
            "state": "10",
            "city_desc": "Puchong",
            "state_desc": "Selangor"
        }
    ]
}

在我的应用程序中,当我调用API时,选择器会加载所有的StateCity
然而,是否有办法过滤StateCity,只显示基于所选StateCity?
例如:如果我选择state: 14,那么选择器应该仅显示city: 262city: 263
或者如果我选择state: 10,那么选择器应该仅显示city: 256
重要提示:目前数据中只有两个State,但将来我将添加多个State,每个州都包含多个City。例如:state: A将拥有5个城市,state: B将拥有3个城市等等。
请告诉我是否有任何高效的方法可以根据所选的State筛选City,如常,非常感谢所有帮助。
以下是提供的代码片段:
class ClientScreen extends Component {

    constructor(props) {
        super(props);
        this.state = {
            pickerValueState: null,
            dataState: [],

            pickerValueCity: null,
            dataCity: [],

            isLoading: true,
        }
    }

    componentDidMount() {
        console.log('ComponentDidMount()');
        this.apiStateCity();
    }

    apiStateCity() {
        let self = this;
        AsyncStorage.getItem('my_token').then((keyValue) => {
            axios({
                method: 'get',
                url: Constants.API_URL + 'user_c/c_State_City/',
                responseType: 'json',
                headers: {
                    'X-API-KEY': Constants.API_KEY,
                    'Authorization': keyValue,
                },
            })
                .then(function (response) {
                    console.log('apiStateCity Response: ', response.data.data);
                    self.setState({
                        dataState: response.data.data,
                    });
                })
                .catch(function (error) {
                    console.log('Error (1st): ', error);
                });
        }, (error) => {
            console.log('Error (2nd): ', error) //Display error
        });
    }

    stateList() {
        return (
            <View>
                <Text>Select Location</Text>
                <Text>State</Text>
                <View>
                    <Picker
                        mode="dropdown"
                        selectedValue={this.state.pickerValueState}
                        onValueChange={(itemValue, itemIndex) => {
                            this.setState({ pickerValueState: itemValue });
                            console.log('State selected (itemValue): ', itemValue);
                        }}
                    >
                        {
                            this.state.dataState.map((item, key) => (
                                <Picker.Item label={item.state_desc} value={item.state} key={key} />)
                            )
                        }
                    </Picker>
                </View>
            </View>
        );
    }

    cityList() {
        return (
            <View>
                <Text>City</Text>
                <View>
                    <Picker
                        mode="dropdown"
                        selectedValue={this.state.pickerValueCity}
                        onValueChange={(itemValue, itemIndex) => {
                            this.setState({ pickerValueCity: itemValue });
                            console.log('City selected (itemValue): ', itemValue);
                        }}
                    >
                        {
                            this.state.dataState.map((item, key) => (
                                <Picker.Item label={item.city_desc} value={item.city} key={key} />)
                            )
                        }
                    </Picker>
                </View>
            </View>
        );
    }

    render() {
        return (
            <View>
                <Text>BookingApp</Text>
                <View>
                    {this.stateList()}
                    {this.cityList()}
                    {this.button()}
                </View>
            </View>
        );
    }
}

应用程序截图:

屏幕截图 1 屏幕截图 2

1个回答

0

var A = {
    "data": [
        {
            "city": "262",
            "state": "14",
            "city_desc": "Kuala Lumpur",
            "state_desc": "Kuala Lumpur"
        },
        {
            "city": "263",
            "state": "14",
            "city_desc": "Sungai Besi",
            "state_desc": "Kuala Lumpur"
        },
        {
            "city": "256",
            "state": "10",
            "city_desc": "Puchong",
            "state_desc": "Selangor"
        }
    ]
};

let B= A.data.filter( item => item.state === '14');
console.log(B);


我只想澄清一下,数据是从API返回的。目前只有两个“State”,但将来会有更多。在您的解决方案中,您只显示了如何过滤出许多“State”中的一个。您能否提供一个示例,以便我能够更好地根据所选的“State”筛选“City”?谢谢。_PS_我已经更新了原始帖子,并添加了其他示例。 - Beep Boop
@Tim 已经注意到了。现在正在更新。 - Beep Boop

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接