【问题标题】:How to display dynamic array of object in table under react native app?如何在反应本机应用程序下的表格中显示对象的动态数组?
【发布时间】:2020-05-30 20:17:19
【问题描述】:

我正在构建一个反应原生应用程序,从主屏幕移动到设置屏幕。
我需要加载表格格式的动态数组。
现在我正在使用 compoundDidMount() 从 api 获取数据的函数。
一切都很好,但我不知道如何渲染从 compoundDidMount() 函数获得的输出数组。

import React from 'react';
import {
  StyleSheet,
  Text,
  TextInput,
  View,
  Button,
  ImageBackground,
  Dimensions,
  ActivityIndicator,
  Alert,
  ScrollView
} from 'react-native';

import { Table, TableWrapper, Row, Rows, Col, Cols, Cell } from 'react-native-table-component';

import { createAppContainer} from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import { createBottomTabNavigator } from 'react-navigation-tabs';
import Icon from 'react-native-vector-icons/Ionicons';
import FormData from 'FormData';
import styles from "../css/styles";

export default class SettingsScreen extends React.Component {


   constructor(props) {
      super(props);
      this.state = {
        tableHead: ['Head', 'Head2', 'Head3'],
        widthArr: [40, 60, 80, 100, 120, 140, 160, 180, 200]
      }
    }
    componentDidMount(){


      return fetch('http://45.33.123.173:3000/query/get-history/Org2MSP.'+this.props.navigation.state.params.id)
      .then((response)=> response.json())
      .then((responseJson)=>{
        console.log('meet history',JSON.stringify(responseJson));
        console.log('meet history',responseJson[0]);
        console.log('meet history',responseJson.length);


      })

      .catch((error)=>{
          console.log(error)
      });
    }

    render() {

      const state = this.state;
      const tableData = [];
      for (let i = 0; i < state.myArray.length; i += 1) {
         state.myArray[i].username
        const rowData = [];
        for (let j = 0; j < 9; j += 1) {
          rowData.push(`${state.myArray[i].username}${state.myArray[i].balance}`);
        }
        tableData.push(rowData);
      }

      return (
        <View style={styles1.container}>
          <ScrollView horizontal={true}>
            <View>
              <Table borderStyle={{borderWidth: 1, borderColor: '#C1C0B9'}}>
                <Row data={state.tableHead} widthArr={state.widthArr} style={styles1.header} textStyle={styles.text}/>
              </Table>
              <ScrollView style={styles1.dataWrapper}>
                <Table borderStyle={{borderWidth: 1, borderColor: '#C1C0B9'}}>
                  {
                    tableData.map((rowData, index) => (
                      <Row
                        key={index}
                        data={rowData}
                        widthArr={state.widthArr}
                        style={[styles1.row, index%2 && {backgroundColor: '#F7F6E7'}]}
                        textStyle={styles1.text}
                      />
                    ))
                  }
                </Table>
              </ScrollView>
            </View>
          </ScrollView>
        </View>
      )
    }
  }

  const styles1 = StyleSheet.create({
    container: { flex: 1, padding: 16, paddingTop: 30, backgroundColor: '#fff' },
    header: { height: 50, backgroundColor: '#537791' },
    text: { textAlign: 'center', fontWeight: '100' },
    dataWrapper: { marginTop: -1 },
    row: { height: 40, backgroundColor: '#E7E6E1' }
  });



  SettingsScreen.navigationOptions={  
    tabBarIcon:({tintColor, focused})=>(  
        <Icon  
            name={focused ? 'ios-settings' : 'md-settings'}  
            color={tintColor}  
            size={25}  
        />  
    )  
}  




【问题讨论】:

    标签: react-native react-native-android render


    【解决方案1】:

    因此,正如您所说,您正在获取数据但不在表格中。因为您尚未将数据保存在表格中所需的状态。

    我正在粘贴您编写的相同代码并进行了一些修改。

    import React, {Component} from 'react';
    import { StyleSheet, View, ScrollView} from 'react-native';
    import { Table, Row} from 'react-native-table-component';
    
    class SettingsScreen  extends Component {
      constructor(props) {
        super(props);
        this.state = {
          myArray: [],
          tableHead: ['ID', 'Name', 'Number'],
          widthArr: [40, 100, 80, 100, 120, 140, 160, 180, 200]
        }
      }
    
      componentDidMount(){
        fetch('https://5df47c4bf9e7ae0014801983.mockapi.io/tabletest')
            .then(response=>response.json())
            .then(myArray=>{
              this.setState({myArray})
            })
      }
    
      render() {
        const tableData = this.state.myArray.map(record=>([record.id, record.name, record.mobile]));
            return (
            <View style={styles1.container}>
              <ScrollView horizontal={true}>
                <View>
                  <Table borderStyle={{borderWidth: 1, borderColor: '#C1C0B9'}}>
                    <Row data={this.state.tableHead} widthArr={this.state.widthArr} style={styles1.header} textStyle={styles1.text}/>
                  </Table>
                  <ScrollView style={styles1.dataWrapper}>
                    <Table borderStyle={{borderWidth: 1, borderColor: '#C1C0B9'}}>
                      {
                        tableData.map((rowData, index) => (
                            <Row
                                key={index}
                                data={rowData}
                                widthArr={this.state.widthArr}
                                style={[styles1.row, index%2 && {backgroundColor: '#F7F6E7'}]}
                                textStyle={styles1.text}
                            />
                        ))
                      }
                    </Table>
                  </ScrollView>
                </View>
              </ScrollView>
            </View>
        )
      }
    }
    
    const styles1 = StyleSheet.create({
      container: { flex: 1, padding: 16, paddingTop: 30, backgroundColor: '#fff' },
      header: { height: 50, backgroundColor: '#537791' },
      text: { textAlign: 'center', fontWeight: '100' },
      dataWrapper: { marginTop: -1 },
      row: { height: 40, backgroundColor: '#E7E6E1' }
    });
    
    export default SettingsScreen ;
    

    输出:

    【讨论】:

    • Tnk u..它真的很有帮助
    • 如何添加分页和搜索过滤器
    猜你喜欢
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 2018-04-02
    相关资源
    最近更新 更多