-1
I am trying to display the array products, however, I cannot change the Ids dynamically so that all products are displayed. So far, I can only display 1 product, because the ID is fixed, as shown in the js below.
//CAMISAS
var j = jQuery.noConflict();
j(document).ready(function() {
j(document).on("click", "ul#menu li.nav-item#1", function() {
j.getJSON('/api/V1/categories/1', function(data) {
console.log(data.items);
var myArray = data.items;
//I tried to use For, but that way it only displays 1 product instead of all products in the array
//for (var i = 0; i < myArray.length; i++) {
var insertHtmlShirt = `<div class="card" id="${data.items[2].id}">
<img class="card-img-top" src="${data.items[1].image}" alt=" "/>
<div class="card-body">
<h5 class="card-title">${data.items[1].name}</h5>
<p class="card-text">R$ ${data.items[1].price}</p>
<a href="#" class="btn btn-primary">COMPRAR</a>
</div>
</div>`;
jQuery('div#Product').html(insertHtmlShirt);
//}
});
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-lg-9 col-md-9 col-sm-7">
<div class="container">
<div class="row">
<div id="ItemProd" class="col-lg-3 col-md-3 col-sm-6 col-xs-6 mobile">
<div id="Product"></div>
</div>
</div>
</div>
<br>
<br>
</div>
Friend, I didn’t get it right. But why are you using data.items[2] as div ID and data.item[1] for the other elements ? I already try to uncomment that FOR there and put the variable i as index. For example data.item[i]. id, etc ...
– Gato de Schrödinger
@Jason-Theinternship.So, get a solution here. I made my For dynamic. Soon I share my solution. ;) Thank you for the interaction.
– R.Park
Ball show, R.Park. Then share yes.
– Gato de Schrödinger