如何从 EJS 模板分页中删除重复页面?

How can I remove the duplicate pages from EJS template pagination?

我发现这个 article 用于在我的 MEN 堆栈应用程序中实现分页功能。这篇文章展示了从前端到后端的一切。一切正常,但我正在尝试稍微调整一下。

文章中的演示应用如下所示:

但我从头到尾替换了 FirstLast,并使用了 110,但现在我有 1 和 10 的副本.像这样:

代码如下:

<% if (pages > 1) { %>

     <ul class="pagination-list">

             <% if (currentPage == 1) { %>

                        <li class="disabled"><a>1</a></li>

             <% } else { %>

                        <li><a href="/1">1</a></li>

             <% } %>


             <% var i = (Number(currentPage) > 5 ? Number(currentPage) - 4 : 1) %>


             <% if (i !== 1) { %>

                        <li class="disabled"><a>...</a></li>

             <% } %>


             <% for (; i <= (Number(currentPage) + 4) && i <= pages; i++) { %>

                  <% if (i == currentPage) { %>

                            <li class="active"><a><%= i %></a></li>

                  <% } else { %>

                            <li><a href="/<%= i %>"><%= i %></a></li>

                  <% } %>

                  <% if (i == Number(currentPage) + 4 && i < pages) { %>

                            <li class="disabled"><a>...</a></li>

                  <% } %>

             <% } %>


             <% if (currentPage == pages) { %>

                     <li class="disabled"><a><%= pages %></a></li>

             <% } else { %>

                     <li><a href="/<%= pages %>"><%= pages %></a></li>

             <% } %>
    </ul>

<% } %>

如何删除重复的?

*** 感谢 Mikhail Evdokimov 提供的教程。 ***

尝试将 for 循环更改为:for (; i <= (Number(currentPage) + 4) && i < pages; i++) {

这应该可以解决您要删除的 10。希望它有效,不确定是否有效,现在无法测试。

如果你不能在 2 小时内让它工作,我可以在我的示例上测试它时帮助你。

编辑:

删除了 10 个。我们只是将“小于或等于”更改为“小于”,这意味着最后一个 for 循环没有执行,因为我们已经处于最后一页的最后一个循环。

现在 1:

var i = (Number(currentPage) > 5 ? Number(currentPage) - 4 : 1)

您应该更改此 i 声明。这段代码告诉我们的是:

If the number of the current page is higher than 5 do:
Number(currentPage) - 4
else
1

并且因为我们在 for 循环中使用 i 变量作为起点,所以我们应该将 1 更改为 2

所以正确的声明应该是:

var i = (Number(currentPage) > 5 ? Number(currentPage) - 4 : 2)

第二次编辑:

现在 ... 问题:

你或许可以将 i !== 1 更改为 i !== 2

应该可以解决,原理是一样的,我们是从2开始for循环的,所以要改成2。我们告诉它:当我们在页面上时不要显示 ...1, 2, 3, 4, 5 现在。