【问题标题】:Pagination in backbone js [closed]骨干js中的分页[关闭]
【发布时间】:2017-02-17 22:23:45
【问题描述】:

无论我必须使用主干 js 还是简单的 javascript,我都在使用带有 spring boot 的 rest api 来开发主干 js,在 jsp 页面中加载更多数据或进行分页。我很困惑。请给我这个混乱的建议

【问题讨论】:

  • 这太宽泛了,不适合 Stack Overflow。请阅读how to ask 上的常见问题解答。

标签: javascript backbone.js pagination


【解决方案1】:

将像这样进行分页 1…3 4 5 6 7…12。 为此,我们需要 PaginationView(backbone View) 和 pagination-view(template)

window.PaginationView = Backbone.View.extend({
    template: _.template($("#pagination-view").html()), // template
    link: "", // link
    page_count: null, // number of pages
    page_active: null, // active page
    page_show: 5, // pagination bar items number
    attributes: { // element attributes
        "class": "pagination"
    },
    initialize: function(params) { // constructor
       this.link = params.link;
       this.page_count = params.page_count;
       if (this.page_count <= this.page_show) {
           this.page_show = this.page_count;
       }
       this.page_active = params.page_active;
    },

    render: function(eventName) { // выдача
       ...
    }

});

现在考虑逻辑问题。对于分页的发布,而是阻塞可见页面,我们需要找到这些页面的开始和结束的索引。我们正在寻找活动和之后的元素数量。也就是分成两半。

var range = Math.floor(this.page_show / 2);
var nav_begin = this.page_active - range;
if (this.page_show % 2 == 0) { //If an even number
    nav_begin++;
}
var nav_end = this.page_active + range;

接下来,我们需要知道,否则我们必须在每一边都给出“...”。我们设置了两个变量,会显示是否需要:

var left_dots = true;
var right_dots = true;

所以,当我们需要“...”时,它开始大于 2(左)而小于结尾 - 1(右)。为此,请编写两个检查,分析一个特定情况。它包含这样一个事实,如果我们有两个活动,分配单元就会多一个元素。

1 2 3 4 5 6 ... 12

就在最后。

1 ... 7 8 9 10 11 12

if (nav_begin <= 2) {
    nav_end = this.page_show;
    if (nav_begin == 2) {
         nav_end++;
    } 
    nav_begin = 1;
    left_dots = false;
}

if (nav_end >= this.page_count - 1 ) {
    nav_begin = this.page_count - this.page_show + 1;
    if (nav_end == this.page_count - 1) {
           nav_begin--;
    }
    nav_end = this.page_count;
    right_dots = false;
}

之后需要发送到模板

$(this.el).html( this.template({
    link: this.link,
    page_count: this.page_count,
    page_active: this.page_active,
    nav_begin: nav_begin,
    nav_end: nav_end,
    left_dots: left_dots,
    right_dots: right_dots
}) );

模板分页视图(我用的是twitter bootstrap)

<ul>

  <% if (page_active > 1) { %>
     <li><a href="<%= link %><%= page_active-1 %>">«</a></li>
  <% } %>
  <% if (left_dots) {%>
     <li><a href="<%= link %>1">1</a></li>
     <li class="disabled"><a href="#">...</a></li>
  <% } %>

  <% for (var i = nav_begin; i <= nav_end; i++) { %> 
      <li <% if (page_active == i) print('class="active"') %> ><a href="<%=    link %><%= i %>"><%= i %></a></li> 
 <% } %>

 <% if (right_dots) {%>
     <li class="disabled"><a href="#">...</a></li>
     <li><a href="<%= link %><%= page_count %>"><%= page_count %></a></li>
 <% } %>

 <% if (page_active < page_count) { %>
    <li><a  href="<%= link %><%= page_active+1 %>">»</a></li>
 <% } %>
</ul>

附:很简单,拿来用吧:)。

P.S 在这种情况下使用 git https://gist.github.com/io41/838460(不是我的)

【讨论】:

    猜你喜欢
    • 2011-10-18
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2011-11-26
    相关资源
    最近更新 更多