Go through an array with a JSON field [duplicated]

-2

Good evening in the next picture you see a doubt that I have in something that I am doing all help is welcome received

    
asked by Cristian Villalba 27.11.2018 в 03:49
source

1 answer

-1

You can use the bootstrap carrousel component: enter the description of the link here

In your case it would be something like:

var data = [{
  nombre: 'noticia1',
  fotos: [{id:0,foto:'https://ichef.bbci.co.uk/images/ic/720x405/p0517py6.jpg'},{id:1, foto:'https://images.mentalfloss.com/sites/default/files/styles/mf_image_16x9/public/549585-istock-909106260.jpg?itok=ds7LqH1N&resize=1100x1100'}]
  },
  {
  nombre: 'noticia2',
  fotos: [{id:3,foto:'https://images.mentalfloss.com/sites/default/files/styles/mf_image_16x9/public/549585-istock-909106260.jpg?itok=ds7LqH1N&resize=1100x1100'},{id:4,foto:'https://ichef.bbci.co.uk/images/ic/720x405/p0517py6.jpg'}]
  }
];

var app = new Vue({
  el:'#app',
  data: {
    noticias: data,
  }
});
.carousel{
  height: 200px;
  background-color: black;
  color: white;
}
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body >
  <div id="app">

  <div v-for="(noticia,idex) in noticias" id="fotos">
    <div v-bind:id="'carouselExampleControls' + idex" class="carousel slide" data-ride="carousel">
      <div class="carousel-inner">
        <div v-for="(foto,idx) in noticia.fotos" class="carousel-item" :class="{ active: idx==0 }">
          <img class="d-block w-100" v-bind:src="foto.foto" v-bind:alt="noticia.nombre">
        </div>
      </div>
      <a class="carousel-control-prev" v-bind:href="'#carouselExampleControls' + idex"  role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
      </a>
      <a class="carousel-control-next" v-bind:href="'#carouselExampleControls' + idex"  role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>
  </div>

  </div>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
  </body>
</html>

Here you see that I create a div for each news and for each news we create a carousel with the photos that are being several "carousel-item", in your case it would create a tr for each news item and within the td the carousel, beware with the ids of the forward and backward controls, since I concatenate the array index to make them unique in the document.

I hope you serve, greetings!

    
answered by 27.11.2018 в 05:00