gpt4 book ai didi

jquery - 在绘制之前向数据表 ajax 调用添加参数

转载 作者:行者123 更新时间:2023-12-03 22:10:09 27 4
gpt4 key购买 nike

我正在使用 DataTables 1.10

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

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

$('#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() {

}
});

这一切都工作正常,但后来我尝试将其绑定(bind)到一个按钮以传递新参数。

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

最佳答案

我已经修改了您的代码以满足您的需要。

初始化数据表:

 $('#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() {

}
});

处理按钮上的点击事件:

注意:我假设这是您为 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();
});

关于jquery - 在绘制之前向数据表 ajax 调用添加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28906515/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com