I need to Paint an array of objects in an option of a Select

0

I need that with the jquery library and the .append tool through a foreach loop the object name appears in an option of a select, I have an array of objects:

[Object [id = 1, attribute2 = example], Object [id = 2, attribute2 = example2]] I have this array and I want it with the .append option to appear in an option of a select using jquery and using the id id tag. Greetings

    
asked by Francisco José Tiscar Romero 24.02.2018 в 17:44
source

2 answers

0

My Object Array is this: [Color [id = 1, CodeColor = Red], Color [id = 2, CodeColor = Green]] and this is my jquery code to go through that array and paint it in an option:             function Color (id, codeColor) {                 this.id = id,                 this.coDigoColor = codeColor             }

        function mostrarColores(){
            $.each(colores, function(color){

                let html = '<option>${color.id}</option>';

                $("#ejemploColor").append(html);
            }); 
        }

        mostrarColores();
  • When I execute it, it says the following: Can not use 'in' operator to search for 'length' in [Color [id = 1, codeColor = Red], Color [id = 2, CodeColor = Green]]
answered by 24.02.2018 в 20:31
0

I recommend that in your controller or servlet in java you establish a list like the following

ArrayList miLista = new ArrayList();
...
request.setAttribute("miLista", miLista);

and already after that in your jsp you run it this way with a foreach, without using JQuery

<select name="database1">
  <c:forEach items="${lista}" var="miLista">
    <option value="${lista}">
        ${lista}
    </option>
  </c:forEach>
</select>    

Greetings

    
answered by 25.02.2018 в 06:31