Home > Software design >  default value for dropdown list using thymeleaf
default value for dropdown list using thymeleaf

Time:06-30

I have a dropdown list, status: having 4 states( Estimated, budgetted, processed, finished). Each unique ID has a possibility to have one of these these 4 status values I use thymeleaf to display all the status values dynamically.

Now the requirement is to open a particular ID, and then update the status. BEfore doing so, it should show the current value of the status( from the db).

I pass the required information from my controller class:

controller.java

StatusTable st = new StatusTable();
List<String>statusValues = st.getAllStatus();
String status = st.getStatus(100); //Herre the status is processed
model.addAttribute("statusdef", statusval)
model.addAttribute("status", statusValues )
return "form.html";

form.html

<html xmlns:th="https://www.thymeleaf.org"
    xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
    <body>
    <div th:fragment="editdata">
        <div >
            <a style="font-weight: bold">Edit the Status</a>
            <div >
                <form>
                    <div >
                        <div >
                            <a>Status</a>
                        </div>
                        <div >
                            <select  aria-label="Default select example"
                                name="status" th:selected ="${statusdef}"  required>
                                <option th:each="statusvalue : ${status}"
                                    th:text="${statusvalue}">
                            </select>
                          </div>
                         </div>
                        </form>
                        </div>
                         </div>




</body>

When I do this, I see that it does not show the default value (processed), it shows the firstvalue (Estimated) I want it to show "processed".. Any suggestions?

CodePudding user response:

You are using th:seleceted at wrong place, it must be with option tag and must evaluate to true, like below -

  <select  aria-label="Default select example"
        name="status"  required>
       <option th:each="statusvalue : ${status}"
            th:selected ="${statusdef==statusvalue}"
            th:text="${statusvalue}">
       </option>
    </select>
  • Related