我试图向数据表中的每一行添加:edit和delete按钮,但单击按钮不会执行在"onclick";
$(schedTableID).DataTable({
data: schedData.data,
order: [[0, "asc"]],
columns: [
{ data: "id",render: function(data, type, row) { return data; }, },
{ data: "timein", render: function(data, type, row) { return data; }, },
{ data: "timeout", render: function(data, type, row) { return data; }, },
{ data: "slot", render: function(data, type, row) { return data; }, },
{ data: "session", render: function(data, type, row) { return data; }, },
{ data: "schedule", render: function(data, type, row) { return data; }, },
{ data: "statuss", render: function(data, type, row) { return data; }, },
{ data: "id", render:
function(data, type, row) {
return(
`
<button class="m-0 p-2 px-2 btn btn-success feather icon-check" data-toggle="button" id="${data}" onClick="${deleteHandler}"></button>
<button class="m-0 p-2 px-2 btn btn-danger feather icon-x" data-toggle="button" id="${data}" onclick="${deleteHandler}"></button>
`
)
},
},
],
pageLength: 5,
lengthMenu: [
[5, 10, 25, 50, 100, -1],
[5, 10, 25, 50, 100, "Show All"],
],
});
以下是onclick函数
const deleteHandler = (e) => {
console.log(123123)
}
const checkHandler = (e) => {
console.log(123123)
}
我用jquery代替
$(document).ready(function () {
$('#data-table-admin').unbind('click').on('click', '.btn-edit', function () {
let table = $('#data-table-admin').DataTable();
//retrieve table id, etc. (table.row($(this).parents('tr')).data().id)
//function here
});
$('#data-table-admin').on('click', '.btn-delete', function () {
let table = $('#data-table-admin').DataTable();
//retrieve table id, etc. (table.row($(this).parents('tr')).data().id)
//functionality here
});
});