uchihaabhi's avatar

Laravel pagination using ajax while page load and filtering

I am building a simple database call which paginates the data and displays. But there is also a radio button which on checking fires an ajax and diplays data. I want to paginate that filtered data as well.

Sorry could not use code snippet here

Below is my code.

$(function() { $('body').on('click', '.pagination a', function(e) { e.preventDefault(); $("#divLoading").addClass('show'); var url = $(this).attr('href'); getOrderList(url); window.history.pushState("", "", url); });

function getOrderList(url) {
    $.ajax({
        url : url  
    }).done(function (data) {
        $('#toAppend').html(data);
        $("#divLoading").removeClass('show');  
    }).fail(function () {
        alert('could not be load.');
    });
}

});

$('input[name="filterby"]').on('change', function() {

    var that = $(this);
    var filterby = $('input[name="filterby"]:checked').val();

        $.ajax({

            url: "{{ route('ajax.filter.orders') }}",
            type: 'get',
            data: 'filterby=' + filterby,
            // dataType: 'json',

            beforeSend: function() {

            },
            success: function(response) {
                $('#toAppend').html(response);
            }
        });

    return false;
});

My controller

public function getWebOrders( Request $request ) {

    $response = [];
    $service_pro_id = Auth::guard('service_provider')->user()->service_pro_id;
    



    $order = new Order;
    $orderDetails = $order->pharmacyOrderList($service_pro_id);

    if( $request->ajax() ) {
        return view('serviceprovider.loadpagination', ['orderDetails' => $orderDetails])->render();
    }

    return view('serviceprovider.getorders', ['orderDetails' => $orderDetails]);
    
    }


    public function ajaxFilterOrders( Request $request ) {

        $service_pro_id = Auth::guard('service_provider')->user()->service_pro_id;
        $filterby = $request->filterby;


        if( $filterby == 1 ) {

            $curMonth = date('m');

            $orderDetails = Order::join('customers', 'customers.customer_id', 'orders.customer_id')->select('orders.order_id', 'orders.invoice_no', 'orders.service_pro_id', 'orders.created_at', 'customers.customer_name', DB::raw('(CASE
            WHEN orders.order_status = "1" THEN "Pending"
            WHEN orders.order_status = "2" THEN "Cancelled"
            WHEN orders.order_status = "3" THEN "Cancelled"
            WHEN orders.order_status = "4" THEN "Dispatched"
            WHEN orders.order_status = "5" THEN "Delivered"
            ELSE "Not Processed"
            END) as order_status'))->whereMonth('orders.created_at', '=', $curMonth)->paginate(1);

        }
        return view('serviceprovider.loadpagination', ['orderDetails' => $orderDetails])->render();



    }

The url changes for the radio button ajax and so the earlier function does not work

0 likes
0 replies

Please or to participate in this conversation.