I have data like below with random number of rows (three level) and I want loop this data to display it like table below
const myData = [
{
subject: "Math",
class: [
{
name: "Math-01",
results: [
{student: "jame",score: "10"},
{student: "kelvin",score: "4"},
{ student: "smith",score: "8"}
]
},
{
name: "Math-02",
results: [
{ student: "joe",score: "8"},
{student: "yassou",score: "8"}
]
}
]
},
{
subject: "History",
class: [
{
name: "History-01",
results: [
{student: "zoe", score: "6"},
{student: "jame", score: "8"},
{student: "bean", score: "9"}
]
},
{
name: "History-02",
results: [
{student: "luffy", score: "10"},
]
}
]
},
{
subject: "literature",
class: [
{
name: "literature-01",
results: [
{student: "nami", score: 5}
]
},
{
name: "literature-02",
results: [
{student: "zoro", score: 3}
]
},
{
name: "literature-03",
results: [
{student: "shank", score: 5}
]
}
]
}
]
I tried with this approach
https://codesandbox.io/s/material-ui-table-demo-with-row-span-dq1w6
but it only works for level 2
here is my code at level 2 and it work
and I continue apply this logic for level 3 and it’s not working properly
can anyone help me with another approach and I want the table to look like the first picture from myData.
(*)Note: myData can have much more data with different number of rows so i would like to be able to build merge cell table dynamically (but it’s still the same format)
thank you so much.
3
Answers
I made this data table using a javascript. You can check and refer to it.
I have 0 experience in reactjs, but I can give you advice on how to structure your database to handle this efficiently. I’ve done this with another Javascript framework in the past which handles tree data.
You are hardcoding your dataset with each level being a different type. The trick here is to soft code your dataset to handle the data dynamically. So if your data changes, your code does not have to change. If you want your dataset truly dynamic, I’d suggest changing your data model to what I have below. And if the database cannot change, you can write a view that queries your current data model to appear like the new data model.
Create a table called node_hierarchy where you have columns parent_id, child_id and node_id. These values are arbitrary and can be anything unique when inserting your data into the database. The PK for this table can be the composite key of both parent_id and child_id. Or you can make a concat_id as the PK which is both strung together and use that single column as the PK rather than two columns (composite). And a second table called node with columns node_id, node_type, and node_value. This way you can add additional types and multiple levels without worrying about changing your source code. The node_id in each table becomes the association, one being the FK in the node table and PK in the node_hierarchy table.
When you query your data, make sure the id of the node is the concatenation of the parent_id + child_id, IF you repeat a node in your tree (hierarchy). Meaning that node has multiple parents. If nodes do not have multiple parents, ignore this. If you do not concatenate the ids, you will have duplicate ids in the tree and may cause an exception when rendering. Often times, client tree ids need to be unique, so verify with reactjs. When building the dataset, add the concat_id to your node in the JSON data, but hide the concat_id when rendering. Use it for reference only.
When querying the data, you can add the node_level, but also hide this in the rendered dataset. The level should not be in the database since this is dynamic based on the number of node_hierarchy records with unique parent_ids. You could even have another render_level, if this is different from the data set level (node_level) in the database.
Example:
Without knowing the type of database you are using, I can’t give you any code to write the recursive queries to pull the dataset. If you are using PostgreSQL for example, then do the following:
Your query on the node_hierarchy (or node_tree if you call it that), should use WITH RECURSIVE to union the ancestors and decendents together into one dataset. The concat_id, node_level and render_level will be dynamic data column values. Join your node table to the node_hierarchy table with an INNER JOIN (node.node_id = node_hierarchy.node_id) to get the static data column values. And the context in which I’m using the words "dynamic" and "static" are based on retrieval of data. This allows your static data (of retrieval) in the database to become dynamic with the context of storage/maintenance of data.
The JSON will look something like so. Realize that the JSON will have variables in the value part of the key/value pairs, but the rendered HTML of the JSON after interpolation will not (and will just have values). Example is this.nodes.node_value. This is just showing where the values are retrieved. Some JavaScript client frameworks allow variables in the JSON interpolation and some do not. Verify with ReactJS. I’m not interpolating the keys here. For example, name could be this.parent_node.node_value (or this.parent_node_value if it exists). Or for example, nodes could be defined based on level. So nodes would be "results" for level 2. So you can interpolate both key or value in some JS client frameworks.
In fact, node_type can be omitted. The node_type will have it’s own node. In the dataset I provided, the node_type will be the node_value of the parent node (ancestor) and the node_value will be the child node (descendent).
Good luck my friend.