jQuery 绘制前向数据表ajax调用添加参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28906515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:33:03  来源:igfitidea点击:

Add parameter to datatable ajax call before draw

jquerydatatables

提问by bstras21

I am using DataTables 1.10

我正在使用数据表 1.10

Does anyone know how to dynamically add a parameter to the ajax call before table.draw() so my request has new parameters? I've looked everywhere and cannot find an answer.

有谁知道如何在 table.draw() 之前向 ajax 调用动态添加参数,以便我的请求有新参数?我到处找,找不到答案。

I have buttons that a person can press and based on that button send different parameters to the server.

我有一个人可以按下的按钮,并根据该按钮向服务器发送不同的参数。

$('#mytable').DataTable({
        iDisplayLength: 10,
        responsive: true,
        processing: true,
        serverSide: true,
        searching: false,
        bLengthChange: false,
        bProcessing: true,
        paging: true,
         ajax: {
            url: me.url,
            dataType: 'json',
            cache:false,
            type: 'GET',
            data: function ( d ) {
                $.extend( d, me.data);
                d.supersearch = $('.my-filter').val();
            }
        },
        columns: me.columns,
        columnDefs: me.renderer,
        initComplete: function() {

        }
    });

This all works fine but then I try to tie it to a button to pass new parameters.

这一切正常,但随后我尝试将其绑定到一个按钮以传递新参数。

$('.button').on('click', function(){
      var table  = $('#mytable').DataTable();
      table.ajax.params({name: 'test'}); <- I want to do something like this
      table.draw(); 
})

采纳答案by Gyrocode.com

I have modified your code to do what you need.

我已经修改了你的代码来做你需要的。

Initialize a data table:

初始化数据表:

 $('#mytable').DataTable({
    iDisplayLength: 10,
    responsive: true,
    processing: true,
    serverSide: true,
    searching: false,
    bLengthChange: false,
    bProcessing: true,
    paging: true,
    ajax: {
       url: me.url,
       dataType: 'json',
       cache:false,
       type: 'GET',
       data: function ( d ) {
          $.extend(d, me.data);
          d.supersearch = $('.my-filter').val();

          // Retrieve dynamic parameters
          var dt_params = $('#mytable').data('dt_params');
          // Add dynamic parameters to the data object sent to the server
          if(dt_params){ $.extend(d, dt_params); }
       }
    },
    columns: me.columns,
    columnDefs: me.renderer,
    initComplete: function() {

    }
 });

Handle click event on a button:

处理按钮上的点击事件:

NOTE: I'm assuming that this is the only place where you would be adding dynamic parameters for AJAX call.

注意:我假设这是您为 AJAX 调用添加动态参数的唯一地方。

 $('.button').on('click', function(){
    // Set dynamic parameters for the data table
    $('#mytable').data('dt_params', { name: 'test' });
    // Redraw data table, causes data to be reloaded
    $('#mytable').DataTable().draw();
 });

回答by cinemazealot

My way is not so pretty, but very simple and works great: when I want to pass custom parameters while redrawing the DataTable, first I simply change its URL:

我的方法不是那么漂亮,但非常简单并且效果很好:当我想在重绘 DataTable 时传递自定义参数时,首先我只需更改其 URL:

$('#table_id').DataTable().ajax.url('/custom/path/with/custom/parameters/inside/');
$('#table_id').DataTable().draw();

Then, if necessary, as a first step of the "on draw.dt" event I change it back to the normal URL:

然后,如有必要,作为“on draw.dt”事件的第一步,我将其改回正常的 URL:

$('#table_id').on('draw.dt', function() {
    $('#table_id').DataTable().ajax.url('/normal/path/for/ajax/source/');
    // ...
)};

The only disadvantage of this approach that it needs some trick to squeeze those custom parameters into the alternative path. Besides, it needs some server-side work also to prepare that alternative route and to extract those custom parameters from it.

这种方法的唯一缺点是需要一些技巧才能将这些自定义参数压缩到替代路径中。此外,它还需要一些服务器端工作来准备替代路由并从中提取那些自定义参数。