Search results

Populating Items in Vue Carousel component

24 May 2022 / 5 minutes to read

In the Carousel, slides can be rendered in two ways as follows,

  • Populating items using carousel item
  • Populating items using data source

When rendering the Carousel component using items binding, you can assign templates for each item separately or assign a common template to each item. You can also customize the slide transition interval for each item separately. The following example code depicts the functionality as item property binding.

Source
Preview
app.vue
Copied to clipboard
<template>
  <div class="control-container">
<ejs-carousel>
  <e-carousel-items>
    <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/cardinal.png' alt='cardinal' style='height:100%;width:100%;' /><figcaption class='img-caption'>Cardinal</figcaption></figure"></e-carousel-item>
    <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/hunei.png' alt='kingfisher' style='height:100%;width:100%;' /><figcaption class='img-caption'>Kingfisher</figcaption></figure>"></e-carousel-item>
    <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/costa-rica.png' alt='keel-billed-toucan' style='height:100%;width:100%;' /><figcaption class='img-caption'>Keel-billed-toucan</figcaption></figure>"></e-carousel-item>
    <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/kaohsiung.png' alt='yellow-warbler' style='height:100%;width:100%;' /><figcaption class='img-caption'>Yellow-warbler</figcaption></figure>"></e-carousel-item>
    <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/bee-eater.png' alt='bee-eater' style='height:100%;width:100%;' /><figcaption class='img-caption'>Bee-eater</figcaption></figure>"></e-carousel-item>
  </e-carousel-items>
</ejs-carousel>
  </div>
</template>

<script>
  import Vue from "vue";
  import { CarouselPlugin } from "@syncfusion/ej2-vue-navigations";

  Vue.use(CarouselPlugin);

  export default {
data: function () {
  return {};
},
  };
</script>

<style>
  @import "../node_modules/@syncfusion/ej2-base/styles/material.css";
  @import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
  @import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";

  .control-container {
height: 360px;
margin: 0 auto;
width: 600px;
  }

  .img-container {
height: 100%;
margin: 0;
  }

  .img-caption {
color: #fff;
font-size: 1rem;
position: absolute;
bottom: 3rem;
width: 100%;
text-align: center;
  }
</style>

Populating items using data source

When rendering the Carousel component using data binding, you can assign a common template only for all items using the itemTemplate property. You cannot set the interval for each item. The following example code depicts the functionality as data binding.

Source
Preview
app.vue
Copied to clipboard
<template>
  <div class="control-container">
<ejs-carousel :dataSource="productItems" :itemTemplate="productTemplate"></ejs-carousel>
  </div>
</template>

<script>
  import Vue from 'vue';
  import { CarouselPlugin } from "@syncfusion/ej2-vue-navigations";

  Vue.use(CarouselPlugin);

  var productVue = Vue.component("product", {
template: `<figure class="img-container"><img :src="getImage(data.imageName)" :alt="getAlt(data.Name)" style="height:100%;width:100%;" />
  <figcaption class="img-caption">{{data.Name}}</figcaption></figure>`,
data() {
  return {
    data: {},
  };
},
computed: {},
methods: {
  getImage: function (bird) {
    return "https://ej2.syncfusion.com/products/images/carousel/" + bird + ".png";
  },
  getAlt: function (altText) {
    return altText;
  }
},
  });

  export default {
data: function() {
  return {
    productItems: [
      { ID: 1, Name: "Cardinal", imageName: 'cardinal' },
      { ID: 2, Name: "Kingfisher", imageName: 'hunei' },
      { ID: 3, Name: "Keel-billed-toucan", imageName: 'costa-rica' },
      { ID: 4, Name: "Yellow-warbler", imageName: 'kaohsiung' },
      { ID: 5, Name: "Bee-eater", imageName: 'bee-eater' }
    ],
    productTemplate: function (e) {
      return {
        template: productVue
      };
    },
  };
}
  }
</script>

<style>
  @import "../node_modules/@syncfusion/ej2-base/styles/material.css";
  @import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
  @import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";

  .control-container {
height: 360px;
margin: 0 auto;
width: 600px;
  }

  .img-container {
height: 100%;
margin: 0;
  }

  .img-caption {
color: #fff;
font-size: 1rem;
position: absolute;
bottom: 3rem;
width: 100%;
text-align: center;
  }
</style>

Selection

The Carousel items will be populated from the first index of the Carousel items and can be customized using the following ways,

  • Select an item using the property.
  • Select an item using the method.

Select an item using the property

Using the selectedIndex property of the Carousel component, you can set the slide to be populated at the time of initial rendering else you can switch to the particular slide item.

