首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用API在ReactTable中创建数据

使用API在ReactTable中创建数据
EN

Stack Overflow用户
提问于 2021-11-29 19:00:35
回答 3查看 35关注 0票数 0

我是React.js新手,我想在这里使用这个API:https://atlas.api.barclays/open-banking/v2.2/branches来创建一个包含巴克莱分支机构数据的表。我的问题是,当我呈现应用程序时,我似乎不能将数据上传到表中,只能上传列的标题。任何帮助都将不胜感激。谢谢!

代码语言:javascript
复制
import React, { Component } from 'react'
import axios from 'axios'
import ReactTable from "react-table-6"; 
import 'react-table-6/react-table.css';

export default class App extends Component {
  constructor(props){
    super(props)
    this.state = {
      branches: [],
      loading:true
    }
  }
  async getBranchesData(){
    const data = await axios.get('https://atlas.api.barclays/open-banking/v2.2/branches')
    console.log(data.Branch)
    this.setState({loading:false, branches: data.Branch})
  }
  componentDidMount(){
    this.getBranchesData()
  }
  render() {
    const columns = [
      {  
      Header: 'Identification',  
      accessor: 'Identification',
     }
     ,{  
      Header: 'Sequence Number',  
      accessor: 'SequenceNumber' ,
      }
     
     ,{  
     Header: 'Name',  
     accessor: 'Name' ,
     }
     ,{  
     Header: 'Type',  
     accessor: 'Type',
     }
     ,{  
     Header: 'Photo',  
     accessor: 'Photo',
     }
     ,{  
     Header: 'Customer Segment',  
     accessor: 'CustomerSegment',
     }
     ,{  
     Header: 'Service and Facility',  
     accessor: 'ServiceAndFacility',
     }
     ,{  
     Header: 'Accessibility',  
     accessor: 'Accessibility',
     }
     ,{  
     Header: 'Other Accessibility',  
     accessor: 'OtherAccessibility',
     }
     ,{  
     Header: 'Other Service and Facility',  
     accessor: 'OtherServiceAndFacility',
     }
     ,{  
     Header: 'Availability',  
     accessor: 'Availability',
     }
     ,{  
     Header: 'Contact Info',  
     accessor: 'ContactInfo',
     }
     ,{  
     Header: 'Postal Address',  
     accessor: 'PostalAddress',
     }
  ]
    return (
     <ReactTable  
      data={this.state.branches}  
      columns={columns} />
    )
  }
} ```
EN

回答 3

Stack Overflow用户

发布于 2021-11-29 19:11:31

更改下一行:

this.setState({loading:false, branches: data.Branch})

为此:

this.setState({loading:false, branches: data.Brand.Branch})

票数 0
EN

Stack Overflow用户

发布于 2021-11-29 19:19:13

问题出在你使用React Table库的方式上。它只提供表实用程序,而不是您试图使用的表组件。您可以在他们的文档中查看此basic usage example,以尝试适合您的用例。

您可以使用HTML表元素或任何UI组件库(如BootstrapMaterial UI)来呈现您的表,但这取决于您。

票数 0
EN

Stack Overflow用户

发布于 2021-11-29 19:55:33

看看api的响应。

代码语言:javascript
复制
const response = {
    meta: {},
    data: [
        {
            "Brand": [
                {
                    "BrandName": "Barclays UK",
                    "Branch": [
                        {
                            "Identification": "UK-B-20533008",
                            // etc
                        },
                        {
                            "Identification": "UK-B-20533008",
                            // etc
                        },
                        // etc
                    ]
                }
            ]
        }
    ]
};

我想你想要data.data.Brand.Branch。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70160009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档