How can I set column in row on the card group bootstrap vue?

11,137

Solution 1

Use CSS to set a max-width for the cards...

.card-group .card {
    max-width: 25%;
}

Demo: https://www.codeply.com/go/DugupIrFxm

If you're using card-deck you need to calculate for the gutter...

.card-deck .card {
    max-width: calc(25% - 30px);
}

Solution 2

Use <b-row> around <b-card-group> and add class="col-..." to the card group. This even allows you to specify different number of columns for the various breakpoints like this: class="col-md-6 col-lg-4 col-xl-3" and it reduces the need of formatting code for your data-collection.

<template>
  ...
  <b-row>
    <b-card-group class="col-md-3" v-for="club in clubs">
        <b-card :title="club.description"
                img-src="http://placehold.it/130?text=No-image"
                img-alt="Img"
                img-top>
            <p class="card-text">
                {{club.price}}
            </p>
            <p class="card-text">
                {{club.country}}
            </p>
            <div slot="footer">
                <b-btn variant="primary" block>Add</b-btn>
            </div>
        </b-card>
    </b-card-group>
  </b-row>
  ...
</template>

<script>
export default {
  data: function () {
    return {
      clubs: [
          {id:1, description:'chelsea', price:1000, country:'england'},
          {id:2, description:'liverpool', price:900, country:'england'},
          {id:3, description:'mu', price:800, country:'england'},
          {id:4, description:'cit', price:700, country:'england'},
          {id:5, description:'arsenal', price:600, country:'england'},
          {id:6, description:'tottenham', price:500, country:'england'},
          {id:7, description:'juventus', price:400, country:'italy'},
          {id:8, description:'madrid', price:300, country:'spain'},
          {id:9, description:'barcelona', price:200, country:'spain'},
          {id:10, description:'psg', price:100, country:'france'}
      ]
    }
  }
}
</script>


Share:
11,137
Success Man
Author by

Success Man

Updated on August 21, 2022

Comments

  • Success Man
    Success Man over 1 year

    My vue component like this :

    <template>
      ...
        <b-card-group deck deck v-for="row in formattedClubs">
            <b-card  v-for="club in row"
                    :title="club.description"
                    img-src="http://placehold.it/130?text=No-image"
                    img-alt="Img"
                    img-top>
                <p class="card-text">
                    {{club.price}}
                </p>
                <p class="card-text">
                    {{club.country}}
                </p>
                <div slot="footer">
                    <b-btn variant="primary" block>Add</b-btn>
                </div>
            </b-card>
        </b-card-group>
      ...
    </template>
    
    <script>
    export default {
      data: function () {
        return {
          clubs: [
              {id:1, description:'chelsea', price:1000, country:'england'},
              {id:2, description:'liverpool', price:900, country:'england'},
              {id:3, description:'mu', price:800, country:'england'},
              {id:4, description:'cit', price:700, country:'england'},
              {id:5, description:'arsenal', price:600, country:'england'},
              {id:6, description:'tottenham', price:500, country:'england'},
              {id:7, description:'juventus', price:400, country:'italy'},
              {id:8, description:'madrid', price:300, country:'spain'},
              {id:9, description:'barcelona', price:200, country:'spain'},
              {id:10, description:'psg', price:100, country:'france'}
          ]
        }
      },
      computed: {
          formattedClubs() {
              return this.clubs.reduce((c, n, i) => {
                  if (i % 4 === 0) c.push([]);
                  c[c.length - 1].push(n);
                  return c;
              }, []);
          }
      }
    }
    </script>
    

    If the script executed, the result like this

    row 1 :

    enter image description here

    row 2 :

    enter image description here

    row 3 :

    enter image description here

    In the row 1 and row 2, the results as I expected. In 1 row exist 4 column

    But the row 3 does not match my expectations. In 1 row only 2 column. There should be 4 columns, 2 columns filled, 2 columns is empty

    How can I solve this problem?

  • moses toh
    moses toh over 5 years
    The results are irregular like this : postimg.cc/JtGkZrSj. Can you give an answer to my case?
  • Zim
    Zim over 5 years
    It would be easier if you isolate the question to the rendered HTML markup instead of Vue components which make the issue difficult to repro. I updated the answer but beyond that I don't know.