I am currently consuming a API
, everything is fine but when I want to show the values in a select
I do not print anything.
La Api returns the following:
{
"producto": {
"id": 1,
"codigo": "PRO-0001",
"nombre": "Disco Duro"
},
"precios": [
{
"precio_a": 200,
"precio_b": 300,
"precio_c": 500
}
]
}
I have a "Add Products" button and what it does is add more products to the list by making a request:
function agregarLista(id){
$.ajax({
type: 'GET',
url : 'http://localhost/proyecto/api/agregar-producto/${id}',
}).done(function(data){
var row = '<tr class="item" data-id="${ id }">';
row = row + '<td>${ data.producto.codigo }</td>';
row = row + '<td>${ data.producto.nombre }</td>';
row = row + '</td></tr>';
$("#addList").append(row);
});
}
Everything is going well, but the problem is that in the same row created dynamically I want to add a price selection, I tried done in many ways and one of them is the following code:
row = row + '<td>
<select class="form-control">
${
$.each(data.precios, function(index) {
'<option value="1">Hola mundo</option>'
})
}
</select>
</td>';
As you can see, there are no options created.
I made requests ajax
with selects
static% ie create them in my HTML and put them % id
and point the values of the options there, but in this case the select
is created dynamically.
This is an example when you create a select
and send the options to the respective id
.
<select id="select"></select>
$.each(data,function(key, registro) {
$("#select").append('<option
value='+registro.id+'>'+registro.nombre+'</option>');
});
But unfortunately now it is not the case.