【问题标题】:Weekly pagination in VuejsVuejs 中的每周分页
【发布时间】:2021-08-06 20:29:07
【问题描述】:

我有一个关于 vue 分页的问题。所以,我有一个票对象,你可以在下面看到代码。目前它显示如下内容:

所以我想要实现的是在我的对象中显示所有票。但每周。我的意思是本周是一年中的第 31 周。然后从今天到周日,它应该显示在第一页。但从下周一到周日(第 32 周),当您单击下一步按钮时,它应该会显示在下一页上。 因此,在我的情况下,我可以找到对象的周数,它包括 31、32 和 33 周,并且由于有 3 个不同的周,您可以单击下一个按钮 2 次(因为有 3 个不同的周/页)。我不能做的,分页。如果你能帮我解决这个问题,我会很高兴的。 这是我的组件:

<template>
    <div class="container" id="app">
        <div class="filter input-group mb-3">
        </div>
        <table class="table" v-for="date in dateSets()">
            <thead>
            <tr class="list-heading">
                <td colspan="3">
                    {{ date.getDate() }}/{{ date.getMonth() + 1 }}/{{
                        date.getFullYear()
                    }}
                    {{ setDayOfDate(date.getDay()) }}
                </td>
            </tr>
            </thead>
            <tbody>
            <tr class="list-content" v-for="ticket in findProducts(tickets, date)">
                <!--                <th>{{ (currPage-1) * countOfPage + index + 1 }}</th>-->
                <td>{{ ticket.time }}</td>
                <td>{{ ticket.date }}</td>
                <td>{{ ticket.name }}</td>
            </tr>
            </tbody>
        </table>
        <nav aria-label="Page navigation">
            <ul class="pagination justify-content-center">
                <li
                    class="page-item"
                    v-bind:class="{ disabled: currPage === 1 }"
                    @click.prevent="setPage(currPage - 1)"
                >
                    <a class="page-link" href="">Prev</a>
                </li>
                <li
                    class="page-item"
                    v-bind:class="{ disabled: currPage === weeksSize }"
                    @click.prevent="setPage(currPage + 1)"
                >
                    <a class="page-link" href="">Next</a>
                </li>
            </ul>
        </nav>
    </div>
</template>

