Javascript 分页上的刷新按钮
我正在构建一个基于ajax源数据的分页:Javascript 分页上的刷新按钮,javascript,jquery,datatables,Javascript,Jquery,Datatables,我正在构建一个基于ajax源数据的分页: let table = $('#userTable').DataTable({ destroy: true, "ajax": {url: url, type: 'GET', "dataSrc": ""}, "pagingType": "simple", "columnDefs": [ // ... ], "order": [[3, "desc"]], "columns": [ { "data": "view"
let table = $('#userTable').DataTable({
destroy: true,
"ajax": {url: url, type: 'GET', "dataSrc": ""},
"pagingType": "simple",
"columnDefs": [
// ...
],
"order": [[3, "desc"]],
"columns": [
{ "data": "view", "defaultContent": "" },
{ "data": "username" },
{ "data": "email", "defaultContent": ""},
{ "data": "lastActive"},
{ "data": "completed" },
{ "data": "_id" }
],
"fnInitComplete": function(oSettings, json) {
console.log("Finished drawing table");
$('#spinner').addClass('d-none');
$('.view-profile').on('click', getProfile);
$('.btn-delete').on('click', purgeUser);
$('.page-link').on('click', () => {
console.log("Clicked on paginate button")
$('.view-profile').on('click', getProfile);
$('.btn-delete').on('click', purgeUser);
})
}
});
我将按钮(.view profile
和.btn delete
)添加到每一行,并且仅在使用[fnInitComplete][2]
初始化表之后才添加侦听器
但是,单击分页按钮。页面链接
,按钮将不再可单击-如何重新侦听分页时的单击?
我尝试添加一个超时函数,首先等待按钮被绘制(.view profile
),然后在添加$('.view profile')之前单击.page链接
等待1s。在('click',getProfile)
,但这不起作用。我错过了什么
if($('.view-profile')){
setTimeout(() => {
$('.page-link').on('click', function() {
setTimeout(() => {
console.log("Next page")
$('.view-profile').on('click', getProfile);
$('.btn-delete').on('click', purgeUser);
}, 1000);
})
}, 1000);
}
将事件侦听器添加到最近的静态元素将您的单击侦听器附加到文档级别,这应该work@mplungjan-您的意思是类似于('click',getProfile')代码>?编号<代码>$(“#用户表”)。在(“单击”,“查看配置文件”,获取配置文件)代码>