【问题标题】:Datatable object passed as parameters on button click按钮单击时作为参数传递的数据表对象
【发布时间】:2017-03-08 08:50:41
【问题描述】:

我想在 laravel 的 post 方法中将数据表对象作为参数传递给控制器​​。我试过了,但它不起作用,请帮助。以下是我尝试过的代码。

数据表对象

 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

)

以下是 Jquery 数据表列表工作正常

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" },                 
          ],

        });

我想将此数据表对象传递给另一个路由函数。请帮忙

HTML 输入

<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::post('admin/sample','SampleController@sampleFn');       

当我单击搜索按钮时 - ajax 调用并将此数据表对象作为输入发布到 SampleController - sampleFn()。请帮助提前谢谢。

【问题讨论】:

    标签: php ajax laravel datatables


    【解决方案1】:

    您可以使用ajax.params()获取上次Ajax请求中DataTables提交给服务器的数据。

    由于您已使用dataTable() 而不是DataTable() 初始化表,因此您必须将此API 方法调用为oTable.api().ajax.params()

    例如:

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

    【讨论】:

    • 是的..我有dataTable对象作为参数..非常感谢... Gyrocode。
    • 谢谢,这正是我想要的。最后一个版本使用table.ajax.params(),不带api()。
    猜你喜欢
    • 1970-01-01
    • 2022-08-03
    • 2021-01-20
    • 2016-12-27
    • 2021-12-27
    • 1970-01-01
    • 2018-04-17
    • 2017-02-03
    • 1970-01-01
    相关资源
    最近更新 更多