I am describing here how i achieve pagination in my Model Glue apps. I hope the code is modular enough to be reused in other people's apps. You can see a running example here. The code mainly consists of a custom tag (cf_paginationMG) located in the view, and a coldfusion component (Pagination cfc) placed in the model. These both have straightforward APIs that can be easily wired via your controller. In your view, the event that generates the pagination can be labelled anything you like. As long as it calls a function you have defined in your controller that in turn invokes the pagination component. A seperate component is needed in the model that returns to the controller the actual record set that needs to be paged. You can see how i have done this in source code of my example.
This is where the meat and bones of the work is carried out. The code is wrapped in a custom tag (cf_paginationMG) called from a view template. (see my example view code)
The tag outputs the page numbered links (1,2,3,4,5,6... etc) as well as the 'next' 'previous' links which allow a user to page through an archive/record set. Having all the code for this in a custom tag makes it much easier to position the final pagination output above and below the output of your record set.
How to display the actual records / content is left up to you. The values returned from the Pagination component's getFromRecord() and getToRecord() methods dictate what particular records can be displayed for the current request. (see my example view code)
The Pagination component's getNumberOfPages() method returns a value that should be used as the value for the numberOfPages parameter of the custom tag.
cf_paginationMG parameters * are required
- * numberOfpages: The number of pages your archive will contain. On each request the value for this parameter is returned by the model pagination method getNumberOfPages()
- * numberOfRecordsOnPage: Determines the number of records that will be show from the record set/archive on a page at any one time. This value can be set (globally) in a coldspring config bean.
- * event: the model glue event (in the case of my example getArchive) that fires each time a user clicks a numbered page link, or 'next' 'previous' link. This event generates the pagination on each request.
- numberlinkClass: Take a CCS class that will determine how you want the page link numbers to look. Defaults to browser default.
- nextPreviouslinkClass: Take a CCS class that will determine how you want the next previous links to look. Defaults to browser default.
- currentPageNumberClass: Take a CCS class that will determine how you want the page number of the currently being viewed page to be displayed (highlighted). Defaults to browser default.
The value returned from getNumberOfPages() should be used as the value for the numberOfpages parameter in the paginationMG custom tag. On each request from a user, the values returned by getFromRecord() and getToRecord() should be used in the view to help determine what particular records are to be displayed from the record set for that specific request. (see example controller view code)
- currentPageNumber: the number of the page in the archive that is currently being displayed on the view. This defaults to 1.
- recordsInArchive: the total number of records that you want to page through. i.e the record count of the record set (in my example this record set is returned from archive.cfc) to be paged through. Your record set's actual content is output independently of the custom tag using a query loop of the record set returned from archive.cfc, and the fromRecord, toRecord values from returned from the pagination methods getFromRecord() and getToRecord().
- numberOfRecordsOnPage: the number of archive page links that will be displayed at the bottom/top (both) of your archives view
This can be built by you. It should simply return record the records you want to page through. As you can see in the controller code of my example the record set's record count value is used to set the value of the recordsInArchive parameter of the Pagination component.