Skip to content

Commit

Permalink
Support dynamic perPage
Browse files Browse the repository at this point in the history
  • Loading branch information
TahaSh committed Jun 1, 2016
1 parent 01c5076 commit 63b3417
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 6 deletions.
39 changes: 37 additions & 2 deletions dist/vue-paginate.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ exports.default = {

var vm = this.vm;
this.listName = this.expression;
var perPage = +this.arg;
var perPage = this.getPerPage();
var limit = +this.params.limit;

if (!vm[this.listName]) {
Expand All @@ -271,6 +271,13 @@ exports.default = {
vm['refresh' + _utils2.default.capitalize(_this.listName) + 'Page']();
});

if (this.isPerPageDynamic()) {
vm.$watch(this.arg, function (newVal) {
_this.list.perPage = +newVal <= 0 ? 1 : +newVal;
vm['refresh' + _utils2.default.capitalize(_this.listName) + 'Page']();
});
}

this.list = { currentPage: 0, initial: 0, perPage: perPage };

// Set links array.
Expand Down Expand Up @@ -307,7 +314,7 @@ exports.default = {
};

vm['refresh' + _utils2.default.capitalize(this.listName) + 'Page'] = function () {
vm['change' + _utils2.default.capitalize(_this.listName) + 'Page'](0);
vm['change' + _utils2.default.capitalize(_this.listName) + 'Page'](1);
};

// Turn on warnings back
Expand Down Expand Up @@ -344,6 +351,34 @@ exports.default = {
var links = new _LimitedLinksGenerator2.default(this.vm, this.list, this.listName).generate(limit);

this.vm.$set('limited' + _utils2.default.capitalize(this.listName) + 'Links', links);
},
getPerPage: function getPerPage() {
var vm = this.vm;
var arg = this.arg;
var regex = new RegExp(arg, 'i');

if (!this.isPerPageDynamic()) {
return +arg;
}

if (isDynamicPerPageValid()) {
this.arg = getDynamicArg();
return +vm[this.arg];
}

return 1;

function getDynamicArg() {
return Object.keys(vm.$data).find(function (a) {
return a.match(regex);
});
}
function isDynamicPerPageValid() {
return +vm[getDynamicArg()] > 0;
}
},
isPerPageDynamic: function isPerPageDynamic() {
return !Number.isInteger(Number.parseInt(this.arg));
}
};

Expand Down
2 changes: 1 addition & 1 deletion dist/vue-paginate.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-paginate",
"version": "2.0.0",
"version": "2.1.0",
"description": "A simple vue.js plugin to paginate data",
"main": "src/index.js",
"scripts": {
Expand Down
36 changes: 34 additions & 2 deletions src/paginate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {

var vm = this.vm;
this.listName = this.expression;
var perPage = +this.arg;
var perPage = this.getPerPage();
var limit = +this.params.limit;

if (!vm[this.listName]) {
Expand All @@ -46,6 +46,13 @@ export default {
vm['refresh' + utils.capitalize(this.listName) + 'Page']();
});

if (this.isPerPageDynamic()) {
vm.$watch(this.arg, (newVal) => {
this.list.perPage = +newVal <= 0 ? 1 : +newVal;
vm['refresh' + utils.capitalize(this.listName) + 'Page']();
});
}

this.list = { currentPage: 0, initial: 0, perPage };

// Set links array.
Expand Down Expand Up @@ -86,7 +93,7 @@ export default {
};

vm['refresh' + utils.capitalize(this.listName) + 'Page'] = () => {
vm['change' + utils.capitalize(this.listName) + 'Page'](0);
vm['change' + utils.capitalize(this.listName) + 'Page'](1);
};

// Turn on warnings back
Expand Down Expand Up @@ -134,5 +141,30 @@ export default {
).generate(limit);

this.vm.$set('limited' + utils.capitalize(this.listName) + 'Links', links);
},

getPerPage () {
let vm = this.vm;
let arg = this.arg;
let regex = new RegExp(arg, 'i')

if (! this.isPerPageDynamic()) {
return +arg
}

if (isDynamicPerPageValid()) {
this.arg = getDynamicArg();
return +vm[this.arg];
}

return 1;

function getDynamicArg() { return Object.keys(vm.$data).find(a => a.match(regex)) }
function isDynamicPerPageValid () { return +vm[getDynamicArg()] > 0; }
},

isPerPageDynamic () {
return ! Number.isInteger(Number.parseInt(this.arg));
}

}
27 changes: 27 additions & 0 deletions test/functional/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,33 @@ describe('Vue-Paginate', () => {
vm.$data.limitedLangsLinks.should.eql([1, 2, 3, 4, 5, 6, 7]);
});
});

describe('Dynamic perPage', () => {
it('supports dynamic perPage', () => {
vm = new Vue({
template: '<div v-paginate:perPage="langs">Hello</div>',
data: {
langs: ['PHP', 'JS', 'Ruby', 'Python'],
perPage: 2
}
}).$mount();

vm.$data.langs.should.have.length(2);
});

it('guards agains non-positive perPage', () => {
vm = new Vue({
template: '<div v-paginate:perPage="langs">Hello</div>',
data: {
langs: ['PHP', 'JS', 'Ruby', 'Python'],
perPage: -3
}
}).$mount();

vm.$data.langs.should.have.length(1);
});

});
});

var triggerUpdate = () => vm._directives[0].update(vm.$data.fullLangs);

0 comments on commit 63b3417

Please sign in to comment.