0

I am building a project that has values in a Java HashMap and I would like to place it into my front-end Thymeleaf. Though I looked up some code on their Documentation and Stack Overflow, I can't seem to find a good answer to my question. I have some code below that does not pass the values and I am wondering where I made the mistake at.

So for all the keys, I am trying to make them a optgroup. For the values, I want to make it a option. Any help would be appreciated.

HTML:

<div class="row">
                            <div class="col col-lg-9 search-bar">
                                <div class="form-group">
                                    <label>Type:</label> 
                                    <select class="js-example-basic-single2" id="selectData2">

                                        <optgroup th:each="tierList : ${hmap}"
                                              th:label="${tierList.key}">
                                            <option th:each=" system : ${tierList.value}"
                                                th:value="${system}"
                                                th:selected="${system}"
                                                th:text="${system}">
                                            </option>
                                        </optgroup>
                                    </select>


                                </div>
                            </div>
                        </div>
kane_004
  • 253
  • 3
  • 18
  • First thing to note is that you're trying to reuse the name `hmap`. Once you fix that, you are looking for `item.name` and `item.key`: https://stackoverflow.com/questions/23144358/how-to-loop-through-map-in-thymeleaf – chrylis -cautiouslyoptimistic- Jan 22 '20 at 23:40
  • @chrylis-onstrike- So I made an edit to my code to add the fixes you stated, however it is still not working. Not sure where, I messed up – kane_004 Jan 23 '20 at 00:57
  • Did you see the second half of my comment or the link? – chrylis -cautiouslyoptimistic- Jan 23 '20 at 02:17
  • @chrylis-onstrike- Thanks but It turns out it was a Java related question, I solved it by adding ```modelAndView.addObject("hmap",hmap);``` I originally forgot to tell the object what to name the ```Map``` when it gets passed. Thanks anyways though – kane_004 Jan 23 '20 at 15:30

0 Answers0