Fahad AlDaferi Fahad AlDaferi - 26 days ago 48
jQuery Question

Laravel: pagination limit the number using dropdown menu

I have a page which contain a table to display a list of users from the database and i used pagination to display only 10 users per page how i can change the number of users depend on the number selected by dropdown menu for example in the link below the second table contain "Show entries" which needed here. so can i pass the value from selected item to controller or there is a different way to do that ?
https://adminlte.io/themes/AdminLTE/pages/tables/data.html

Answer Source

You will have to make a variable which will hold the pageinate data, suppose you are sending through ajax, use the pageinateData and in your controller you can call something like this:

$users = App\User::paginate($request->pageinateData);

So every time you can call with this data set.

EDIT:

In your controller you can do something like this:

public function pUserList(Request $request) {
    $data = [];
    $data['users'] = App\User::orderBy('id', 'desc')->paginate($request->pageinateData);


    return view('userlist', $data);
}

$this will give you errors. You can use on change in jquery to get the values

$(document).ready(function() {
  $('#issueinput5').on('change', function() {
    alert($('#issueinput5').val());
    $.ajax({
        url:'your url here',
        method:'POST',
        data:{'pageinateData':$('#issueinput5').val(),},
        success:function(d){
         // Do your code...
        }
   });
  });
});

Edit 2:

For the csrf_token you need to do the following: add to your html headers

<meta name="csrf-token" content="{{ csrf_token() }}">

and in ajax call you need to have:

$(document).ready(function() {
  $('#issueinput5').on('change', function() {
    $.ajax({
        url:'/lists/user',
        method:'POST',
        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
        data:{'pageinateData':$('#issueinput5').val(),},
        success:function(d){
          console.log(d)
        }
   });
  });
});

Hope this helps.