Eli
Eli

Reputation: 1276

How to access json object/data in datatable server side processing in Laravel 5.2?

We're using with datatables on a new project and due to the large amount of data we are working with, we are using the server side processing of datatables.But right now we're trying to figure out how to get all the data from the server through datatables.As I use the Custom HTTP variables server side processing and as I take a look with the data returned, there are no values coming from the database. How could I access the data? Please help. Thanks a lot. Here are my code

javascript:

$('#table-prod-contents').DataTable({
              processing: true,
              serverSide: true,
              ajax: $.fn.dataTable.pipeline( {
                  url: '{{ url("postproductsdata") }}',
                  pages: 6000, // number of pages to cache
                  "data": function ( d ) {
                     console.log(d);
                  }
              } ),
              columns: [
                  {data: 'id', name: 'id'},
                  {data: 'category', name: 'category'},
                  {data: 'pharmaceutical', name: 'pharmaceutical'},
                  {data: 'description', name: 'description'},
                  {data: 'type', name: 'type'},
                  {data: 'unit', name: 'unit'},
                  {data: 'price', name: 'price'},
                  {data: 'created_at', name: 'created_at'},
              ],


          });

The console,log only shows the data shown in the image below:

enter image description here

enter image description here

As I look inside the Object, there is no value of an id. Something like id: 31

Controller.php

public function anyData()
{
    $conditionTxt = "Medical and Lab Supplies";

    $products = Product::where('category', 'ILIKE', '%'.$conditionTxt.'%')
                        ->orderBy('created_at', 'desc')
                        ->get();

    return Datatables::of($products)->make(true);

}

Upvotes: 0

Views: 3273

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58900

Option ajax.data allows to define function to manipulate the data before it's sent to the server.

You could use ajax.dataSrc to get access to data received from the server, however you're also using pipelining which doesn't allow that.

Use either drawCallback option along with ajax.json() API method to get access to retrieved data or createdRow option or xhr.dt event.

For example:

$('#table-prod-contents').DataTable({
    processing: true,
    serverSide: true,
    ajax: $.fn.dataTable.pipeline( {
        url: '{{ url("postproductsdata") }}',
        pages: 6000, // number of pages to cache
        "data": function ( d ) {
           console.log(d);
        }
    } ),
    drawCallback: function(settings){
       var api = this.api();

       console.log(api.ajax.json());
    },
    columns: [
        {data: 'id', name: 'id'},
        {data: 'category', name: 'category'},
        {data: 'pharmaceutical', name: 'pharmaceutical'},
        {data: 'description', name: 'description'},
        {data: 'type', name: 'type'},
        {data: 'unit', name: 'unit'},
        {data: 'price', name: 'price'},
        {data: 'created_at', name: 'created_at'},
    ],
});

Upvotes: 3

Related Questions