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

wlp8pajw  于 2023-05-18  发布在  Vue.js
关注(0)|答案(6)|浏览(155)

以下是我的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的微框架中,如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中是否有类似上述的方法呢?

cpjpxq1n

cpjpxq1n1#

我将使用helper groups array来呈现行中的文章组:

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

Demo:https://codesandbox.io/s/rj60o8l5p

xtfmy6hx

xtfmy6hx2#

我会使用计算属性来将它们分成块。如果您有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>
bvn4nwqk

bvn4nwqk3#

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

c3frrgcw

c3frrgcw4#

使用.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>
gwbalxhn

gwbalxhn5#

在代码中使用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/

eyh26e7m

eyh26e7m6#

对于Vue 3,你可以这样做:
1.安装lodash将数组/对象拆分为块:
npm i lodash
1.导入lodash并在vue组件中使用如下:

<script setup>
    import _ from "lodash";

    const ObjecVariableHere = {
    {name: 'example'},{name: 'example2'},{name: 'example3'},{name: 'example4'},{name: 'example5'},{name: 'example6'},{name: 'example7'},{name: 'example8'},{name: 'example9'},{name: 'example10'},{name: 'example11'},{name: 'example12'},
    };

    const chunk = (objOrArr, numberColumns) => {
        return _.chunk(objOrArr, numberColumns);
    };
</script>
<template>
    <div class="card card-body rounded-lg shadow-none border-light w-100">
        <div v-for="(group,index) in chunk(ObjecVariableHere,4)" :key="index" class="row">
            <div v-for="(item,subindex) in group" class="col-md-3 mb-3">
                <ul>
                    <li>group: {{index}} - item: {{subindex}}</li>
                </ul>
            </div>
        </div>
    </div>
</template>
  • 上面的例子使用了bootstrap设计,但不是必需的。* 如果你点击“运行代码片段”,不会发生任何事情,因为它不受支持,但它应该在你的Vue 3项目中工作。

相关问题