我想从initComplete
代码中以编程方式触发dblclick事件,以便在第一次显示页面时随机选择一行。以下是示例代码:
$(document).ready(function() {
var table = $('#mytable').DataTable({
// The data is comming from ajax
"initComplete": function(settings, json) {
var table = this.api();
var nrows = table.rows().count();
var nr = parseInt(Math.random() * nrows);
var event = new Event('dblclick');
table.row(nr).node().dispatchEvent(event);
}
});
table.on('dblclick', 'tr', function() {
alert('dblclick event has been fired!');
}
但是,事件永远不会被解雇。
答案 0 :(得分:0)
感谢大家的评论。正如@rory所建议的,我将事件的逻辑放在一个单独的函数中:
function myDblClickEvent(table, self) {
alert('dblclick event has been fired!' );
}
$(document).ready(function() {
var table = $('#mytable').DataTable({
// The data is comming from ajax
"initComplete": function(settings, json) {
var table = this.api();
var nrows = table.rows().count();
var nr = parseInt(Math.random() * nrows);
myDblClickEvent(table, table.row(nr).node());
}
});
table.on('dblclick', 'tr', function() {
myDblClickEvent(table, this);
}