Nana-Odai
1 week ago

How to replace part of the view with new result or view using ajax

Posted 1 week ago by Nana-Odai

I want to change the result in the table when any option in the drop down is selected. link to image https://i.stack.imgur.com/0RmPW.png

View:

<div class="tab-content" id="myTabContent">
        @include('invoices.invoice-table')
</div>

Controller:

 $invoices = Invoice::where('slug', $request->slug)->paginate(15);
  $html =  view('invoices.invoice-table', compact('invoices'))->render();

  return response()->json(compact('html'));

Ajax code:

$(function($){
            $(document).on('change', '#select-customer', function(){
                let cust =  $(this).val();
                $.ajax({
                    url: '/invoices/search',
                    type: 'post',
                    dataType:'json',
                    data:{slug:cust, _token:'{{ csrf_token() }}'},
                    success: function(data){
                        $("#myTabContent").html(data.html);
                    }
                });
            })
        })

Please sign in or create an account to participate in this conversation.