Home > Blockchain >  How can I pass multiple drop down lists' value back to controller?
How can I pass multiple drop down lists' value back to controller?

Time:06-26

In part of my spring boot project, there is a page let user to select items in different drop down lists. Since the drop down lists are build with a list of object, I cant pass the selected result with their id as I do in other place. Is there any way or workaround for me to pass all the selected items to my controller?

<div th:each="group : ${groups}">
    <label th:text="${group.name   ': '}"></label>
    <select name="name" id ="id">       
            <option th:each="item: ${group.items}" th:object="${item}" th:value="${item.id}" th:text="${item.name}"></option>
    </select>
    <br>
</div>  

CodePudding user response:

I can give you a hypothetical solution that im using but you will have to implement the rest yourself.

In my templating, I have a dropdown and what I did is added an onchange action. This adds whatever I selected from the dropdown to a hidden input field. Once i'm done adding, I send the data in the text field.

    <select  onchange="addProgrammingLanguages(this, 'dropId', 'langtxt');">
      <option value="Select">Select languages *</option>
      <option value="Java">Java</option>
      <option value="Javascript">Javascript</option>
      <option value="Python">Python</option>
      <option value="C">C</option>
      <option value="C#">C#</option>
    </select>
    <div  id="dropId" style="max-width:80%; border:none; height:auto; display:flex; flex-direction:row; gap:5px; flex-wrap:wrap;">

    </div>
    <input id="langtxt" type="hidden" th:field="*{language}">
function addProgrammingLanguages(menu, ident, txtfield)
{
    var selected = menu.value;

    button.onclick = function() {
        removeMenu(this.id, ident);
        document.getElementById(txtfield).value = document.getElementById(txtfield).value.replace(this.id.split("_")[0] "_",'');
     };


    document.getElementById(txtfield).value  = selected '_';
    menu.selectedIndex=0;
}

In my code i added a _ to all the inputs to in my java I would parse with string.split() method. You dont have to add a _ but in my case I had options that were singular characters and also I makes it easier to split if you add a special character.

Hope this helps.

  • Related