Connor Bishop Connor Bishop - 4 months ago 8
Ajax Question

Moving through database records on a single webpage

I basically have a table which needs to be filled with data from rows in my database. I have a next and previous button to move through the different records in my database. I will also have a filter.

Now the way I see it I have two options, run a query sever side to get all the rows from the the table in my database and send all of that to browser, then use javascript/jquery to navigate through these records. Or I only ever send one record at a time to the browser and use ajax requests to get the next/previous records.

What are the advantages/disadvantages of these two approaches? Also Im using laravel if anyone knows of an easy way to do this with lavarel's built in features.

Answer

For basic browsing you should use pagination - it's really easy to implement in laravel. Quick examle

$posts = App\Post::paginate(15);

$posts contains now the url to the next page ( 15 entires per page ) and also the data for the first 15 results. Laravel will also provide nice helpers in blade to generate the page buttons.

However, you will only be able to filter on the currently visible 15 entires - therefore you should create a "second table" which will always show the filtered results. You could run an ajax call to query these results for your given filters. Then fill the second table with the results and hide the first one.

When the filter is removed simply hide the second table and show your default table again.

Here's more to read: https://laravel.com/docs/5.2/pagination

Comments