What is the best way to showcase several cards containing data retrieved in Vue?

I have a component called Beers and another component called BeerDetailList. In the second component, I'm using a Bootstrap card to display image, title, etc. My issue is that I want to display these cards in multiple rows and columns. I've tried a few different approaches but haven't been successful. Can someone please advise me on how to achieve this?

Beers

<template>
  <div>
    <div v-bind:key="beer.id" v-for="beer in beers">
      <BeerDetailsList v-bind:beer="beer" />
    </div>
  </div>
</template>
<script>
import BeerDetailsList from "./BeerDetailsList";
export default {
  components: { BeerDetailsList },
  name: "Beers",
  props: ["beers"],
};
</script>
<style scoped></style>

Beer Details List

<template>
  <div class="container">
    <b-card-group deck>
      <b-card
        :title="beer.name"
        :img-src="beer.image_url"
        :alt="beer.name"
        img-top
        tag="article"
        style="max-width: 20rem ;"
        class="mb-2"
      >
        <b-card-text>
          {{ beer.tagline }}
        </b-card-text>

        <b-button href="#" variant="primary">View Beer details</b-button>
      </b-card>
    </b-card-group>
  </div>
</template>
<script>
export default {
  name: "BeerDetailsList",
  props: ["beer"],
};
</script>
<style scoped></style>

Answer №1

To ensure that each row contains at most 4 cards, place your for loop within the b-card-group component. Next, include another for loop within the b-card component to display 4 cards for each card group.

You do not require a second component. See example code below:

<template>
  <div class="container">
        <b-card-group deck v-for="i in Math.ceil(beers.length/4)" :key="i">
          <b-card
            v-bind:key="beer.id"
            v-for="beer in beers.slice((i-1)*4, (i-1)*4 +  4)"
            :title="beer.name"
            :img-src="beer.image_url"
            :alt="beer.name"
            img-top
            style="max-width: 20rem ;"
            class="mb-2"
          >
            <b-card-text>
              {{ beer.tagline }}
            </b-card-text>
            <b-button href="#" variant="primary">View Beer details</b-button>
            
          </b-card>
        </b-card-group>
      </div>
</template>
<script>
export default {
  name: "Beers",
  props: ["beers"],
};
</script>
<style scoped></style>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Element in the Middle

I have just started learning HTML and CSS, and I'm curious to know how I can center the links Home, About, and Contact in the header. HTML: <body> <header> <h1><a href="#">HBT</a> </h1& ...

Can Vue.js render functions generate an array of VNodes as output?

Currently in the process of expanding a Vue.js frontend application, I find myself analyzing a render function within a functional component. According to my understanding from the documentation, the render function in a functional component should retur ...

Discover the best way to render nested components or slotted content using vueonrails!

In an attempt to integrate Vue.js components into a Ruby-on-Rails application with SSR capabilities, I have decided to utilize Hypernova and the vueonrails helpers. Successfully, I am able to display a registered component using Hypernova in a view (.html ...

Incorporate the Bootstrap classes to arrange the divs side by side, ensuring that any surplus space is utilized effectively

Having implemented a layout with two images and two paragraphs side by side, everything seemed to be working fine until I encountered an issue when setting both images' height to 400px. The goal was for the paragraph to adjust its size based on conten ...

Utilize Bootstrap's custom validation exclusively on fields that are invalid

Exploring Bootstrap features has led me to wonder if there's a straightforward method to change the styling of a field specifically when it is considered invalid. <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" ...

What is the process for embedding a 3D model using three.js into a specific div on a webpage?

Lately, I've been struggling with a persistent issue. I can't seem to figure out how to integrate my three.js scene into a div on my website. Despite watching numerous YouTube videos and reading countless articles on the topic, I still haven&apos ...

Ways to customize parent element when child has a specific class in Vue?

What is the best way to style a parent div if the child has a specific class? I attempted using computed properties with $refs, but I always get an undefined value for $refs.[class] Example <div :style="isChildHasClass" class="container"> < ...

Rendering content on the server side and creating a cached version of the index.html file using Vuejs and Nodejs

Having multiple websites (site1.com, site2.com) connected to a single server poses an interesting challenge. I am able to capture the domain name when a user enters a site, and based on that domain name, I fetch appropriate JSON data from an API to display ...

Discover the step-by-step process of leveraging require.js to define a template similar to a Vue component and establish a

When working on projects that involve express and require.js along with Vue CDN, I often encounter the need to define a template using require.js that is similar to Vue components. In my index.js file, I have a data list that I want to display in the inde ...

Change your favicon dynamically based on the user's online or offline status using Vue

I am currently working on setting up different icons to display when my browser is online (normal logo) and offline (greyed out logo). With Vue JS, I am able to detect the online and offline states, as well as set different favicons accordingly. However, t ...

Achieving Horizontal Alignment in a Dropdown Menu with Bootstrap 4

I am utilizing a dropdown that contains 3 main "categories", each with multiple checkboxes for filtering search results. Below is an example: <div class="dropdown"> <button type="button" class="btn dropdown-toggle" data-toggle="dropdown">B ...

Fixing W3 Validation Errors in your Genesis theme through CSS requires a few simple steps. Let's

As a newcomer to this forum, I kindly ask for understanding regarding my current issue. Upon checking my website vocaloid.de/Wordpress/ using the W3C CSS validator, I discovered several parsing and value errors. In an attempt to solve this, I disabled all ...

Explore a variety of themes for the antd design token

Is there a way to access the text color of both the default and dark themes using design tokens? I am looking for a method to seamlessly switch between the two color schemes based on specific conditions, without having to change the entire theme. For ins ...

Angular toolbar on the left side that hovers seamlessly

1 I am using Angular Material toolbar and trying to position a span inside the toolbar on the left side. I have attempted using CSS float left, but it is not working. Can anyone provide some assistance please? <mat-toolbar> <span>le ...

Guide to center-aligning ElementUI transfer components

Has anyone successfully incorporated ElementUI's transfer feature inside a card element and centered it? https://jsfiddle.net/ca1wmjLx/ Any suggestions on how to achieve this? HTML <script src="//unpkg.com/vue/dist/vue.js"></ ...

Struggling to get my chart to render dynamically in vue-chartjs

In my MainChart.vue file, I defined my chart like this: import { Line, mixins } from 'vue-chartjs' const { reactiveProp } = mixins // const brandPrimary = '#20a8d8' export default { extends: Line, mixins: [reactiveProp], props: [& ...

Comparison of getComputedStyle() and cssText functionality between Internet Explorer and Firefox

Take a look at this example to see the issue in action. I'm attempting to access the cssText property of a <div> using window.getComputedStyle(element) (which provides a CSSStyleDeclaration object). While this works smoothly in Chrome, it' ...

How can I update the chartjs instance?

I am currently working on creating a reactive graph that updates based on certain values. However, I am running into issues where my computed ChartData() function is not updating the component as expected. I have tried using the update() function, but I am ...

Manipulate classes based on scrolling (Wordpress)

Trying to manipulate a widget by adding and removing classes based on user scroll behavior has proven to be quite challenging. Specifically, I aim to add one class when the user scrolls down by 50px and remove it when they scroll back up. Website: Check o ...

Div that scrolls independently without affecting the position of other elements

Visit this link for reference <div class="col-10 content" id="content"> <ul> <li>News</li> <li>News</li> <li>News</li> <li>News</li> < ...