Hi everone i have a issue or more over i want to know "How can i create a tree table kind off structure using the nested json". With out using any 3rd part lib/package
I have a nested json like below :
[
{
"id": 1,
"name": "VehicleData",
"owner": "admin",
"sub_details": [
{
"sub_name": "Lexus",
"sub_id": 4,
},
{
"sub_name": "BMW",
"sub_id": 3,
}
]
},
{
"id": 2,
"name": "Mobiles,
"owner": "admin",
"sub_details": [
{
"sub_name": "Apple",
"sub_id": 2,
}
]
},
{
"id": 3,
"name": "Laptop",
"owner": "admin",
"sub_details": []
}
]
What i have tried :-
import React, {useState, useEffect} from 'react';
import classes from './TableData.module.css';
const TableData = () => {
const [oldData, newData] = useState([]);
useEffect(() => {
fetchData();
}, []);
const Id = (dataId) => {
alert(dataId);
};
const Name = (dataName) => {
alert(dataName);
};
const fetchData = async () => {
try {
const response = await fetch('someapi/json');
const data = await response.json();
newData(data);
} catch (e) {
console.warn(e)
}
}
const DisplayData = oldData.map((data) => {
return (
<tr key={
data.id
}>
<td> {
data.id
} </td>
<td> {
data.name
} </td>
<td> {
data.owner
} </td>
<td>
<button onClick={
() => Id(data.id)
}>getId</button>
<button onClick={
() => Name(data.name)
}>getname</button>
</td>
</tr>
)
})
return (
<div>
<div>
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>owner</th>
</tr>
</thead>
<tbody> {DisplayData} </tbody>
</table>
</div>
</div>
)
}
export default TableData;
what i have done : I have created a table using json and also added button for getting the parent data
what i need and how can i do :
How can i add create the nested structure without using 3rd party plugins and also add button to each row .
How can i get the parent data and child data on each child row button click .
How can i able to toggle & keep open up the nested structure .
if any info url are there it will be great full ?