javascript - 如何从json文件填充React中ReactTable中被认为是外键的列
问题描述
问题是我已经映射了对象:一对多(例如“类别”和“产品”)以及使用 React 在前端应用程序中管理外键。
后端是 Spring Boot Data Rest。
列如下:
class ListProd extends Component {
constructor(props) {
super(props);
this.state = { prod: []};
}
componentDidMount() {
this.fetchProds();
}
fetchEtuds = () => {
fetch ('http://localhost:8080/' + 'products')
.then((response) => response.json())
.then((responseData) => {
this.setState({
prod: responseData._embedded.products,
});
})
.catch(err => console.error(err));
}
const columns = [{
Header: 'Id',
accessor: 'id',
Cell: this.renderEditable
}, {
Header: 'Code Product',
accessor: 'codep',
}, {
Header: 'Label',
accessor: 'label',
}, {
Header: 'Price',
accessor: 'price',
}, {
id : 'category_id',
Header: 'Category',
accessor: '_links.category.href', // **my problem is here**
...
return (
<div className="App">
<ReactTable data={this.state.prod} columns={columns} filterable={true} pageSize={10}/>
</div>
);
}
}
export default ListProd;
json代码是这样的
{
"_embedded" : {
"products" : [ {
"id" : 1,
"label" : "l1",
"price" : 100,
"_links" : {
"self" : {
"href" : "http://localhost:8080/products/1"
},
"product" : {
"href" : "http://localhost:8080/products/1",
"templated" : true
},
"Category" : {
"href" : "http://localhost:8080/products/1/category"
}
}
}, {
"id" : 2,
所以问题是我没有得到外键值,而是每行的 URL,如下所示:
http://localhost:8080/products/1/category
for the product id=1 女巫返回 json 格式:
{
"id" : 1,
"nameCat" : "C1",
"_links" : {
"self" : {
"href" : "http://localhost:8080/categories/1"
},
"category" : {
"href" : "http://localhost:8080/categories/1"
},
"products" : {
"href" : "http://localhost:8080/categories/1/products",
"templated" : true
}
}
}
如何获取产品 listProd 列表中每个类别的值?
解决方案
推荐阅读
- python - keras.models load_model中的TypeError('关键字参数不理解:','组')
- java - 如何使用 ArrayList 将数据从模型类正确传输到 RecyclerAdapter 类(从 Firebase 实时数据库中检索数据)
- linux - 在汇编 x64 中调用函数后导致段错误的原因
- html - 当我从 index.html 转到 index.html/#contact 时,为什么我的 CSS 会消失?
- python - 我的 Pickle Dump 有问题……有人能解决这个问题吗?
- r - mclapply() 在 32 核系统上仅使用 2 个核
- python - How to live update Matplotlib plot on top of a background image?
- flutter - 如何更改每个颜色栏中文本的颜色?
- c - In this below C code related to pointers and its output, then why doesn't the value a[1] i.e 20 get stored at consecutive address de9?
- python - extract cryptographic key from json keypair