我有一个通过以下 ajax 请求在单击按钮时生成的表:
function getDrivers() {
$.ajax({
type: "GET",
url: "/admin/getDrivers",
datatype: "json",
success: function(response) {
$("#head").children().remove();
var trHTML = '';
trHTML += '<h2>' + "Водители" + '</h2>' +
'<div class="table-responsive">' +
'<table id="dataTable" class="table table-hover">' +
'<thead><tr><th>' +
'Ник' + '</th><th>' +
"Имя" + '</th><th>' +
"Фамилия" + '</th><th>' +
"Мобильный телефон" + '</th><th>' +
"Электронная почта" + '</th><th>' +
"Город" + '</th><th>' +
'</th></tr></thead><tbody>'
;
$.each(response, function(i, item) {
trHTML += '<tr><td>' +
item.nickname + '</td><td>' +
item.firstName + '</td><td>' +
item.lastName + '</td><td>' +
item.mobileNumber + '</td><td>' +
item.email + '</td><td>' +
item.city.cityName + '</td><td>' +
'<input type="checkbox" value=""/>' +
'</td></tr>';
});
trHTML += '</tbody>' + '</table>' + '</div>';
$("#head").append(trHTML);
},
error: function() {
alert("error")
}
});
}
所有这些东西都附加到下一个块div
:
<div id="head" class="col-sm-9 offset-sm-3 col-md-10 offset-md-2 pt-3">
接下来,我需要为该表创建一个上下文菜单。我使用以下脚本制作此菜单:
$("#dataTable").bootstrapTable({
rowStyle: "rowStyle",
contextMenu: '#context-menu',
contextMenuTrigger: 'right',
onClickRow: function(row, $el) {
$("#dataTable").find(".success").removeClass('success');
$el.addClass('success');
},
onContextMenuItem: function(row, $el) {
if ($el.data("item") == "edit") {
alert("Edit: " + row.itemid + ' ' + row.name + ' ' + row.price);
} else if ($el.data("item") == "delete") {
alert("Delete: " + row.itemid + ' ' + row.name + ' ' + row.price);
} else if ($el.data("item") == "action1") {
alert("Action1: " + row.itemid + ' ' + row.name + ' ' + row.price);
} else if ($el.data("item") == "action2") {
alert("Action2: " + row.itemid + ' ' + row.name + ' ' + row.price);
}
}
});
因此,此菜单不适用于单击按钮时生成的表格,但如果表格是静态的,则可以。请告诉我如何解决这个问题。我尝试通过$("#dataTable").ready(скрипт контекстного меню)
它没有用。
使用回调
.done()
并将呼叫放在.bootstrapTable()
那里。到目前为止,您有一个异步请求并且调用.bootstrapTable()
在结束前触发。