Laravel 5 AJAX Pagination with JQuery Example

Laravel 5 AJAX Pagination with JQuery Example

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
  1. 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.

  1. public function productList(Request $request){
  2. $products = Product::paginate(5);
  3. if ($request->ajax()) {
  4. return view('presult', compact('products'));
  5. }
  6. return view('productlist',compact('products'));
  7. }
Step3: Product Model

  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Product extends Model
  5. {
  6. public $fillable = ['name','details'];
  7. }
Step4: View productlist.blade.php

  1. <div class="row">
  2. <div class="col-lg-12 margin-tb">
  3. <div class="pull-left">
  4. <h2>Laravel AJAX Pagination with JQuery</h2>
  5. </div>
  6. </div>
  7. </div>
  8. <div id="product_container">
  9. @include('presult')
  10. </div>
  11. <script>
  12. $(window).on('hashchange', function() {
  13. if (window.location.hash) {
  14. var page = window.location.hash.replace('#', '');
  15. if (page == Number.NaN || page <= 0) {
  16. return false;
  17. }else{
  18. getData(page);
  19. }
  20. }
  21. });
  22. $(document).ready(function()
  23. {
  24. $(document).on('click', '.pagination a',function(event)
  25. {
  26. $('li').removeClass('active');
  27. $(this).parent('li').addClass('active');
  28. event.preventDefault();
  29. var myurl = $(this).attr('href');
  30. var page=$(this).attr('href').split('page=')[1];
  31. getData(page);
  32. });
  33. });
  34. function getData(page){
  35. $.ajax(
  36. {
  37. url: '?page=' + page,
  38. type: "get",
  39. datatype: "html",
  40. // beforeSend: function()
  41. // {
  42. // you can show your loader
  43. // }
  44. })
  45. .done(function(data)
  46. {
  47. console.log(data);
  48. $("#product_container").empty().html(data);
  49. location.hash = page;
  50. })
  51. .fail(function(jqXHR, ajaxOptions, thrownError)
  52. {
  53. alert('No response from server');
  54. });
  55. }
  56. </script>
Step5: presult.blade.php
  1. <table class="table table-bordered">
  2. <tr>
  3. <th>Name</th>
  4. <th>Details</th>
  5. </tr>
  6. @foreach ($products as $product)
  7. <tr>
  8. <td>{{ $product->name }}</td>
  9. <td>{{ $product->details }}</td>
  10. </tr>
  11. @endforeach
  12. </table>
  13. {!! $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.

Phone: (+91) 8800417876
Noida, 201301
sakarya escort sakarya escort sakarya escort sakarya escort serdivan escort izmir escort eporner
akyazı escort sakarya escort ferizli escort sakarya escort geyve escort sakarya escort hendek escort sakarya escort karasu escort sakarya escort sapanca escort serdivan escort webmaster forum ankara travesti
pendik escort atasehir escort