Javascript 如果通过的json没有';我没有内置这些功能
当我查看columns数组的react表文档时,它有一个作为函数的Cell属性。如果我的json来自服务器,我如何实现该单元函数 来自服务器的列的json如下所示:Javascript 如果通过的json没有';我没有内置这些功能,javascript,reactjs,react-table,Javascript,Reactjs,React Table,当我查看columns数组的react表文档时,它有一个作为函数的Cell属性。如果我的json来自服务器,我如何实现该单元函数 来自服务器的列的json如下所示: [ { Header: "name", id: "name" }, { Header: "age", id: "age" } ] 最终结果: [ { Header: "name", id: "name", C
[
{
Header: "name",
id: "name"
},
{
Header: "age",
id: "age"
}
]
最终结果:
[
{
Header: "name",
id: "name",
Cell: props => (
return props.whatever
),
},
{
Header: "age",
id: "age",
Cell: props => (
return props.whatever
),
}
]
更新:
假设你有下面的链接
在这个链接中,他从api调用中获取数据,然后使用它在数据属性中显示。下面是一个硬编码的列数组,具有一些属性。我的问题是,我的columns数组也将来自服务器,那么我如何向传入的columns数组json添加一个cell属性函数呢 您需要在响应数组中显式添加单元格字段,如下所示
AddCell(response) {
let responseArr = [...response];
return responseArr.map((ele, i) => {
let obj = {...ele}
obj.cell = <RowElement />
return obj
})
}
//Please don't do any network call and set state inside constructor because it will re-render all the child component so use componentDidMount to call.
componentDidMout() {
axios.get("https://jsonplaceholder.typicode.com/posts").then(res => {
const updatedData = AddCell(res.data);
// Update react-table
this.setState({
posts: updatedData,
data: updatedData.slice(0, 5),
pages: updatedData.length / 5,
loading: false
});
});
}
AddCell(响应){
让responseArr=[…response];
返回responseArr.map((ele,i)=>{
设obj={…ele}
obj.cell=
返回obj
})
}
//请不要在构造函数中执行任何网络调用和设置状态,因为它将重新呈现所有子组件,所以请使用componentDidMount调用。
组件didmout(){
axios.get(“https://jsonplaceholder.typicode.com/posts)然后(res=>{
const updatedata=AddCell(res.data);
//更新反应表
这是我的国家({
帖子:更新数据,
数据:updateData.slice(0,5),
页码:updatedata.length/5,
加载:错误
});
});
}
Happy Coding friend:)我在哪里调用addcell函数?这是最让我困惑的部分。你在哪里调用api?你能检查一下我在原始帖子中更新的部分吗?