window.PaginationView = Backbone.View.extend({ template: _.template($("#pagination-view").html()), // link: "", // page_count: null, // - page_active: null, // page_show: 5, // - attributes: { // "class": "pagination" }, initialize: function(params) { // 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) { // - nav_begin++; } var nav_end = this.page_active + range;
var left_dots = true; var right_dots = true;
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 }) );
<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>
Source: https://habr.com/ru/post/160651/
All Articles