Source
Preview
app.vue
Copied to clipboard
<template>
  <div class="control-container">
<ejs-carousel :selectedIndex="selectedIndex">
  <e-carousel-items>
    <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/cardinal.png' alt='cardinal' style='height:100%;width:100%;' /><figcaption class='img-caption'>Cardinal</figcaption></figure"></e-carousel-item>
    <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/hunei.png' alt='kingfisher' style='height:100%;width:100%;' /><figcaption class='img-caption'>Kingfisher</figcaption></figure>"></e-carousel-item>
    <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/costa-rica.png' alt='keel-billed-toucan' style='height:100%;width:100%;' /><figcaption class='img-caption'>Keel-billed-toucan</figcaption></figure>"></e-carousel-item>
    <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/kaohsiung.png' alt='yellow-warbler' style='height:100%;width:100%;' /><figcaption class='img-caption'>Yellow-warbler</figcaption></figure>"></e-carousel-item>
    <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/bee-eater.png' alt='bee-eater' style='height:100%;width:100%;' /><figcaption class='img-caption'>Bee-eater</figcaption></figure>"></e-carousel-item>
  </e-carousel-items>
</ejs-carousel>
  </div>
</template>

<script>
  import Vue from "vue";
  import { CarouselPlugin } from "@syncfusion/ej2-vue-navigations";

  Vue.use(CarouselPlugin);

  export default {
data: function () {
  return {
    selectedIndex: 3,
  };
},
  };
</script>

<style>
  @import "../node_modules/@syncfusion/ej2-base/styles/material.css";
  @import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
  @import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";

  .control-container {
height: 360px;
margin: 0 auto;
width: 600px;
  }

  .img-container {
height: 100%;
margin: 0;
  }

  .img-caption {
color: #fff;
font-size: 1rem;
position: absolute;
bottom: 3rem;
width: 100%;
text-align: center;
  }
</style>

Select an item using the method

Using the prev or next public method of the Carousel component, you can switch the current populating slide to a previous or next slide.

Source
Preview
app.vue
Copied to clipboard
<template>
  <div>
<ejs-button cssClass="e-info" v-on:click.native="prevBtnClick">Previous</ejs-button>
<ejs-button cssClass="e-info" v-on:click.native="nextBtnClick">Next</ejs-button>
<div class="control-container">
  <ejs-carousel>
    <e-carousel-items>
      <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/cardinal.png' alt='cardinal' style='height:100%;width:100%;' /><figcaption class='img-caption'>Cardinal</figcaption></figure"></e-carousel-item>
      <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/hunei.png' alt='kingfisher' style='height:100%;width:100%;' /><figcaption class='img-caption'>Kingfisher</figcaption></figure>"></e-carousel-item>
      <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/costa-rica.png' alt='keel-billed-toucan' style='height:100%;width:100%;' /><figcaption class='img-caption'>Keel-billed-toucan</figcaption></figure>"></e-carousel-item>
      <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/kaohsiung.png' alt='yellow-warbler' style='height:100%;width:100%;' /><figcaption class='img-caption'>Yellow-warbler</figcaption></figure>"></e-carousel-item>
      <e-carousel-item template="<figure class='img-container'><img src='https://ej2.syncfusion.com/products/images/carousel/bee-eater.png' alt='bee-eater' style='height:100%;width:100%;' /><figcaption class='img-caption'>Bee-eater</figcaption></figure>"></e-carousel-item>
    </e-carousel-items>
  </ejs-carousel>
</div>
  </div>
</template>

<script>
  import Vue from "vue";
  import { CarouselPlugin } from "@syncfusion/ej2-vue-navigations";
  import { ButtonPlugin } from "@syncfusion/ej2-vue-buttons";

  Vue.use(ButtonPlugin);
  Vue.use(CarouselPlugin);

  export default {
data: function () {
  return {};
},
methods: {
  prevBtnClick: function (event) {
    var carouselObj = document.querySelector(".e-carousel").ej2_instances[0];
    carouselObj.prev();
  },
  nextBtnClick: function (event) {
    var carouselObj = document.querySelector(".e-carousel").ej2_instances[0];
    carouselObj.next();
  },
},
  };
</script>

<style>
  @import "../node_modules/@syncfusion/ej2-base/styles/material.css";
  @import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
  @import "../node_modules/@syncfusion/ej2-navigations/styles/material.css";

  .control-container {
height: 360px;
margin: 0 auto;
width: 600px;
  }

  .img-container {
height: 100%;
margin: 0;
  }

  .img-caption {
color: #fff;
font-size: 1rem;
position: absolute;
bottom: 3rem;
width: 100%;
text-align: center;
  }
</style>