A roulette is being developed, the code works correctly with hardcoded code. The code will be shown below:
var totalSponsors = '${sponsors.size()}';
// Create new wheel object specifying the parameters at creation time.
let theWheel = new Winwheel({
'numSegments' : totalSponsors, // Specify number of segments.
'outerRadius' : 212, // Set outer radius so wheel fits inside the background.
'textFontSize' : 18, // Set font size as desired.
'segments' : // Define segments including colour and text.
[
{'fillStyle' : '#eae56f', 'text' : 'Prize 1'},
{'fillStyle' : '#89f26e', 'text' : 'Prize 2'},
{'fillStyle' : '#7de6ef', 'text' : 'Prize 3'},
{'fillStyle' : '#e7706f', 'text' : 'Prize 4'},
{'fillStyle' : '#eae56f', 'text' : 'Prize 5'},
{'fillStyle' : '#89f26e', 'text' : 'Prize 6'},
{'fillStyle' : '#7de6ef', 'text' : 'Prize 7'},
{'fillStyle' : '#e7706f', 'text' : 'Prize 8'},
{'fillStyle' : '#eae56f', 'text' : 'Prize 9'},
{'fillStyle' : '#89f26e', 'text' : 'Prize 10'},
{'fillStyle' : '#7de6ef', 'text' : 'Prize 11'},
{'fillStyle' : '#e7706f', 'text' : 'Prize 12'},
{'fillStyle' : '#eae56f', 'text' : 'Prize 13'},
{'fillStyle' : '#89f26e', 'text' : 'Prize 14'},
{'fillStyle' : '#7de6ef', 'text' : 'Prize 15'},
{'fillStyle' : '#e7706f', 'text' : 'Prize 16'},
{'fillStyle' : '#eae56f', 'text' : 'Prize 17'},
{'fillStyle' : '#89f26e', 'text' : 'Prize 18'}
],
'animation' : // Specify the animation to use.
{
'type' : 'spinToStop',
'duration' : 5, // Duration in seconds.
'spins' : totalSponsors, // Number of complete spins.
'callbackFinished' : alertPrize
}
});
// Insert the sponsor name in the roulette
<c:forEach items='${sponsors}' var="index" varStatus="status">
//console.log("NAME: " + '${index.getSponsorName()}');
</c:forEach>
I want to insert the data from the "sponsors" list (forEach loop) into the "text" field of the "segments" array. The names of each sponsor is obtained with the following line of code:
'${index.getSponsorName()}'
The roulette code has been obtained from the following link:
I believe your best call would be creating the "segments" option as a JSONArray in your controller.
Random random = new Random();
int nextInt = random.nextInt(0xffffff + 1);
JSONArray segments = new JSONArray();
for(Object item : sponsors){
segments.put(new JSONObject("{\"fillStyle\":\""+String.format("#%06x", nextInt)+"\",\"text\":\""+.getSponsorName()+"\"}"));
}
Then segments.toString would give you what you need.
See this link for more: https://www.baeldung.com/java-org-json