Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Dynamically Create Buttons from JSON

ANSWERED

I have a json that has a list of values that I would like to dynamically create buttons. Any ideas on creating buttons dynamically that call a generic callback with its self as the object, so that I can read the text of the selected button?

 

Thanks,

-Don

Best Answer
1 BEST ANSWER

Accepted Solutions

As for right now, creating elements dynamically is not possible.  The best approach is to define your possibility domain (the max number of buttons you would ever possibly need), and then predefine them as hidden elements within the app with an ordinal identification (something like id="bt001") but set the attributes such that they won't all be rendered (perhaps width="0" height="0").

 

Then, each time you need a new element, you just reference some index value pointing to the next available button element and convert that to the appropriate element identifier ("bt001", "bt002", etc).

 

I had to take this approach for creating a barcode since I could not add rect elements to the UI after the fact. It's a little heavy, but I understand the optimizations they were likely able to make because of it.  (https://github.com/bayssmekanique/fitbit-gym-card)

View best answer in original post

Best Answer
2 REPLIES 2

As for right now, creating elements dynamically is not possible.  The best approach is to define your possibility domain (the max number of buttons you would ever possibly need), and then predefine them as hidden elements within the app with an ordinal identification (something like id="bt001") but set the attributes such that they won't all be rendered (perhaps width="0" height="0").

 

Then, each time you need a new element, you just reference some index value pointing to the next available button element and convert that to the appropriate element identifier ("bt001", "bt002", etc).

 

I had to take this approach for creating a barcode since I could not add rect elements to the UI after the fact. It's a little heavy, but I understand the optimizations they were likely able to make because of it.  (https://github.com/bayssmekanique/fitbit-gym-card)

Best Answer

Thanks so much for the excellent solution!  

 

I tried to add 176 buttons to the app, however, the app kept crashing. After dwindling down the buttons to 21 I am no longer seeing crashes. I plan to add a next page button at the bottom and that should get me what I need. 🙂

Best Answer
0 Votes