[vue學習] 卡片展示分行功能簡單實現


 

如圖所示,實現簡單的卡片展示分行功能。

分行功能較多地用於展示商品、相冊等,本人在學習的過程中也是常常需要用到這個功能;雖然說現在有很多插件都能實現這個功能,但是自己寫出來,能夠理解原理,相信能夠進步不少。

 

首先看看這個簡單的原理分析:

最核心的就是多維數組,而且是不規則數組。將全部卡片全部分解為多維數組,頁面渲染這個不規則多維數組,最終能達到這樣的效果。

 

自己寫一個商品列表goodsList,它里面的數據如下:

有9個商品,然后在前端渲染時將這十個商品分行,五個一行

 1 goodsList: [
 2                 {
 3                     id: "1",
 4                     name: "1"
 5                 }, {
 6                     id: "2",
 7                     name: "2"
 8                 }, {
 9                     id: "3",
10                     name: "3"
11                 }, {
12                     id: "4",
13                     name: "4"
14                 }, {
15                     id: "5",
16                     name: "5"
17                 }, {
18                     id: "6",
19                     name: "6"
20                 }, {
21                     id: "7",
22                     name: "7"
23                 },{
24                     id: "8",
25                     name: "8"
26                 },{
27                     id: "9",
28                     name: "9"
29                 }
30 ]

 

  • 首先寫一個方法:getRow

  • 聲明一個數組並賦值為空,用於存儲將多維數組。

    let arr = [];

  • 確定行數,使用Math.ceil方法向上取舍(如果有7個商品 ,一行 5 個,7 / 5 = 1.4,向上取舍得2行)

    let row = Math.ceil(this.goodsList.length / 5);

  • 因為我們沒有理由讓商品剛好放滿每一行,有時候我們發現最后一行是不夠5個商品,所以使用不規則數組。使用嵌套循環,將全部商品分割成多個長度上限為5的數組。第一層循環是行數循環,第二層循環是列循環

 1 getRow () {
 2         let arr = [];
 3         let row = Math.ceil(this.goodsList.length / 5);
 4         //  行循環
 5         for (let i = 0; i < row; i++) {
 6           //  第i行的商品
 7           arr[i] = [];
 8           // modLast:最后一行的商品個數
 9           let modLast = this.goodsList.length % 5 === 0 ? 5 : this.goodsList.length % 5;
10           // lastRow:判斷當前循環是不是最后一行,是就讓內層循環最后一行的商品個數,不是則默認5個
11           let lastRow = i === (row - 1) ? modLast : 5;
12           //  列循環
13           for (let j = 0; j < lastRow; j++) {
14             arr[i][j] = this.goodsList[5 * i + j];
15           }
16         }
17     
18         //  最后得到arr數組,將數組賦值給一個變量
19         this.branchData = arr;
20 }

 

 
  • 根據需求觸發 getRow() 執行,我這里是通過Vue生命周期中的mounted,當頁面被掛載后執行函數。

1 mounted () {
2   this.getRow();
3 }

 

  • 最后將分割后的多維數組進行前端遍歷渲染

1 <div class="row" v-for="rows in branchData">
2         <div class="card" v-for="cols in rows">
3              <div class="name">{{cols.name}}</div>
4         </div>
5 </div>

 

 

一個簡單的分行功能在此實現!歡迎指正!謝謝。

 

附錄:

前端代碼:

<template>
    <div class="branch">
        <div class="row" v-for="rows in branchData">
            <div class="card" v-for="cols in rows">
                <div class="name">{{cols.name}}</div>
            </div>
        </div>
    </div>
</template>

 

樣式:

<style scoped>
    .branch {
      width: 800px;
      margin: 0 auto;
    }
​
    .card {
      height: 100px;
      width: 100px;
      margin: 15px;
      background-color: turquoise;
      display: flex;
      justify-content: center;
      align-items: center;
    }
​
    .row {
      display: flex;
      flex-direction: row;
    }
</style>

 

JS:

 1 <script>
 2 export default {
 3     name: "Branch",
 4     data() {
 5         return {
 6             goodsList: [
 7                 {
 8                     id: "1",
 9                     name: "1"
10                 }, {
11                     id: "2",
12                     name: "2"
13                 }, {
14                     id: "3",
15                     name: "3"
16                 }, {
17                     id: "4",
18                     name: "4"
19                 }, {
20                     id: "5",
21                     name: "5"
22                 }, {
23                     id: "6",
24                     name: "6"
25                 }, {
26                     id: "7",
27                     name: "7"
28                 },{
29                     id: "8",
30                     name: "8"
31                 },{
32                     id: "9",
33                     name: "9"
34                 }
35             ],
36             branchData: []
37         }
38     },
39     mounted () {
40         this.getRow();
41     },
42     methods: {
43       getRow () {
44         let arr = [];
45         let row = Math.ceil(this.goodsList.length / 5);
46         for (let i = 0; i < row; i++) {
47           arr[i] = [];
48           let modLast = this.goodsList.length % 5 === 0 ? 5 : this.goodsList.length % 5;
49           let lastRow = i === (row - 1) ? modLast : 5;
50           for (let j = 0; j < lastRow; j++) {
51                  arr[i][j] = this.goodsList[5 * i + j];
52           }
53         }
54         this.branchData = arr;
55       }
56    }
57 }
58 </script>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM