0

I want to pass the datatable object as parameters to controller in post method in laravel. I have tried but it does not work, please help. Below are the code I have tried.

Datatable Object

 Array
 (
 [draw] => 1
 [columns] => Array
    (
        [0] => Array
            (
                [data] => id
                [name] => 
                [searchable] => true
                [orderable] => true
                [search] => Array
                    (
                        [value] => 
                        [regex] => false
                    )

            )

        [1] => Array
            (
                [data] => name
                [name] => 
                [searchable] => true
                [orderable] => true
                [search] => Array
                    (
                        [value] => 
                        [regex] => false
                    )

            )
    )

[order] => Array
    (
        [0] => Array
            (
                [column] => 0
                [dir] => asc
            )

    )

[start] => 0
[length] => 10
[search] => Array
    (
        [value] => 
        [regex] => false
    )

[_token] => RwkzmLMcy9VW9bzwPN54zv320YsY7Rwbt7sPZCzm

)

Below are the Jquery datatable list works fine

var oTable = $('#dataTables-baselineComplete').dataTable({                          
          "processing": true,
          "serverSide": true,     
          "ajax": {
            "url": "{!!url('admin/baselinereport')!!}",
            "type": "POST",
            "data":{"_token": "{{ csrf_token() }}"},
          },
          "columns": [
                { "data": "id" },
                { "data": "name" },                 
          ],

        });

I want to pass this datatable object to another route function. Please help

Html Input

<input type="button" name="search" id="search"/>

Jquery

$("#search").click(function(){      
 $.ajax({
    url:'{!!url('admin/sample')!!}',
    method:'POST',
    data:{"_token": "{{ csrf_token() }}","datatable_object": oTable},
    success:function(response){
    }
 });

});

Route

 Route::post('admin/sample','SampleController@sampleFn');       

when I click search button - ajax call and post this datatable object as input to that SampleController - sampleFn(). Please help thanks in advance.

1 Answer 1

2

You can use ajax.params() to get the data submitted by DataTables to the server in the last Ajax request.

Because you have initialized your table with dataTable() instead of DataTable(), you have to call this API method as oTable.api().ajax.params().

For example:

$.ajax({
    url:'{!!url('admin/sample')!!}',
    method:'POST',
    data:{
        "_token": "{{ csrf_token() }}",
        "datatable_object": oTable.api().ajax.params()
    },
    success:function(response){
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Yes.. I am got dataTable Object as params..Thank you So much... Gyrocode.
Thank you, this is exactly what I was looking for. The last version uses table.ajax.params() without api().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.