0

I filled a dropdown list with data from a database table and I have a second one which I want to display information based on the data displayed in the first dropdown. Is there any way I could do this in SPRING? Or could you tell me any other good way to do this?

These are the dropdown-lists:

<select name="Oras" class="drop-down">
  <option th:each="oras : ${orase}"
          th:text="${oras}"
          th:value="${oras}"></option>
</select>
<select name="Baza sportiva" class="drop-down" path="">
  <option th:each="bazaSportiva : ${bazeSportive}" th:text="${bazaSportiva.nume}"
                    th:value="${bazaSportiva}">
</option>
</select>

I created a controller which decides what data should be displayed in the first dropdown(from the database):

@RequestMapping(value="") public String afisareOrase(Model model){

ArrayList<BazaSportiva> bazeSportive = (ArrayList<BazaSportiva>) bazaSportivaDao.findAll();
ArrayList<String> orase = new ArrayList<String>();
for(BazaSportiva bazaSportiva : bazeSportive){
    String oras = bazaSportiva.getOras();
    if(!orase.contains(oras)){
        orase.add(oras);
    }
}
model.addAttribute("orase", (Iterable) orase);
return "platforma/services";

}

1 Answer 1

1

You need to write a javascript/jquery code in order to send an AJAX request upon the selection of any option within the first drop-down in order to fetch the second set of your data from database.

then after the AJAX response with its data went back to your AJAX function, use another javascript/jquery function to access to the second select tag (using id/class) and fill it up with the acquired data!

Refer to this youtube video tutorial for understand the concept behind this and implement it in your own code -> Tutorial

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.