Laravel 5 AJAX Pagination with JQuery Example – onlinecode
In this post we will give you information about Laravel 5 AJAX Pagination with JQuery Example – onlinecode. Hear we will give you detail about Laravel 5 AJAX Pagination with JQuery Example – onlinecodeAnd how to use it also give you demo for it if it is necessary.
In this tutorial, i will tell you the use of ajax for Laravel Pagination.
Ajax makes your application more flexible, you don’t need to reload or refresh the whole body for small changes, you can made changes in any part without loading page.
Step1: Add Route
- Route::get('laravel-ajax-pagination',array('as'=>'ajax-pagination','uses'=>'FileController@productList'));
Route::get('laravel-ajax-pagination',array('as'=>'ajax-pagination','uses'=>'FileController@productList'));
Step2: FileController
In this step, i am creating a function for product listing and check if there will be ajax request then pass data without templates.
- public functionproductList(Request $request){
- $products= Product::paginate(5);
- if($request->ajax()){
- returnview('presult',compact('products'));
- }
- returnview('productlist',compact('products'));
- }
public function productList(Request $request){ $products = Product::paginate(5); if ($request->ajax()) { return view('presult', compact('products')); } return view('productlist',compact('products')); }
Step3: Product Model
- <?php
- namespace App;
- use IlluminateDatabaseEloquentModel;
- class Product extends Model
- {
- public $fillable=['name','details'];
- }
<?php namespace App; use IlluminateDatabaseEloquentModel; class Product extends Model { public $fillable = ['name','details']; }
Step4: View productlist.blade.php
- <div class="row">
- <div class="col-lg-12 margin-tb">
- <div class="pull-left">
- <h2>Laravel AJAX Pagination with JQuery</h2>
- </div>
- </div>
- </div>
- <div id="product_container">
- @include('presult')
- </div>
- <script>
- $(window).on('hashchange',function(){
- if(window.location.hash){
- var page =window.location.hash.replace('#','');
- if(page ==Number.NaN|| page <=){
- returnfalse;
- }else{
- getData(page);
- }
- }
- });
- $(document).ready(function()
- {
- $(document).on('click','.pagination a',function(event)
- {
- $('li').removeClass('active');
- $(this).parent('li').addClass('active');
- event.preventDefault();
- var myurl = $(this).attr('href');
- var page=$(this).attr('href').split('page=')[1];
- getData(page);
- });
- });
- functiongetData(page){
- $.ajax(
- {
- url:'?page='+ page,
- type:"get",
- datatype:"html",
- // beforeSend: function()
- // {
- // you can show your loader
- // }
- })
- .done(function(data)
- {
- console.log(data);
- $("#product_container").empty().html(data);
- location.hash = page;
- })
- .fail(function(jqXHR, ajaxOptions, thrownError)
- {
- alert('No response from server');
- });
- }
- </script>
<div > <div > <div > <h2>Laravel AJAX Pagination with JQuery</h2> </div> </div> </div> <div id="product_container"> @include('presult') </div> <script> $(window).on('hashchange', function() { if (window.location.hash) { var page = window.location.hash.replace('#', ''); if (page == Number.NaN || page <= 0) { return false; }else{ getData(page); } } }); $(document).ready(function() { $(document).on('click', '.pagination a',function(event) { $('li').removeClass('active'); $(this).parent('li').addClass('active'); event.preventDefault(); var myurl = $(this).attr('href'); var page=$(this).attr('href').split('page=')[1]; getData(page); }); }); function getData(page){ $.ajax( { url: '?page=' + page, type: "get", datatype: "html", // beforeSend: function() // { // you can show your loader // } }) .done(function(data) { console.log(data); $("#product_container").empty().html(data); location.hash = page; }) .fail(function(jqXHR, ajaxOptions, thrownError) { alert('No response from server'); }); } </script>
Step5: presult.blade.php
- <tableclass="table table-bordered">
- <tr>
- <th>Name</th>
- <th>Details</th>
- </tr>
- @foreach ($products as $product)
- <tr>
- <td>{{ $product->name }}</td>
- <td>{{ $product->details }}</td>
- </tr>
- @endforeach
- </table>
- {!! $products->render() !!}
<table > <tr> <th>Name</th> <th>Details</th> </tr> @foreach ($products as $product) <tr> <td>{{ $product->name }}</td> <td>{{ $product->details }}</td> </tr> @endforeach </table> {!! $products->render() !!}
Laravel has make very simple way to render pagination in application.
When i am working with this code then i noticed that url is same because i don’t need to refresh or reload page while working with ajax so its very difficult to understand on which page i am that’s why i used hashes in url and update page number on click.
Now click here to see the demo to use ajax pagination in laravel…
Show Demo
Hope this code and post will helped you for implement Laravel 5 AJAX Pagination with JQuery Example – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs