Skip to content

Commit 5c1304c

Browse files
authored
Merge pull request #11 from gilbitron/range-support
Add range support
2 parents 6360c66 + d3ec7f5 commit 5c1304c

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ Vue.component('example-component', {
7777
| Name | Type | Description |
7878
| --- | --- | --- |
7979
| `data` | Object | An object containing the structure of a [Laravel paginator](https://laravel.com/docs/5.4/pagination) response. See below for default value. |
80-
| `limit` | Number | (optional) Limit of pages to be rendered. Default `0` (unlimited links) `-1` will hide numeric pages and leave only arrow navigation. `3` will show 3 previous and 3 next numeric pages from current page. |
80+
| `limit` | Number | (optional) Limit of pages to be rendered. Default `0` (unlimited pages) `-1` will hide numeric pages and leave only arrow navigation. Any positive integer (e.g. `2`) will define how many pages should be shown on either side of the current page when only a range of pages are shown (see below for example output). |
81+
82+
**Default `data`**
8183

8284
```javascript
8385
{
@@ -93,6 +95,10 @@ Vue.component('example-component', {
9395
}
9496
```
9597

98+
**Example `limit`**
99+
100+
![screen shot 2018-01-12 at 4 47 50 pm](https://user-images.githubusercontent.com/203882/34885624-8001513e-f7b8-11e7-9922-236e2b07caa0.png)
101+
96102
### Events
97103

98104
| Name | Description |

src/laravel-vue-pagination.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ module.exports = {
3434

3535
methods: {
3636
selectPage: function(page) {
37+
if (page === '...') {
38+
return;
39+
}
40+
3741
this.$emit('pagination-change-page', page);
3842
},
3943
getPages: function() {
@@ -45,16 +49,31 @@ module.exports = {
4549
return this.data.last_page;
4650
}
4751

48-
var start = this.data.current_page - this.limit,
49-
end = this.data.current_page + this.limit + 1,
52+
var current = this.data.current_page,
53+
last = this.data.last_page,
54+
delta = this.limit,
55+
left = current - delta,
56+
right = current + delta + 1,
57+
range = [],
5058
pages = [],
51-
index;
59+
l;
5260

53-
start = start < 1 ? 1 : start;
54-
end = end >= this.data.last_page ? this.data.last_page + 1 : end;
61+
for (var i = 1; i <= last; i++) {
62+
if (i == 1 || i == last || (i >= left && i < right)) {
63+
range.push(i);
64+
}
65+
}
5566

56-
for (index = start; index < end; index++) {
57-
pages.push(index);
67+
for (var i of range) {
68+
if (l) {
69+
if (i - l === 2) {
70+
pages.push(l + 1);
71+
} else if (i - l !== 1) {
72+
pages.push('...');
73+
}
74+
}
75+
pages.push(i);
76+
l = i;
5877
}
5978

6079
return pages;

tests/laravel-vue-pagination.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,41 @@ describe('LaravelVuePagination', function() {
4141
expect(vm.$el.getElementsByTagName('li')[0].classList).toContain('active');
4242
});
4343

44-
it('has correct DOM structure with 1 link limit on page 3', function() {
45-
exampleData.current_page = 3;
46-
exampleData.next_page_url = 'http://example.com/page/4';
47-
exampleData.prev_page_url = 'http://example.com/page/2';
44+
it('has correct DOM structure with -1 limit on page 2', function() {
45+
exampleData.current_page = 2;
46+
exampleData.next_page_url = 'http://example.com/page/3';
47+
exampleData.prev_page_url = 'http://example.com/page/1';
48+
49+
const vm = getComponent(LaravelVuePagination, {
50+
data: exampleData,
51+
limit: -1
52+
});
53+
54+
expect(vm.$el.nodeName).toBe('UL');
55+
expect(vm.$el.getElementsByTagName('li').length).toBe(2);
56+
});
57+
58+
it('has correct DOM structure with 1 link limit on page 5', function() {
59+
exampleData.current_page = 5;
60+
exampleData.last_page = 11;
61+
exampleData.per_page = 1;
62+
exampleData.next_page_url = 'http://example.com/page/6';
63+
exampleData.prev_page_url = 'http://example.com/page/4';
4864

4965
const vm = getComponent(LaravelVuePagination, {
5066
data: exampleData,
5167
limit: 1
5268
});
5369

5470
expect(vm.$el.nodeName).toBe('UL');
55-
expect(vm.$el.getElementsByTagName('li').length).toBe(5);
56-
expect(vm.$el.getElementsByTagName('li')[2].classList).toContain('active');
71+
expect(vm.$el.getElementsByTagName('li').length).toBe(9);
72+
expect(vm.$el.getElementsByTagName('li')[4].classList).toContain('active');
5773
});
5874

5975
it('has correct DOM structure when on page 2', function() {
6076
exampleData.current_page = 2;
77+
exampleData.last_page = 6;
78+
exampleData.per_page = 2;
6179
exampleData.next_page_url = 'http://example.com/page/3';
6280
exampleData.prev_page_url = 'http://example.com/page/1';
6381

0 commit comments

Comments
 (0)