Onchange in the dropdown, dist_nm should be reflected in the text box.. Can someone help?
<select id="editDistName" th:field="*{districts}" th:onchange="selectedDistName();">
<option value="default">Select the District </option>
<option th:each="dist : ${districts}" th:value="${dist.dist_id}"
th:text="${dist.dist_nm}" th:selected="${dist.dist_nm}"/>
</select>
<input type="text" class="form-control" id="dist_nm" name="dist_nm"/>
Finally, I found a way to fetch the name through table by passing ID from Dropdown.
HTML - form
<div class="form-group">
<select id='DCode' name='DCode'>
<option th:each="dist : ${districts}"
th:value="${dist.dist_id}"
th:text="${dropDownItem.dist_nm}" />
</select>
</div>
<div class="form-group">
<div id="textBox"></div>
</div>
Repository -- fetching name through ID
@Query("SELECT name FROM Sample WHERE id =:ID ORDER BY id")
public String getName(@Param("ID") int code);
AjaxController
@RequestMapping(value="Ajax",method={ RequestMethod.GET, RequestMethod.POST })
public @ResponseBody String editName(@RequestParam(value="ID") int d_Code ) {
String name = rep.getName(d_Code);
return name;
}
JS-file /passing ID to get Name from table/
$(document).on("change", "#DCode", function() {
try {
var d_id= $("#DCode option:selected").val();
var params = {ID : d_id};
$.ajax({
type:'POST',
url:'/Ajax',
data:params,
success:function(responseJson){
var jSON_res = JSON.stringify(responseJson);
var d_Name = jSON_res.replace(/\"/g, "" ); //remove double quote from string
var text_val = "<input type='text' id ='new_name' name ='new_name' " +
" value =' "+d_Name+" ' />";
$("#textBox").empty().append(text_val);
}//success function
});
} catch(err) {
alert("Exception ::-- "+err);
}
});
I'm very new to spring boot and this is one of the way I tried. Let me know, if there is better way of getting the result. Thank you,