0

I have simple pagination function that outputs array.

exports.pagination = function(currentPage, nrOfPages) {
 var delta = 1,
 range = [],
 rangeWithDots = [],
 l;
 range.push(1); 
 if (nrOfPages <= 1){
 return range;
 }
 for (let i = currentPage - delta; i <= currentPage + delta; i++) {
 if (i < nrOfPages && i > 1) {
 range.push(i);
 }
 } 
 range.push(nrOfPages);
 for (let i of range) {
 if (l) {
 if (i - l == 2) {
 rangeWithDots.push(l + 1);
 } else if (i - l !== 1) {
 rangeWithDots.push('...');
 }
 }
 rangeWithDots.push(i);
 l = i;
 }
 return rangeWithDots;
}

handlebars:

{{#each pagination}}
 <li class="paginator-item">
 <a href="{{this}}" class="paginator-itemLink">{{this}}</a>
 </li>
{{/each}}

output:

1 ... 7 8 9 ... 19

It works well. But what i want to do is to seperate "number" and "href" so i can use

{{#each pagination}}
 <li class="paginator-item">
 <a href="{{this.href}}" class="paginator-itemLink">{{this.number}}</a>
 </li>
 {{/each}}

But dont know how can i do this but unable. Cant find the correct way. rangeWithDots.push(number:'...',href:''). Tried lot of things but cant find that one correct

asked Dec 9, 2018 at 15:20

2 Answers 2

1

you can push as an object which consist of multiple properties.

So from the array you can read by accessing the object and the corresponding property.

i hope this will solve the issue.

var rangeWithDots = []
rangeWithDots.push({href: "/sample", number: 5})
console.log(rangeWithDots)
//you can access the number by
console.log("number", rangeWithDots[0].number)

answered Dec 9, 2018 at 15:25
Sign up to request clarification or add additional context in comments.

Comments

0

You’re almost right! You just need to wrap your parameter to push as an object using {...}:

rangeWithDots.push({ number: ..., href: '...' });
answered Dec 9, 2018 at 15:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.