Pawan Kumar
Pawan Kumar

Reputation: 1601

How to populate select element in thymeleaf springboot?

I am writing a web application using thymeleaf, springboot. I have one model class with id, name, address, state and district fields. I have 3 tables user table, state table with id, state_name, state_code, and district table with id, district_code, district_name, state_code.

How to map List of states to user table state field?.

Q1. When I load my newrecord view it should fatch all record from state table and populate in select element?.

Q2. When I select state from select fatch all dstrict list of that state?.

Q3. When I open same record in edit mode state and district list show its default value?.

Controller

@RequestMapping("user/new")
public String newUser(Model model){
    model.addAttribute("user", new User());
    return "userform";
}

@RequestMapping("user/edit/{id}")
public String update(@PathVariable Integer id, Model model){
    model.addAttribute("user", userService.getProductById(id));
    return "userform";
}

Model

@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String address;
private String state;
private String dist;
//getter setter
}

public class State {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer state_id;
private String state_name;
private String state_code;
}

Thymeleaf view

<html xmlns:th="http://www.thymeleaf.org">
<head lang="en"> 
</head>
<body>
  <div class="container">
  <h2>User Details</h2>
<div>
    <form class="form-horizontal" th:object="${user}" th:action="@{/user}" method="post">
        <input type="hidden" th:field="*{id}"/>            
        <div class="form-group">
            <label class="col-sm-2 control-label">Name:</label>
            <div class="col-sm-10">
             <input type="text" class="form-control" th:field="*{name}"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">Address:</label>
            <div class="col-sm-10">
            <input type="text" class="form-control" th:field="*{address}"/>
            </div>
        </div>
**//NOTE:- These select list not working I am able to display above information not this list**
        *<div class="form-group">
            <label class="col-sm-2 control-label">State:</label>
            <div class="col-sm-10">
              <select class="form-control" name="selectState" th:field="*{state}" >
            <option th:each="sopt:${state}" th:value="?" th:text="?">State</option>
             </select>
            </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">Dist:</label>
            <div class="col-sm-10">
              <select class="form-control" name="selectDistrict" th:field="*{dist}" >
            <option th:each="sopt:${dist}" th:value="?" th:text="?">Dist</option>
             </select>
            </div>
        </div>*
        <div class="row">
            <button type="submit" class="btn btn-default">Submit</button>
        </div>
    </form>
</div>
</div> 
</body>
</html>

Upvotes: 2

Views: 4214

Answers (1)

Pawan Kumar
Pawan Kumar

Reputation: 1601

For Q1 i got solution by using What is @ModelAttribute in Spring MVC?

@ModelAttribute("states")
public List<State> populateStates() {
    List<State> listStates = new ArrayList<State>();
    for (State e : stateService.listAllState()) {
        System.out.println(e.getState_code());
        listStates.add(e);
    }
    return listStates;
}

Q3 problem also solved by this when you open it in edit mode it will select your saved value

<select th:field="*{states}" id="state" onChange="onstatechange(this)" class="infobox">
    <option value="0">Select Your State</option>
    <option th:each="state : ${states}" th:value="${state.state_id}" th:text="${state.state_name}"></option>
</select>

For Q2 you can use ajax call or javascript onChange can solve my problem.

Upvotes: 1

Related Questions