Hi i need some help of how to display data dynamically.i just need someone to explain me the concept. actually I am retrieving some data from database but i dont know how many data there will.it can 2,3 10,50,150,...200 and so on.
in javascript, i will display 10 par page,when the user will click on next button then it will display the next 10 and goes on like this. I am using a javscript library which allow me to bind event on next and previous button. normally if i know how many data exactly, i can already calculate the number of times the user can click on "next" and using for loop i can do the job. but here the scenario is different i dont know how many data will there be. how will i know how many for loop i should defined? any idea? can someone guide me?

4 Years
Discussion Span
Last Post by lambing

If you control what is returned from the database, then you can decide to return the total number of records with every call. What I would do is an Ajax call that has the current page as parameter, and the number of items you want to show. What I would return is either the number of records, or the number of pages, and the requested results.


the database will return me the whole record in an array. according to the length of array i will know how much record in all i have. but i cant understand why i should use ajax?actually i have reduced my complexity. i can have records up to 50. it can be 2 3 15 25...50 not more.


Basically when you click a next button, you pass some parameters (e.g. {:page => 2}) to your backend, then your backend will then do the query on the set of data you are going to display. Let's say per page you display 10 records you do some query like this.

I'm using ruby so never mind my syntax

per_page = 10
page = params[:page]
offset = per_page * page

so your query will be something like this.
Table.limit(10).offset(offset) - What this does is something like select * from table limit 10 offset 20

It will start searching data on the 20th record given the fact that you passed page 2 on your params and will return a set of 10 records.

Then return the data on your client and display the retrieved data. If no more data is returned, then that's the end of your pagination, so you may hide the next button. I hope that helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.