在绘制前将参数添加到datatable ajax调用 [英] Add parameter to datatable ajax call before draw

查看:263
本文介绍了在绘制前将参数添加到datatable ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何在table.draw()之前动态地向ajax调用一个参数,所以我的请求有新的参数?我使用的数据表1.10



我看到无处不在,找不到答案。



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

  $('#mytable')DataTable({
iDisplayLength:10,
respond:true ,
处理:true,
serverSide:true,
search: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();
}
},
列:me.columns,
columnDefs:me.renderer,
initComplete:function(){

}
});

这一切都很好,但是我尝试把它绑定到一个按钮来传递新的参数。 ('click',function(){

  $('。button' #mytable')DataTable(); 
table.ajax.params({name:'test'});< - 我想做这样的事情
table.draw();
$)


解决方案

我修改了你的代码你需要什么



初始化数据表

  $('#mytable')。DataTable({
iDisplayLength:10,
respond: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();

//检索动态参数
var dt_params = $('#mytable')。 dt_params');
//向发送到服务器的数据对象添加动态参数
if(dt_params){$ .extend(d,dt_params);}
}
} ,
列:me.columns,
columnDefs:me.renderer,
initComplete:function(){

}
});

处理按钮上的点击事件:



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

  $('。button')。on('click',function(){
//设置动态数据表的参数
$('#mytable')。data('dt_params',{name:'test'});
//重绘数据表,导致重新加载数据
$('#mytable')。DataTable()。draw();
});


I am using DataTables 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.

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(); 
})

解决方案

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.

 $('.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();
 });

这篇关于在绘制前将参数添加到datatable ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