Datatables.net 和 sweetalert
所以我使用 Datatables ,每行末尾都有一个删除按钮,我将其与 Sweetalert。
我每页有 10 个结果,如果有更多页面,我转到下一页并尝试使用删除按钮,但什么也没有发生。就像除了前 10 行之外的任何其他内容都没有渲染 sweetalert 一样。
下面是我的 Sweetalert 代码,尽管我不认为它与此代码相关,因为它对于前 10 行工作正常。
<script>
$(document).ready(function () {
$('.deleteItem').on('click', function (e) {
e.preventDefault();
var form = $(this).parents('form');
Swal.fire({
title: 'Some title',
text: "Some text",
}).then((result) => {
if (result.value) {
form.submit();
}
});
});
});
</script>
我只是想知道使用 Datatables 的其他人是否遇到过类似的问题,或者是否有人知道问题可能是什么。就像页面上只呈现前 10 行一样。
我初始化数据表的方式如下所示,所以没什么花哨的:
$(document).ready(function () {
$('.datatable').dataTable();
});
我所有的删除按钮看起来都是这样的:
<form method="POST" action="something">
<a type="submit" class="deleteItem">
Delete
</a>
</form>
So I am using Datatables and every row has a delete button at the end, which I use in combination with Sweetalert.
I have 10 results per page and if for example there are more pages, and I go to the next page and try to use the delete button, nothing happens. It's like the sweetalert is not being rendered for anything else than the first 10 rows.
Below is my Sweetalert code, although I do not think it is related to this code since it works fine for the first 10 rows.
<script>
$(document).ready(function () {
$('.deleteItem').on('click', function (e) {
e.preventDefault();
var form = $(this).parents('form');
Swal.fire({
title: 'Some title',
text: "Some text",
}).then((result) => {
if (result.value) {
form.submit();
}
});
});
});
</script>
I am just wondering if anyone else using Datatables has ran into a similar issue or if someone knows what the issue might be. It's like only the first 10 rows are being rendered on the page.
The way I am initializing the datatable is simply as follows, so nothing fancy:
$(document).ready(function () {
$('.datatable').dataTable();
});
All my delete buttons look like this:
<form method="POST" action="something">
<a type="submit" class="deleteItem">
Delete
</a>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所指出的,其他删除锚点不在其他页面的 DOM 中。您可以侦听 DataTables 触发的
draw
事件并分配单击此处使用 Sweetalert 的处理程序。我定义了一个函数registerDeleteItemHandlers
来封装单击事件处理程序的注册。As you noted, the other delete anchors are not in the DOM for the other pages. You can listen for the
draw
event fired by DataTables and assign the click handler using Sweetalert there. I've defined a functionregisterDeleteItemHandlers
to encapsulate the registration of the click event handlers.