Monday, February 27, 2017

[SOLVED]how can i create a pagination and select option , changes per page amount at the same time with laravel , ajax?

i tried the simple way with making the select option and send the request through ajax to the controller then i retrieve the collection plus using the laravel built in pagination but it didn't work !!, even i tried to append the result with a partial blade and append it to the previous amount of products but it didn't work when i use the pagination, also i try to add another param to the url in links method ($products->append(Requeset::only('perpage'))->links()) but it didn't work.
//the html
<label>Show</label>
 <select id="selectAmount">
    <option id="tweleve" value="12" selected="selected">12</option>
    <option id="fifteen" value="15">15</option>
    <option id="thirty"  value="30">30</option>
 </select>

 //the default number of products is 12
 <div class="products">
     <ul>
       @foreach($products as $product)
         <li></li> 
         <li></li>
         <li></li>
       @endforeach  
     </ul>
     
 </div>


  // the route
  Route::get('products', 'ProductController@index');



 //the controller
  class ProductController extends Controller
    {
       public function index(Request $request)
       {

        $perpage = $request->input('amount');
        $products = Product::paginate($perpage);

        return view('pages.products', compact('products'));
        }
    }


 //the ajax request
$(document).ready(function(){
   $('#selectAmount').change( function() {

    var selected = $('#selectAmount option:selected');
    var myurl = '/products';

     if(selected.val() <= 30) {
       var amount = selected.val();

       $.ajax({
            type: 'GET', 
            url: myurl,
            data: {amount:amount}
    });
});



from Latest question asked on Laravel tag.


via Mostafa Esmat

Advertisement