TanStackT
TanStack3y ago
6 replies
progressive-amaranth

Creating dynamic columns from API response

Hello! I have a problem that is driving me crazy and I wasn't able to find a solution in the docs.

Let me explain it:

An endpoint returns me an array of objects with the following shape:

https://pastebin.com/hGMQ18S5

I have attached an image of what I'm trying to achieve (the styling is not a problem).

As you can see, the key of the objects in the top level is used to name the first header, and then, a new column is created for every object in the array, where
name
is the second header and
amount
is the cell.

Now, the real problem is that I can't hardcode any of these columns, since the API will return a different amount of objects in the arrays depending on filter, dates, etc, so I need to be able to generate these columns dynamically.

Here's what I've tried so far and got ALMOST working, with the exception of the header:

https://pastebin.com/vLZDyKPq

As you can see, this is kind of a mess since I'm using the real object (reports), which I know will always have hours and supplements, but I don't know about their content. I'm also nos using methods like accessorFn because I don't know how to use them in this case.

Any help will be greatly appreciated, thank you very much in advance!
Screenshot_2023-09-29_at_4.45.50_PM.png
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Was this page helpful?