Delete row
Delete row

I made similar datatable to this:
http://live.datatables.net/comeyesa/6/watch
I do not know how to delete row.
I added in button render the row.Name (which in my real datatable is row._id). If I use ejs and use:
var table = $('#contentTable').DataTable( {data: <%-JSON.stringify(table)%>...
, this includes _id. This id will be needed as req.params.id in server side when I want to delete record in db.
I do not understand how the e.target.getAttribute("id")
works, this should give back the button id that was pressed, but in many cases it is null or if it is not null then it has no value (as in live example), though I added id to delete button.
In live example,var data = table.row( this ).data(); console.log( 'Data',data );
gives back the row object, but in my real datatable it is undefined.
Thank you for your help.
Answers
There is a lot in that event handler. Are you using child rows?
Here is a simple example of a delete button using
defaultContent
.http://live.datatables.net/xijecupo/400/edit
Kevin
Hi Kevin,
Thank you for your reply.
I still cannot delete row. In my datatable the
var data = table.row( row ).data()
is undefined. Though it reaches the click listener$('#example tbody').on('click', 'input[name="delete"]', function () {...
In your version I could not set icon for the button as in your example it had
value="Delete"
. I want to use this icon<i class=\"fa fa-trash-o\" aria-hidden=\"true\"></i>
Where shall I put it?Are you defining the variable
table
? Hard to say without seeing what you have.You can remove that. The input you create is up to you. There is nothing Datatables specific in the input and Datatables has no control of if.
Please update my test case, or create your own or post a link to your page so we can see what you have.
Kevin
I managed to delete row:
http://live.datatables.net/comeyesa/6/watch
Though this does not work on this live example, it works on my datatable. The table.row( row ).data() is still undefined, but I changed
this:
var row = $(this).parents('tr');
to this:
var row = $('#delete'+rowid).parents('tr');
and it found the row that was deleted when clicked on row's button and it removed the row.I got the rowid after ajax delete success from server side.
I mentioned that e.target.getAttribute("id") was sometimes null. It was because the delete button had a trash icon
<i class=\"fa fa-trash-o\" aria-hidden=\"true\"></i>
( in live example it has only text saying Delete) and e.target.getAttribute("id") did not find id when I clicked on icon and not on button. So I changed to this:<i id=\"delete' + row._id + '\" class=\"fa fa-trash-o\" aria-hidden=\"true\"></i>
I got row._id from columns
render:(data,type,row)=>{
(in live example it was row.Name). So button and its icon also has id now, e.target.getAttribute("id") can be found in both.Thank you for support, datatables are awesome