I need to create tabs dynamically based on data fetch from a dB so the number of tabs may change according to the data. The Tabs example (reported below) would be just fine:
but I don’t understand how I can modify it so that tabs are added dynamically to the UX markup. For example let’s say I have the following data fetched and exported:
This works fine as the tabs name correspond to the fetch data datas, but the problem I don’t know how to solve is how to change dynamically the strings page1Tab, page2Tab, page3Tab and the page1, page2, page3 so that the tabs may react correctly on the clicked event.
I guess I have to use templates but reading the docs and trying many times did not helped me.
Thank you Uldis.
Your post helped me a lot but although I’m spending a lot of time on the code I’m still stuck on a part of my issue.
Your post works as I wish but now I’m trying to modify it so that it reacts to some data which I fetch from an API. The data has the following structure and I would like to give to the tabs the names cinema and shows while the other data would be the content of the page inside a ScrollView. The strings cinema and shows are dynamic and I can’t “hard code” them. They must be fetch from the API.
The problem is that I don’t understand how to set the tab name as said (set correctly the Observable and data-bind it).
Below I took your post and I changed the structure of the Observable pages. I’m trying to set the Tabs name so that match the strings cinema and shows. As said these strings should not be hardcoded as pages here represent data fetched from an API so the name of the tabs and the number of the tabs may change. The other data (title and subtitle) represent the content of the page.
I guess a part of the question was still if it’s possible to access the key names in UX. A direct answer to that is a “no”. Enrico, you should look into refactoring that pages Observable to be a list instead of a single-value Observable, otherwise the Each simply won’t work.
Yes Uldis, you are right. I was trying to acces the key name in UX. As I’m fetching data from a dB with an API I made by myself I can change it easily. Do you have any suggestion how to refactor pages (which would be the result of fetch)?
It should be as easy as reading about Observable.replaceAll and other Observable list functions. If your API would return a JSON array, you could directly call pages.replaceAll(result) with the API response.