How can I tell react-table
to show a sub-ponent/react-table
for a selected Rows nested Child Object?
For example,
[
{
"objectA":{
"field1":"field1Data",
"nestedObjectA":{
"nestedfield1":"nestedfield1Data",
"nestedNestedObjectA":{
"nestedNestedObjectA":"nestedNestedObjectA",
"nestedNestedObjectB":"nestedNestedObjectB"
}
}
}
}
]
In the json above I will be creating react-table for objectA which will display its fields. Once the user selects objectA row, I want to show a nested table for the selected rows nested data.
In the examples given in react-table docs here or here doesnt contain much plex situations. The main concern here is to tell react-table that I have selected a row and for this selected rows data we have to create a separate table right underneath it. How can I achieve this? Thank you.
How can I tell react-table
to show a sub-ponent/react-table
for a selected Rows nested Child Object?
For example,
[
{
"objectA":{
"field1":"field1Data",
"nestedObjectA":{
"nestedfield1":"nestedfield1Data",
"nestedNestedObjectA":{
"nestedNestedObjectA":"nestedNestedObjectA",
"nestedNestedObjectB":"nestedNestedObjectB"
}
}
}
}
]
In the json above I will be creating react-table for objectA which will display its fields. Once the user selects objectA row, I want to show a nested table for the selected rows nested data.
In the examples given in react-table docs here or here doesnt contain much plex situations. The main concern here is to tell react-table that I have selected a row and for this selected rows data we have to create a separate table right underneath it. How can I achieve this? Thank you.
Share Improve this question edited Aug 22, 2019 at 0:45 Ahmed asked Aug 22, 2019 at 0:34 AhmedAhmed 3,2708 gold badges45 silver badges76 bronze badges1 Answer
Reset to default 2I figured it out myself. But still if anyone else has a more elegant solution please feel free to share.
Added a few lines to the example code in the links shared above. data
and the row from subComponent
have some significance in this.
In short I am calling a map function in data
to check if id
in row.original
from subponent matches the id
in data
item.
In short,
data={rowData => rowData.map(item => {
if (item.id === row.original.id) {
return item.nestedChild;
} else {
// Since an empty array is expected otherwise React-Table will //result in error.
return [];
}
}
rowData is the actual data used as the source of the Nested React-Table. row.original
is the data of the original item which can be used to check if the data matches.
Example,
<ReactTable
data={data}
columns={columns}
defaultPageSize={10}
className="-striped -highlight"
SubComponent={row => {
return (
<div style={{ padding: "20px" }}>
<em>
You can put any ponent you want here, even another React
Table!
</em>
<br />
<br />
<ReactTable
data={rowData =>
rowData.map(item => {
if (item.id === row.original.id) {
return item;
} else {
return [];
}
})
}
columns={columns}
defaultPageSize={3}
showPagination={false}
SubComponent={row => {
return (
<div style={{ padding: "20px" }}>
Another Sub Component!
</div>
);
}}
/>
</div>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745630372a4637071.html
评论列表(0条)