admin管理员组文章数量:1410717
I'm using the material-table ponent, which I fill with dynamic data ing from Redux (an array of objects), but then I do other things with that data inside my ponent's state. To create column dropdown filters, there's an element inside each column's array of options, lookup
, that receives an object and creates the dropdown based on it's values.
I am extracting some items from my data and putting them inside an element in my ponent's state. This is an object, the same kind that lookup
receives. The thing is that the ponent shows an empty dropdown, as if the object was empty, but it's not. I'm logging it in into the console and the object is filled with the data I need.
I initially thought it was a render problem, that the object is empty at the beggining, and then it's filled with data, but the ponent renders every time.(Yeah, React is reactive).
This is only the code needed to help me solve this problem:
Table ponent
import React, { Component } from "react";
import MaterialTable from "material-table";
class CustomTable extends Component {
state = {
column1: "",
column2: "",
column3: "",
column1FilterList: {}
columns: [
{
title: "Column1",
field: "column1",
editable: "onAdd",
filtering: true,
lookup: { ...this.column1FilterList }
},
{
title: "Column2",
field: "column2",
editable: "onAdd",
filtering: true,
},
{
title: "Column3",
field: "column3",
editable: "onAdd",
filtering: true
}
]
};
ponentDidMount() {
this.props.fetchValues()
this.props.fetchApplications()
this.filterColumn1ExistingKeys()
}
filterColumn1ExistingKeys = () => {
return this.props.elements.map(element => {
return this.setState(prevState => ({
column1FilterList: {
...prevState.column1FilterList,
[element.name]: element.name
}
}))
})
}
render() {
return (
<div>
<MaterialTable
options={{
search: false,
actionsColumnIndex: 4,
filtering: true
}}
title="Search by"
columns={this.state.columns}
data={this.state.data}
/>
</div>
);
}
}
export default CustomTable;
I'm using the material-table ponent, which I fill with dynamic data ing from Redux (an array of objects), but then I do other things with that data inside my ponent's state. To create column dropdown filters, there's an element inside each column's array of options, lookup
, that receives an object and creates the dropdown based on it's values.
I am extracting some items from my data and putting them inside an element in my ponent's state. This is an object, the same kind that lookup
receives. The thing is that the ponent shows an empty dropdown, as if the object was empty, but it's not. I'm logging it in into the console and the object is filled with the data I need.
I initially thought it was a render problem, that the object is empty at the beggining, and then it's filled with data, but the ponent renders every time.(Yeah, React is reactive).
This is only the code needed to help me solve this problem:
Table ponent
import React, { Component } from "react";
import MaterialTable from "material-table";
class CustomTable extends Component {
state = {
column1: "",
column2: "",
column3: "",
column1FilterList: {}
columns: [
{
title: "Column1",
field: "column1",
editable: "onAdd",
filtering: true,
lookup: { ...this.column1FilterList }
},
{
title: "Column2",
field: "column2",
editable: "onAdd",
filtering: true,
},
{
title: "Column3",
field: "column3",
editable: "onAdd",
filtering: true
}
]
};
ponentDidMount() {
this.props.fetchValues()
this.props.fetchApplications()
this.filterColumn1ExistingKeys()
}
filterColumn1ExistingKeys = () => {
return this.props.elements.map(element => {
return this.setState(prevState => ({
column1FilterList: {
...prevState.column1FilterList,
[element.name]: element.name
}
}))
})
}
render() {
return (
<div>
<MaterialTable
options={{
search: false,
actionsColumnIndex: 4,
filtering: true
}}
title="Search by"
columns={this.state.columns}
data={this.state.data}
/>
</div>
);
}
}
export default CustomTable;
Share
Improve this question
edited Aug 2, 2019 at 19:23
Shubham Verma
5,0641 gold badge11 silver badges23 bronze badges
asked Aug 2, 2019 at 19:10
TheWoodStudioTheWoodStudio
2135 silver badges13 bronze badges
6
- Where do you use filterColumn1ExistingKeys function?? – Shubham Verma Commented Aug 2, 2019 at 19:13
-
In
ponentDidMount()
. I forgot to paste it. Now the code is edited. – TheWoodStudio Commented Aug 2, 2019 at 19:18 - Can you put your code in codesandbox. I Still didn't get it. What is CustomTable there is two-ponent you using. How other function work in ponentDidMount?? – Shubham Verma Commented Aug 2, 2019 at 19:21
- I don't think I can do that. It's work related. Sorry about the copy errors. There's a lot of code. Fixed the ponent. – TheWoodStudio Commented Aug 2, 2019 at 19:24
- Its hard to tell from this code. try making small POC and see what's happening on code sandbox. – Shubham Verma Commented Aug 2, 2019 at 19:26
1 Answer
Reset to default 3The problem is how you save that data. You create a new object in the constructor with { ...this.column1FilterList }
. This will create a new object which will act as the lookup object, which is filled with the initial data of column1FilterList (empty). Updating the column1FilterList later does not change that lookup object, because it is disconnected (new object). You have to update the lookup within the columns as well like this:
const filterColumn1ExistingKeys = () => {
const column1FilterList = this.state.column1FilterList;
this.props.elements.forEach(element => column1FilterList[element.name] = element.name)
this.setState({
column1FilterList,
columns: [{
title: "Column1",
field: "column1",
editable: "onAdd",
filtering: true,
lookup: { ...column1FilterList }
},
{
title: "Column2",
field: "column2",
editable: "onAdd",
filtering: true,
},
{
title: "Column3",
field: "column3",
editable: "onAdd",
filtering: true
}
]
})
}
Hope this helps. Let me know, if that works for you. If you have any questions, let me know. Happy coding.
本文标签: javascriptDynamic lookup from React39s state in materialtableStack Overflow
版权声明:本文标题:javascript - Dynamic lookup from React's state in material-table - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745020318a2638092.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论