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
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort
ataşehir escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort
bedava bahis