In this article I will demonstrate one way to create an Ajax pagionation using Laravel and jQuery. In order to understand this post, you need to be familiar with standard (non-Ajax) Laravel’s paginator (how it works), and you should also have a basic knowledge of Ajax and jQuery.
Let’s say that we have a simple Laravel webiste with articles and we want to have Ajax pagination on the page that shows (lists) all of the articles (for example, this page can be accessed fromand it displays paginated lists of article titles where each of these titles is a link):
Let’s say that we have a simple Laravel webiste with articles and we want to have Ajax pagination on the page that shows (lists) all of the articles (for example, this page can be accessed fromand it displays paginated lists of article titles where each of these titles is a link):
First, let’s define a partial view infor displaying pagination results:
Note that there isat the bottom – the Laravel’s links method will render the links to the rest of the pages in the result set. Each of these links will already contain the proper page query string variable. As for the– you’ll see its purpose later.
This partial view will be rendered and returned as a response to a jQuery Ajax Request. Let’s get this straight: when the user clicks on the pagination link (number) – an Ajax request containing URI with the proper page query string variable (page number) will be sent to themethod that is defined in. So, let’s see how thatmethod looks like:
As you can see, in themethod we first use themethod on an Eloquent query:
Themethod provided by Laravel automatically takes care of setting the proper limit and offset based on the current page being viewed by the user. By default, the current page is detected by the value of thequery string argument on the HTTP request. Of course, this value is automatically detected by Laravel, and is also automatically inserted into links generated by the paginator1. Those paginated results are stored in thevariable.
Next, we check if the received request is actually an Ajax request:
If it is, we will pass thevariable to the partial view defined in, render it and return as HTML response.
Finally, we want to make sure that our website is working if JavaScript is disabled; in that case we’ll have a standard (non-Ajax) pagination:
As you can see, we passinto the viewand return it.
Now, let’s define that view located in:
Note that we are including partial viewthat we defined earlier. Now, if we register a resourceful route in(or if you are using Laravel 5.3 in):
we will have a standard (non-Ajax) pagination that is working (you can test it).
The next step is to put the following jQuery just above:
Let’s analyze this script:
– here we attach an event handler function to the “click” event (to the click on the pagination link). And, when a user clicks on the pagination link – the default action of the event will not be triggered [].
Next, we temporarily change the color of the listed titles of articles (which are links) and we append loading.gif image (with an absolute position) inside of(which is inthat we defined earlier).
– this will get the value of the href attribute of the pagination link that was clicked. For example, if the user clicked on the number 2 link – the value of thevariable would be.
Finally, we are passing this URL to thefunction which just sends an HTTP (Ajax) request to that URL. And since we defined a resourceful route in(or if you are using Laravel 5.3 in) – we are actually sending Ajax request to themethod defined in. If everything is ok (if themethod returned rendered HTML response) – we just need to set the HTML contents:If something went wrong – you can do whatever you want, in my case an alert box pops up with the message “Articles could not be loaded”.
Lastly, there isto keep (show) pagination URLs in the address bar so that the users can bookmark or share the links…
So, here is the final version of:
And that’s it. If you know a better or a more elegant way to implement this, please let me know.
This Tutorial Taken from Laraget.Com . Thanks



0 comments: