单击按钮时,如何触发 jquery 数据表 fnServerData 以通过 AJAX 更新表? [英] how can I trigger jquery datatables fnServerData to update a table via AJAX when I click a button?
问题描述
我将数据表插件与服务器端数据一起使用,并使用 AJAX 更新表.
I'm using the datatables plugin with server-side data and am updating the table using AJAX.
我的数据表设置如下所示:
My dataTables setup looks like this:
tblOrders = parameters.table.dataTable( {
"sDom": '<"S"f>t<"E"lp>',
"sAjaxSource": "../file.cfc",
"bServerSide": true,
"sPaginationType": "full_numbers",
"bPaginate": true,
"bRetrieve": true,
"bLengthChange": false,
"bAutoWidth": false,
"aaSorting": [[ 10, "desc" ]],
"aoColumns": [
... columns
],
"fnInitComplete": function(oSettings, json) {
// trying to listen for updates
$(window).on('repaint_orders', function(){
$('.tbl_orders').fnServerData( sSource, aoData, fnCallback, oSettings );
});
},
"fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
var page = $(oSettings.nTable).closest('div:jqmData(wrapper="true")')
aoData.push(
{ "name": "returnformat", "value": "plain"},
{ "name": "s_status", "value": page.find('input[name="s_status"]').val() },
{ "name": "s_bestellnr", "value": page.find('input[name="s_bestellnr"]').val() },
{ "name": "form_submitted", "value": "dynaTable" }
);
$.ajax({ "dataType": 'json', "type": "POST", "url": sSource, "data": aoData , "success": fnCallback });
}
我有一些用于过滤数据服务器端的自定义字段,我将这些字段与 AJAX 请求一起推送.问题是,我不知道如何从表外部触发 JSON 请求.如果用户输入过滤器,fnServerData 会触发并更新表.但是,如果用户选择表外的控件,我不知道如何触发 fnServerData 函数.
I have some custom fields for filtering the data server-side, which i'm pushing along with the AJAX request. The problem is, I don't know how to trigger a JSON request from outside of the table. If the user types into the filter, fnServerData fires and updates the table. However, if the user picks a control outside of the table, I have no idea how to trigger the fnServerData function.
现在我正在尝试在 fnInitComplete 中触发和监听一个自定义事件.虽然我可以检测到用户选择自定义过滤条件,但我缺少 fnServerData 正确触发所需的所有参数.
Right now I'm trying with a custom event I'm firing and listening to in fnInitComplete. While I can detect the user picking a custom filtering criteria, I'm missing all parameters needed for fnServerData to trigger correctly.
问题:
有没有办法从实际 dataTables 表之外的按钮触发 fnServerData?
Question:
Is there a way to trigger fnServerData from a button outside of the actual dataTables table?
我想我可以尝试在过滤器中添加一个空格,但这不是一个真正的选择.
I guess I could try to add a space to the filter, but this is not really an option.
感谢您的意见!
问题
推荐答案
我前段时间发现了这个脚本(所以我不记得它来自哪里 :( 以及归功于谁 :'( ) 但在这里:
I found this script some time ago (so I don't remember where it came from :( and who to credit for it :'( ) but here :
$.fn.dataTableExt.oApi.fnReloadAjax = function (oSettings, sNewSource, fnCallback, bStandingRedraw) {
if (typeof sNewSource != 'undefined' && sNewSource != null) {
oSettings.sAjaxSource = sNewSource;
}
this.oApi._fnProcessingDisplay(oSettings, true);
var that = this;
var iStart = oSettings._iDisplayStart;
var aData = [];
this.oApi._fnServerParams(oSettings, aData);
oSettings.fnServerData(oSettings.sAjaxSource, aData, function (json) {
/* Clear the old information from the table */
that.oApi._fnClearTable(oSettings);
/* Got the data - add it to the table */
var aData = (oSettings.sAjaxDataProp !== "") ?
that.oApi._fnGetObjectDataFn(oSettings.sAjaxDataProp)(json) : json;
for (var i = 0; i < aData.length; i++) {
that.oApi._fnAddData(oSettings, aData[i]);
}
oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
that.fnDraw();
if (typeof bStandingRedraw != 'undefined' && bStandingRedraw === true) {
oSettings._iDisplayStart = iStart;
that.fnDraw(false);
}
that.oApi._fnProcessingDisplay(oSettings, false);
/* Callback user function - for event handlers etc */
if (typeof fnCallback == 'function' && fnCallback != null) {
fnCallback(oSettings);
}
}, oSettings);
}
添加BEFORE 调用数据表初始化函数.然后你可以像这样调用重新加载:
Add that BEFORE you call the datatable initialization function. then you can just call the reload like this:
$("#userFilter").on("change", function () {
oTable.fnReloadAjax(); // In your case this would be 'tblOrders.fnReloadAjax();'
});
userFilter
是下拉列表的 ID,因此当它更改时,它会重新加载表的数据.我只是将其添加为示例,但您可以在任何事件上触发它.
userFilter
is an ID for a dropdown, so when it changes, it reloads the data for the table. I just added this as an example but you can trigger it on any event.
这篇关于单击按钮时,如何触发 jquery 数据表 fnServerData 以通过 AJAX 更新表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!