Angular paging component
install npm ngx-paging
Angular version 9.1.5 or up.
-
Show on need
When there is no need for paging at all (for example the items which need to display less than the page size) then theHTML
component will disappear.
This will be established by statementshouldPagingComponentBeHidden
.
you can set the propertyshowComponentAlways
totrue
to always show the component, even if there is neeed for showing the pages. -
Disable on need
for example, if you are already selected the last page, why thelast page
or theNext
button should be available to press?
*I am handling this and in that case I am disabling those buttons by applyingdisabled
class on those buttons` -
Distinguish the Selected page
a special class (in this case calledactive
class) is applied on the selected page, to make the user know in which page he/she is right now.
This is established by the applying theactive
class -
Last & First
going to the first and last page is also available by simple buttons dedicated to this. -
Limits for displayed buttons
Suppose you have a lot of pages, for example 1000 pages, then what will happened? would you display them all for the user ? absolutely not
you have to display just a few of them according to the current page. for example showing 3 pages before the current page and other 3 pages after the selected page.
This case has been handled heregeneratePages
thegeneratePages
function applying a simple algorithm to determine if we need to show all the pages (the page count is under the threshold, which could be determined easily), or to show just some of the buttons.
you can determine the threshold by changing the value of themaxPageCount
variable
Right now I assigned it as the followingmaxPageCount = 7;
which mean that no more than 7 buttons could be displayed for the user (3 before the SelectedPage, and 3 after the Selected Page) and the Selected Page itself.
You may wonder, what if there was not enough pages after OR before the current page to display? do not worry I am handling this in the algorithm
for example, if you have11 pages
and you havemaxPageCount = 7
and the currentselected page is 10
, Then the following pages will be shown
5,6,7,8,9,10(selected page),11
so we always stratifying themaxPageCount
, in the previous example showing5
pages before the selected page and just1
page after the selected page. -
Selected Page Validation
All set operation for thecurrentPage
get property which determine the selected page by the user, is go through the validation.
the validation will check that before setting the value we make sure that we will not go beyond the available pages.
NOTE 1
You may noticed that I am using some bootstrap
classes in this component,
This is suitable for me, but of course you can use your own classes instead of the bootstrap classes.
The bootstrap classes which I used here are pagination
, pagination-sm
, active
and disabled
Feel free to change them as you need.
PRs is more than welcome
No specific style is needed, your code will be refactored anyway, so use the style which you love