代码之家  ›  专栏  ›  技术社区  ›  Success Man

如何在卡组引导vue上设置行中的列?

  •  0
  • Success Man  · 技术社区  · 7 年前

    我的vue组件如下:

    <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>
    

    如果脚本执行,结果如下

    第1行:

    enter image description here

    第2行:

    enter image description here

    第3行:

    enter image description here

    在第1行和第2行中,结果与我预期的一样。一行四列

    我怎样才能解决这个问题?

    1 回复  |  直到 7 年前
        1
  •  3
  •   Carol Skelly    7 年前

    使用CSS设置卡片的最大宽度。。。

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

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

    如果你用的是纸牌组,你需要计算排水沟。。。

    .card-deck .card {
        max-width: calc(25% - 30px);
    }
    
        2
  •  1
  •   Ueli Heiniger    5 年前

    使用 <b-row> <b-card-group> 并添加 class="col-..." 到卡组。这甚至允许您为各种断点指定不同的列数,如下所示: class="col-md-6 col-lg-4 col-xl-3" 它减少了数据收集对格式化代码的需求。

    <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>
    
    
    
    推荐文章