【发布时间】:2019-12-09 10:56:31
【问题描述】:
我使用 React 和 axios 从以下位置获取数据: https://data.nasa.gov/resource/gh4g-9sfh.json
我获取 JSON 并将其作为道具传递给 ResultTable 以在 Table 中呈现数据
let formQuery =
"https://data.nasa.gov/resource/gh4g-9sfh.json?$where=UPPER(name)like'" +
this.convertUserInputToQuery() +
"'";`enter code here`
this.setState({
loading: "loading..."
});
axios
.get(formQuery)
.then(data => {
let searchResult = data.data;
console.log(searchResult);
//sets states which renders the result in the ResultTable component
this.setState({
searchResult: searchResult,
loading: "search",
pagination: { current: 1 }
});
console.log(this.state.pagination);
})
.catch(err => {
console.log(err);
this.setState({ searchResult: "error", loading: "search" });
})
.then(data => {
this.setState({ pagination: {} });
});
}
我将在此处使用 Ant Design 将这个日期渲染到表格中:
import React, { Component } from "react";
import { Table } from "antd";
import "antd/dist/antd.css";
const columns = [
{
title: "Name",
dataIndex: "name",
key: "name"
},
{
title: "Fall",
dataIndex: "fall",
key: "fall"
},
{
title: "Mass",
dataIndex: "mass",
key: "mass"
},
{
title: "Year",
dataIndex: "year",
key: "year"
}
];
export class ResultTable extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div id="result-output">
{this.props.searchResult != "error" && (
<Table
className="result-table"
dataSource={this.props.searchResult}
columns={columns}
pagination={this.props.pagination}
/>
)}
{this.props.searchResult == "error"}
</div>
);
}
}
并且在“年份”列中存在额外字符的问题:
如何重写我获取的 JSON 文件中的所有“年份”数据?所以它在没有额外字符的情况下呈现?
【问题讨论】:
标签: javascript json reactjs axios