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):
– 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 [ ].
– 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 the variable would be .
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 in
for 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 the
method that is defined in . So, let’s see how that method looks like:
As you can see, in the
method we first use the method on an Eloquent query:
The1. Those paginated results are stored in the variable.
method 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 the query 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 paginator
Next, we check if the received request is actually an Ajax request:
If it is, we will pass the
variable 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 pass
into the view and return it.
Now, let’s define that view located in
:
Note that we are including partial view
that 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:
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 in that we defined earlier).
Finally, we are passing this URL to the
function 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 the method defined in . If everything is ok (if the method 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 is
to 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: