data table separated different button
data table separated different button
hi, i got data table, in data table row are two button, i wanna separated them, because one for delete, one for edit. I use js, to add what button. Picture bellow
Now then i press edit or delete i got the same. my code js code
var table = $('#calEvents').DataTable( {
"processing": true,
"serverSide": false,
"order": [[ 3, "asc" ]],
"ajax": "/api/v1/calendar/get",
'columnDefs': [
{
targets: 2, render: function(data1){ return moment(data1).format('dddd')},
},
{
targets: -1, defaultContent: '<button class="btn btn-secondary" data-dismiss="modal" id="Edit" type="button">Edit</button>'
+ '  <button class="btn btn-danger" id="Delete" data-dismiss="modal" type="button">Delete</button>'
},
{ targets: 3, render: function(data2){ return moment(data2).format('YYYY-MM-DD')}},
]
} );
$('#calEvents').on( 'click', 'button', function () {
var data = table.row( $(this).parents('tr') ).data();
var row= this.parentNode.parentNode;
var confirmalert = confirm("Are you sure?");
if (confirmalert == true) {
document.getElementById("calEvents").deleteRow(row.rowIndex);
}else{
alert('Invalid ID.');
}
});
Now i got only delete function, because i cant test if they working like one.
i try do something like
$('#calEvents .deleteButton').click(function (){
<button class="btn btn-danger" id="Delete" data-dismiss="modal" class="deleteButton" type="button">Delete</button>'
but my button not working then.
This question has an accepted answers - jump to answer
Answers
Maybe try using the button name attribute and a selector using the name s shown in this Stack Overflow thread.
Kevin
I try this one, but still nothing
$("button[name='delete']").click(function()
Isn't going to work. You need to use delegated events like you had before and as shown in this example.
Try `$('#calEvents').on( 'click', 'button[name="delete"]', function () {``
If this doesn't work then please build an example with what you are trying so we can better help you.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Its working, thank you very much
In my case maybe can you give some tips how to star edit function? i like to do like this https://editor.datatables.net/examples/simple/inTableControls.html
my json data looks like this:
and my html code looks like this (table)
If you don't want to use the Editor then you will need to create your won forms and submit them using ajax. If you don't know how then you will need to find a tutorial, like this. Once you ge the data submitted via ajax then your server script will need to update the row and provide an ajax response so you know at the client the row was updated. One option is to have the server respond with the updated row data and you can directly update the row in the table. Or you can use
ajax.reload()
to refresh the table from the server DB.If you have Datatables related questions let us know. If you have questions around the HTML forms, then you are better served by search Stack Overflow as these are more generic not Datatables specific questions.
Kevin