There have been many projects that I’ve done that have required pagination to navigate display assets. It’s a pretty common navigation technique that you can find nearly everywhere you look on the web. The current project I’m working on requires pagination, so I did a quick Google search to see if I could find a decent pagination class in as3. I found a few helpful resources, but to my surprise I didn’t find a class that easily dealt with pagination. So, I present to you, my pagination class.
It’s a pretty easy class to work with, and the first thing you should know is that it does not handle displaying assets on the screen. Rather, this class is designed to just crunch the numbers. It works by passing it an array of assets that will be displayed on the screen. Then, you tell it the maximum number of assets that should be displayed on a page, and it calculates the total number of pages and a start and end index. Using the startIndex and endIndex variables, you would run a for loop to iterate through the assets array, displaying the content of each element. Here’s an example:
import com.shiftf12.utilities.Paginator; var assets:Array = []; for(var i:int = 0; i < 10; i++) { assets.push("asset " + i); } var paginator:Paginator = new Paginator(assets, 3); if(paginator.gotoNextPage()) { _displayAssets(); trace(paginator.toString()); } if(paginator.gotoFirstPage()) { _displayAssets(); trace(paginator.toString()); } if(paginator.gotoLastPage()) { _displayAssets(); trace(paginator.toString()); } if(paginator.gotoPage(3)) { _displayAssets(); trace(paginator.toString()); } function _displayAssets():void { for(var i:int = paginator.startIndex; i <= paginator.endIndex; i++) { this.addChild(assets[i]); } }
As you can see, actually navigating with the class is pretty easy. All navigation methods will return either true or false. Only if a navigation method returns true should you modify what’s displayed on the screen. This way, you’re not wasting resources by unnecessarily refreshing the display, and you’re not left with an empty display when something goes wrong with the startIndex and endIndex calculations (for example, if a number is passed to the gotoPage() method that is out of bounds, or gotoNextPage() is called when you’re already on the last page).
The class purposefully does the minimum that is required. If you need it to have more functionality, simply extend the class. Hopefully someone finds this class as useful as I have. If you have any questions or concerns, or you use this class, let me know in the comments!
Download:

Good job. I needed such class. I wrote a sceleton of the class in the paper and before I started to code I found out your website. Interesting thing is that I designed nearly the same names for functions and properties.