vue.js - 如何将对象数组拆分为多个div列

时间:2018-04-26 08:18:56

标签: javascript arrays vue.js vuejs2

这是我的vue布局:

<template lang="pug">
  .row
    .col-4(v-for="article in articles") // need to render 1-3 items here
      | {{ article.name }}
  .row
    .col-4(v-for="article in articles") // need to render 4-6 items here
      | {{ article.name }}
</template>

<script>
export default {
  name: 'Articles',
  data() {
    return {
      articles: [
        { name: 'Article 1' },
        { name: 'Article 2' },
        { name: 'Article 3' },
        { name: 'Article 4' },
        { name: 'Article 5' },
        { name: 'Article 6' },
      ]
    }
  },
}
</script>

目标是:

<div class="row">
  <div class="col-4">article[0].name</div>
  <div class="col-4">article[1].name</div>
  <div class="col-4">article[2].name</div>
</div>

<div class="row">
  <div class="col-4">article[3].name</div>
  <div class="col-4">article[4].name</div>
  <div class="col-4">article[5].name</div>
</div>

在基于Python的Micro Framework(如Flask和Jinja)中,可以这样做:

{% for article_row in articles | batch(3, '&nbsp;') %}
  <div class="row">
    {% for article in article_row %}
    <div class="span4">{{ article }}</div>
    {% endfor %}
  </div>
{% endfor %}

那么,有没有办法在vue.js中执行上述操作?

5 个答案:

答案 0 :(得分:4)

我会使用帮助组数组来呈现行中的文章组:

<template lang="pug">
  .container
    .row(v-for="(group, i) in articleGroups")
      .col-4(v-for="article in articles.slice(i * itemsPerRow, (i + 1) * itemsPerRow)")
        | {{ article.name }}
</template>

<script>
export default {
  name: 'Articles',
  data() {
    return {
      itemsPerRow: 3,
      articles: [
        { name: 'Article 1' },
        { name: 'Article 2' },
        { name: 'Article 3' },
        { name: 'Article 4' },
        { name: 'Article 5' },
        { name: 'Article 6' },
      ]
    }
  },
  computed: {
    articleGroups () {
      return Array.from(Array(Math.ceil(this.articles.length / this.itemsPerRow)).keys())
    }
  },
}
</script>

演示: https://codesandbox.io/s/rj60o8l5p

答案 1 :(得分:3)

v-for="(article,i) in articles"v-if="i>=0 && i<3"

的组合

答案 2 :(得分:2)

我会使用一个计算属性来填充它们。如果您有lodash可用,则可以执行以下操作:

computed: {
  chunked () {
    return _.chunk(this.articles, 3)
  },
},

如果你没有lodash,你可以找到整个地方的分块逻辑。

function chunk (arr, len) {

  const chunks = []
  const i = 0
  const n = arr.length

  while (i < n) {
    chunks.push(arr.slice(i, i += len))
  }

  return chunks
}

然后,你可以这样做:

<div class="row" v-for="chunk in chunked">
  <div class="col-4" v-for="article in chunk">
    {{ article.name }}
  </div>
</div>

答案 3 :(得分:0)

使用.reduce可以将数组分成X组。

<template>
    <div class="container">
        <template v-for="group in groupedArticles">
            <div class="row">
                <span v-for="article in group">
                    {{ article.title }}
                </span>
            </div>
        </template>
    </div>
</template>

<script>
    export default {
        data: function() {
            return {
                articles: [
                    { id: 1, title: 'Hello People' },
                    { id: 2, title: 'What Time is it?' },
                    { id: 3, title: 'Dogs & Cats' }
                ],
                itemsPerRow: 2
            }
        },
        computed: {
            groupedArticles: function() {
                return this.articles.reduce((accumulator, article, index) => {
                    if (index % this.itemsPerRow == 0) {
                        accumulator.push([article]);
                    } else {
                        accumulator[accumulator.length - 1].push(article);
                    } 

                    return accumulator;
                }, []);
            }
        },
    }
</script>

答案 4 :(得分:0)

在代码中使用lodash.chunk()函数的更简单方法是:

<template lang="pug">
  .row(v-for="column in articles")
    .col-4(v-for="article in column") 
      | {{ article.name }}
</template>

<script>
import lodash from "lodash"

export default {
  name: 'Articles',
  data() {
    return {
      articles: lodash.chunk([
        { name: 'Article 1' },
        { name: 'Article 2' },
        { name: 'Article 3' },
        { name: 'Article 4' },
        { name: 'Article 5' },
        { name: 'Article 6' },
      ], 3),
    }
  },
}
</script>

有关lodash.chunk()函数的详细说明,请参见此处:https://dustinpfister.github.io/2017/09/13/lodash-chunk/