简体   繁体   English

如何根据API响应动态创建一组视图

[英]How to create a set of views dynamically according to API response React Native

I'm working on a react native application and the requirement is to create a list of views to display the booked time slots and the free slots in two different colors. 我正在开发一个本机应用程序,要求是创建视图列表,以两种不同的颜色显示预订的时隙和空闲时隙。 The API response gives only the booked time slots. API响应仅给出预订的时隙。 So is there a way to display a chart like a timetable to this scenario. 因此,有一种方法可以显示这种情况下的时间表等图表。

I have tried the following code to get the outcome. 我尝试了以下代码来获得结果。 But failed. 但是失败了。

componentDidMount() {
    return fetch("https://XXXXXXXXXXX.com")
        .then(response => response.json())
        .then((responseJson) => {
            this.setState({
                isLoading: false,
                dataSource: responseJson
            }),
            function () {
                this.arrayholder = responseJson;
            }

            renderCard = () => {
                return (
                    <View style={{ backgroundColor: 'red', 
                                   height: 100, 
                                   width: 100 }}>
                    </View>
                )
            };
        })
        .catch(error => console.log(error))
}

renderItem = (data) =>
    <TouchableOpacity
        onPress={() => {
            this.props.navigation.navigate('OfferDetails');
        }}
        style={{ borderWidth: 2, 
                 borderColor: '#02539D', 
                 height: config.deviceHeight * 0.15, 
                 justifyContent: 'center', 
                 marginLeft: config.deviceWidth * 0.05, 
                 width: config.deviceWidth * 0.9, 
                 borderRadius: 10 
        }}
    >
        <View style={{ flexDirection: 'row' }}>
            <View style={{ flex: 0.35 }}>
                <Image style={{ height: config.deviceHeight * 0.12, 
                                width: config.deviceWidth * 0.3 
                             }}
                       source={images.durdans_logo}>
                </Image>
            </View>
            <View style={{ flex: 0.65 }}>
                <View style={{ flex: 1, backgroundColor: 'red' }}></View>
            </View>
        </View>
    </TouchableOpacity>

render() {
    return (
        <View style={styles.container}>
            <SafeAreaView>
                <ScrollView horizontal={'true'}>
                    <View style={{ width: config.deviceWidth }}>
                        <ScrollView 
                            vertical={true} 
                            style={{ marginTop: config.deviceHeight * 0.03, 
                                     marginLeft: config.deviceWidth * 0.05, 
                                     marginRight: config.deviceWidth * 0.05   
                                  }
                        >
                        <FlatList
                            data={this.state.dataSource}
                            ItemSeparatorComponent={this.FlatListItemSeparator}
                            renderItem={
                                item => this.renderItem(item)}
                                enableEmptySections={true}
                                style={{ marginTop: 10 }}
                                keyExtractor={(item, index) => index.toString()
                            }
                        />
                        </ScrollView>
                    </View>
                </ScrollView>
            </SafeAreaView>
        </View>
    );
}

Is there a way to build a solution for this scenario. 有没有一种方法可以针对这种情况构建解决方案。 Thanks in advance. 提前致谢。

Instead of rendering only booked time slots, render all the slots, and show it as booked or not. 代替仅呈现预订的时隙,而是呈现所有时隙,并将其显示为已预订或不显示。

eg: 例如:

renderItem = (data) =>
   data.booked?
    <TouchableOpacity
        onPress={() => {
            this.props.navigation.navigate('OfferDetails');
        }}
        style={{ borderWidth: 2, 
                 borderColor: '#02539D', 
                 height: config.deviceHeight * 0.15, 
                 justifyContent: 'center', 
                 marginLeft: config.deviceWidth * 0.05, 
                 width: config.deviceWidth * 0.9, 
                 borderRadius: 10 
        }}
    >
        <View style={{ flexDirection: 'row' }}>
            <View style={{ flex: 0.35 }}>
                <Image style={{ height: config.deviceHeight * 0.12, 
                                width: config.deviceWidth * 0.3 
                             }}
                       source={images.durdans_logo}>
                </Image>
            </View>
            <View style={{ flex: 0.65 }}>
                <View style={{ flex: 1, backgroundColor: 'red' }}></View>
            </View>
        </View>
    </TouchableOpacity>
   :
    <Text>Not Booked</Text>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM