gpt4 book ai didi

javascript - DataTables 成功回调替代方案

转载 作者:行者123 更新时间:2023-11-30 17:12:01 25 4
gpt4 key购买 nike

我正在尝试将 DataTables 与成功回调一起使用。因为我想在用户输入的创建 DataTable 的值有错误时向用户发出警告。

不幸的是,DataTables 需要自己的成功回调,因此我不能重载它。

目前代码是:

configurationBlockChart = $('#blockSearchTable').DataTable(
{
processing: true,
serverSide: true,
stateSave: true,
bDestroy: true,
ajax:
{
type: 'GET',
url:"ajax_retreiveServerSideBlockNames/",
data:
{
'csrfmiddlewaretoken':csrftoken,
'username':username
}
},
rowCallback: function(row, data)
{
if ($.inArray(data.DT_RowId, blockSelected)!== -1)
{
$(row).addClass('selected');
}
},
});

此 Ajax Get 返回的数据是数据行。

但是有可能返回的数据返回的是invalid,没有返回行

我尝试在rowCallBack之前添加成功:

success: function(response)
{
if(response.status == "invalid")
//then inform user
}

也尝试过使用 fnDrawCallBack

fnDrawCallback: function(settings, response)
{
console.log("Hello World!");
if(response.status == "invalid")
{
$('#invalid').modal("show");
$('#usernameSearch').modal("show");
}
}

但是,fnDrawCallBack 只会在返回行时调用。

问题是有时没有返回任何行,javascript代码给出了一个异常。

不过,我可以 try catch ,但我仍然希望我的服务器为 javascript 代码提供 json 状态。

编辑:使用 xhr,它可以捕获无效响应,同时不会干扰 ajax 的成功功能。

$('#chartSearchUsername').click(function(event)
{
$('#chartConfigModal').modal("hide");
$('#usernameSearch').modal("show");

configurationUserChart = $('#userSearchTable').DataTable(
{
processing: true,
serverSide: true,
stateSave: true,
bDestroy: true,
ajax:
{
type: 'GET',
url:"ajax_retreiveServerSideUsernames/",
data:
{
'csrfmiddlewaretoken':csrftoken
},
},
rowCallback: function(row, data)
{
if ($.inArray(data.DT_RowId, userSelected)!== -1)
{
$(row).addClass('selected');
}
},
})
.on('xhr.dt', function(e, settings, response)
{
if(response.status == "invalid")
{
$('#invalid').modal("show");
$('#usernameSearch').modal("hide");
}
});
});

最佳答案

$('#example').dataTable();

$('#example').on( 'xhr.dt', function () {
console.log( 'Ajax call finished' );
} );

关于javascript - DataTables 成功回调替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26791421/

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