Adding pagination - Ramaze/ramaze GitHub Wiki
Pagination will help you display "large" amounts of data (usually database records) in pages. You just display, for instance, 10 items at a time, and have a buttons bar that will let you get the next/previous 10 items, or display the first/last 10 items.
It's quite eay to set-up, it's just 4 lines of code to get started. Here, we're assuming you use Sequel to write an application to manage your audio CDs.
- load the pagination helper in your controller
For instance, if you want to add pagination to your Albums
controler, just add :
class Albums < Ramaze::Controller
#...
helper :paginate
#...
- make your controller method paginate the Sequel results
Assuming Album is your model class :
class Albums < Ramaze::Controller
helper :paginate
#...
def albumlist
@albums = paginate(Album)
end
- load the Sequel 'pagination' extension somewhere
model/init.rb
(if you have one) is a good place to load pagination :
Sequel.extension(:pagination)
- display the navigation bar in your view
Now you just have to iterate over @albums
to fetch the paginated
elements. You should also display the paginator somewhere in your page.
<table>
<!-- this is the table where you display your data -->
<?r @albums.each do |a|
<tr>
<td>#{a.name}</td><td>...</td>
</tr>
<?r end ?>
</table>
<center>#{@albums.navigation}</center>
You're done. No step 5 !