Home > database >  How to show pagination using JS List?
How to show pagination using JS List?

Time:12-31

Hello I really need your help I'm new to JAVASCRIPT and I need to implement a pagination alphabetically from a-z I don't know where to start .. I wish I could have a list of first letters found in the result (I mean, if there is no row beginning with 'a', I don't want the 'a' to be display on the pagination links). Is there any kind of plain Javascript pagination exists ?

CodePudding user response:

Lets say you have a list of names instead of rows to paginate. It might be a good idea to group them under their initial character and then set up your pagination accordingly.

 var names = ["aaron", "amber", "bruce", "david", "izzy", "jasmine", "jeniffer", "karen", "lily", "mandy", "marry", "ozzy", "ricky", "sally", "sandy", "tom", "vanesa", "zoe"],
     pages = names.reduce( (ps,n) => ps[n[0].toUpperCase()] ? (ps[n[0].toUpperCase()].push(n), ps)
                                                            : (ps[n[0].toUpperCase()] = [n], ps)
                         , {}
                         );
console.log(pages);

  • Related