Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

osamashaki's avatar

DataTable not showing json response data

I am trying to reload data inside the datatable without refreshing the page, function is returning the data and the data is passed to the datatable but the datatable is not showing the new data. *When I pass the data from a txt file it is working fine.

public function ajaxsalesfilter(Request $request)
{
    $bookings = Booking::join('listings', 'bookings.listing_id', '=', 'listings.id')
        ->get(['bookings.*', 'listings.*', 'bookings.id']); 

    $bookings = $bookings->filter(function ($b) {
        return ($b->is_paid == 1 && !in_array($b->status, [4])) || $b->status == 1;
    });

    $bookings = $bookings->sortByDesc('booking_date');
    
    $a = array();

    foreach($bookings as $b)
    {
        array_push($a, $b);
    }
    echo json_encode($a);
}

the js code:

    $.ajax({
        type:"post",
        cache : false,
        async: false,
        data: form_data,
        url: "/admin/ajax/sales/filter",
        success:function(data)
        {            
            var obj1 = JSON.parse(JSON.stringify(data));
            obj1 = '{ "data": ' + obj1 + '}'; //reformat obj1 to pass to  datatable
            tbl = $('#example1').DataTable({
                ajax: obj1,  ///objects2.txt'
                columns: [
                    { data: 'booking_no'},
                    { data: 'booking_date'},
                    { data: 'shopping_mall_name'},
                    { data: 'lot_no'},
                    { data: 'from_date'},
                    { data: 'to_date'},
                ],
                "responsive": true,
                "autoWidth": false,
                "paging": true,
                "ordering": true,
                "footer": true,
                "destroy": true,
                "order": []
            });
        },
        error:function(data) {
            console.log('error');
        }
    });

Any clue why? Thank you guys

0 likes
5 replies
osamashaki's avatar

@silencebringer thank you for your reply. yes I know that, but this is not a solution for my issue.. I am using ajax request to reload data but something is wrong and the data is not showing in the datatable.

anyway thanks

SilenceBringer's avatar

@osamashaki datatables have a lot of functions, supporting searches, filters and ajax data reloading. Try to spend some time to read aboput it and make it in a "right way" instead of making your way just because "I didn't read the docs or I don't know how it's working". Datatable havs really good docs and you can find a lot of examples and answers about any common question

Sinnbeck's avatar

Don't return all data and paginate everything in Javascript. That is highly ineffective and will slow you page to a crawl as booking grow. Try seeding 10000 records and see what happens

Just laravel pagination along with data sources as suggested by @silencebringer

Please or to participate in this conversation.