【问题标题】:React Native fetching items from database using Flatlist and listitem to display dataReact Native 使用 Flatlist 和 listitem 从数据库中获取项目以显示数据
【发布时间】:2020-07-21 15:32:23
【问题描述】:

我正在从我的数据库中获取数据并使用 flatlist 和 listitem 将其显示在我的 UI 上。我在一个屏幕上获取数据,然后使用导航道具将该数据发送到下一个屏幕。一切正常,但是当我使用 flatlist 和 listitem 显示数据时,所有数据都集中在一行上。我尝试在后端的 php 代码中使用换行符,但它不起作用。

我曾尝试在 javascript 中使用 RegExp 来分隔数据,但这不起作用。我觉得我错过了这么小的东西,我就是不能指望它。

这是我的 fetch 方法,它从我的后端抓取数据并将其发送到下一个屏幕。

_openTable = (item) => {
        fetch('URL', {
            method: 'POST',
            body: JSON.stringify({tablenumber: item}),
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
        })
            .then((response) => response.text())
            .then((responseJson) => {
                this.setState({
                    dataSource: responseJson,
                });
                console.log(responseJson)
                alert(responseJson)
                this.props.navigation.navigate('ViewParty', {
                    party: responseJson,
                    otherParam: '101',
                });
            })
            .catch((error) => {
                console.log(error);
            });
    }
    GetGridViewItem(uname) {
        Alert.alert(uname);
    }

这是我从最后一个屏幕获取数据并通过 Flatlist 和 Listitem 显示的代码。

constructor(props) {
        super(props);
        this.state = {
            dataSource: [],
        }
    }

    render() {

        const  { navigation } = this.props;
        const cust = navigation.getParam('party', 'No-User');
        const other_param = navigation.getParam('otherParam', 'No-User');

        const cust1 = JSON.stringify(cust);
        const cust2 = cust1.replace(/[\])}[{(]/g, '');
        const cust3 = cust2.replace(/[""]/g, '');
        const cust4 = cust3.replace(/[,]/g, '');
        const cust5 = cust4.replace(/[\\]/g, '');

        let data = this.state.dataSource;
        data.push([cust5]);

        return (
            <View style={styles.container}>
                <MenuButtonWS navigation={this.props.navigation} />
                <FlatList
                    data={data}
                    extraData={this.state}
                    keyExtractor={(item, index) => index.toString()}
                    //numColumns={numColumns}
                    renderItem={({ item, index }) => (
                        <ListItem
                        titleStyle={{ color: 'black', fontWeight: 'bold'}}
                        title={`${item}`}
                        bottomDivider
                        chevron
                        />
                    )}
                />
            </View>

当我控制台记录数据时,这是我将获得的示例..["sam123","beth345"]。它会像这样显示在我的列表项中。全部在一条线上.... sam123beth345。请帮忙。

【问题讨论】:

  • .then((response) =&gt; response.text()) 应该是.then((response) =&gt; response.json()) 可能
  • 非常感谢您回复@skorp。我尝试了 .text() 和 .json() ,它们都给了我相同的结果。我在代码的其他部分使用相同的方式从数据库中获取数据,并且工作正常。我就是不明白。
  • 数据似乎被转换成一个大字符串而不是一个数组。返回给您的数据是什么格式的?为什么需要使用正则表达式转换为数组?也许您可以发布返回给您的原始数据以及格式化数组的完整 console.log。
  • 如果我运行你所有的 cust*.replaces cust5 的最后一个输出是: sam123beth345 删除它们应该可以工作。需要JSON.stringify(cust); 不要删除为什么需要这个替换?
  • @RossHochwert 非常感谢您的回复。是的,它显示为一个大字符串。它作为数组返回。我正在使用正则表达式来删除括号和引号。当我控制台记录它时,我得到 ["sam123","beth345"]。我只是想让用户名适合列表项。

标签: javascript react-native react-native-flatlist listitem react-native-elements


【解决方案1】:

试试这样:

render() {
    const  { navigation } = this.props;
    const cust = navigation.getParam('party', 'No-User');
    const other_param = navigation.getParam('otherParam', 'No-User');

    const cust1 = JSON.stringify(cust);
    return (
        <View style={styles.container}>
            <MenuButtonWS navigation={this.props.navigation} />
            <FlatList
                data={cust1}
                extraData={this.state}
                keyExtractor={(item, index) => index.toString()}
                //numColumns={numColumns}
                renderItem={({ item, index }) => (
                    <ListItem
                    titleStyle={{ color: 'black', fontWeight: 'bold'}}
                    title={item}
                    bottomDivider
                    chevron
                    />
                )}
            />
        </View>

如果 const cust1 = JSON.stringify(cust); 看起来像这样:["sam123","beth345"] 那么这应该可以工作。

在这里查看一个工作示例:snack example 如果它不工作,console.log 数据,cust && cust1

更新

render() {
    const  { navigation } = this.props;
    const cust = navigation.getParam('party', 'No-User');
    const other_param = navigation.getParam('otherParam', 'No-User');

    data = [...cust]
    return (
        <View style={styles.container}>
            <MenuButtonWS navigation={this.props.navigation} />
            <FlatList
                data={data}
                extraData={this.state}
                keyExtractor={(item, index) => index.toString()}
                //numColumns={numColumns}
                renderItem={({ item, index }) => (
                    <ListItem
                    titleStyle={{ color: 'black', fontWeight: 'bold'}}
                    title={item}
                    bottomDivider
                    chevron
                    />
                )}
            />
        </View>

【讨论】:

  • 非常感谢您帮助我解决这个问题。好的,所以我确实更改了我的代码以匹配您上面的代码,但我仍然得到相同的东西“[\”sam123\”,”\beth345\“]”。当我查看您的小吃示例时,它正在工作,但您的平面列表中没有列表项。
  • 我认为列表项不是问题,我认为您的问题是您的数据源不是有效的数组。我用 listitem 更新了这个例子。向我们展示您的 cust 变量在此行之后的样子const cust = navigation.getParam('party', 'No-User');snack.expo.io/@skorp/flatlist-array-usage
  • 所以当我导航到下一个屏幕并使用 console.log 时,我得到了这个 ["sam123","beth345"]。所以它是一个数组。我的构造函数(道具)中有我的数据源。我查看了您的零食以及您在我的代码中拥有的内容,但它不像您拥有的那样工作。
  • 所以第一个是我从数据库中获取后返回的响应数据。["mh036","lh035","padams11","ahall09"]....... ....这是在我通过导航道具发送它之后,我在执行 this.state.dataSource.push(cust); 之后控制台.log cust; ["[\"mh036\",\"lh035\",\"padams11\",\"ahall09\"]"]
  • 如果我将 cust 更改为 cust1 执行 JSON.stringify(cust);我得到......................[“\”[ \\"mh036\\\\",\\\\"lh035\\\\",\\\\"padams11\\\\",\\\\"ahall09\\\\"]\\""] 当我 console.log cust1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多