<script>
export default {
    data() {
        return {
            currPage: 1,
            filter_name: "",
            tableHeader: "",
            // oldestFirst: false,
            weeks: new Set(),
            tickets: [
                {
                    id: 0,
                    time: "09:45",
                    date: new Date(),
                    name: "Swimming in the hillside pond",
                },
                {
                    id: 1,
                    time: "09:45",
                    date: new Date(),
                    name: "Swimming in the hillside pond",
                },
                {
                    id: 2,
                    time: "09:45",
                    date: new Date("2021-08-06"),
                    name: "Swimming in the hillside pond",
                },
                {
                    id: 3,
                    time: "06:30",
                    date: new Date("2021-08-05"),
                    name: "Schwimmen im Hangeweiher",
                },
                {
                    id: 4,
                    time: "09.45",
                    date: new Date("2021-08-03"),
                    name: "Swimming in the hillside pond",
                },
                {
                    id: 5,
                    time: "09.45",
                    date: new Date("2021-08-07"),
                    name: "Swimming in the hillside pond",
                },
                {
                    id: 6,
                    time: "09.45",
                    date: new Date("2021-08-08"),
                    name: "Swimming in the hillside pond",
                },
                {
                    id: 7,
                    time: "09:45",
                    date: new Date("2021-08-09"),
                    name: "Swimming in the hillside pond",
                },
                {
                    id: 8,
                    time: "09:45",
                    date: new Date("2021-08-10"),
                    name: "Swimming in the hillside pond",
                },
                {
                    id: 9,
                    time: "09:45",
                    date: new Date("2021-08-09"),
                    name: "Swimming in the hillside pond",
                },
                {
                    id: 10,
                    time: "06:30",
                    date: new Date("2021-08-10"),
                    name: "Schwimmen im Hangeweiher",
                },
                {
                    id: 11,
                    time: "09.45",
                    date: new Date("2021-08-12"),
                    name: "Swimming in the hillside pond",
                },
                {
                    id: 12,
                    time: "09.45",
                    date: new Date("2021-08-11"),
                    name: "Swimming in the hillside pond",
                },
                {
                    id: 13,
                    time: "09.45",
                    date: new Date("2021-08-13"),
                    name: "Swimming in the hillside pond",
                },
            ],
        };
    },
    computed: {
        weeksSize() {
            const s = this.getAllWeekNumbers(this.tickets);
            return s.size;
        },
        splitDate: function () {
            let newDate = [...this.list];
            newDate.map((el) => {
                return (el.date = el.date.split(" "));
            });
            return newDate;
        },
    },
    methods: {
        setPage: function (idx) {
            if (idx <= 0 || idx > this.totalPage) {
                return;
            }
            this.currPage = idx;
        },
        currentDate() {
            const current = new Date();
            const date = `${current.getDate()}/${
                current.getMonth() + 1
            }/${current.getFullYear()}`;
            return date;
        },
        dateSets() {
            const today = new Date();
            const sunday = new Date();
            sunday.setDate(sunday.getDate() - sunday.getDay() + 7);
            const dates = [];
            const diff = sunday.getDate() - today.getDate();
            for (let i = 0; i <= diff; i++) {
                const upDate = new Date();
                upDate.setDate(today.getDate() + i);
                dates.push(upDate);
            }
            console.log(dates);
            return dates;
        },
        setDayOfDate(number) {
            if (number === 0) {
                return "Sonntag";
            } else if (number === 1) {
                return "Montag";
            } else if (number === 2) {
                return "Dienstag";
            } else if (number === 3) {
                return "Mittwoch";
            } else if (number === 4) {
                return "Donnerstag";
            } else if (number === 5) {
                return "Freitag";
            } else {
                return "Samstag";
            }
        },
        getAllWeekNumbers(tickets) {
            tickets.forEach(ticket => {
                let result = this.getWeekNumber(ticket.date);
                this.weeks.add(result)
            });
            this.sortSet(this.weeks);
            console.log(this.weeks);
            return this.weeks;
        },
        getWeekNumber(date) {
            const oneJan = new Date(date.getFullYear(), 0, 1);
            const numberOfDays = Math.floor((date - oneJan) / (24 * 60 * 60 * 1000));
            let weekNumber = Math.ceil((date.getDay() + 1 + numberOfDays) / 7);
            return weekNumber;
        },
        sortSet(set) {
            const entries = [];
            for (const member of set) {
                entries.push(member);
            }
            set.clear();
            for (const entry of entries.sort()) {
                set.add(entry);
            }
            return set;
        },
        findProducts(products, date) {
            return products.filter((product) => {
                return (
                    product.date.getDate() === date.getDate() &&
                    product.date.getMonth() === date.getMonth() &&
                    product.date.getFullYear() === date.getFullYear()
                );
            });
        },
    },
};
</script>

我知道这有点长,但我已经为此苦苦挣扎了一段时间。所以请看看并给我一些建议。 谢谢

【问题讨论】:

    标签: javascript vue.js vuejs2 pagination


    【解决方案1】:

    我猜您想在前端执行此操作(通常分页是在 BE 端完成的,因此您会通过 API 收到一系列周数)。

    在这种情况下,我强烈建议使用像 https://date-fns.org/https://momentjs.com/ 这样的库。有了它,您可以对日期进行简单的操作,这样您就可以将您的门票数组集中起来并按周订购,例如..

    import { getWeek } from "date-fns";
    
    let currentWeek = getWeek(tickets[0].date, { weekStartsOn: 1 });
    
    // contains all the 'week'-arrays - represents your pages in the pagination.
    const ticketsClustered = [];
    
    // contains all tickets of one week
    let week = [];
    
    tickets.forEach((ticket) => {
      if (getWeek(ticket.date, { weekStartsOn: 1 }) === currentWeek) {
        week.push(ticket);
      } else {
        ticketsClustered.push(week);
    
        // clear the array and increment the week
        week = [];
        currentWeek++;
      }
    });
    
    ticketsClustered.push(week);
    

    输出将是一个数组,其中包含带有门票本身的数组。这最终将代表您的页面(或几周)。

    由于您的票现在并没有真正按日期排序,您应该对它们进行排序(例如described here 或使用“datefns”)。

    【讨论】:

      猜你喜欢
      • 2011-07-11
      • 2020-12-28
      • 2019-09-03
      • 2021-08-07
      • 1970-01-01
      • 2021-08-04
      • 2021-06-25
      • 1970-01-01
      • 2016-10-24
      相关资源
      最近更新 更多