I have a dynamic jQuery data table. For the final column, I have the option of deleting rows. In order for this to execute, I need to pass the itemId value to the function specified within the button onClick attribute.
This is how I have currently done it but no luck:
defaultContent: "<button class='btn btn-danger' onclick='StockSearchManagment.DeleteStock(" + data.itemId + ")'>Delete</button>"
This is my full JavaScript function:
GetInfo: function (tble) {
$(document).ready(function () {
$.ajax({
url: '/Home/GetList',
dataType: "json",
method: 'post',
success: function (data) {
tble.DataTable().destroy();
tble.DataTable({
data: data.html
,
"columns": [
{ data: "itemId" },
{ data: "name" },
{ data: "description" },
{
data: null,
defaultContent: "<button class='btn btn-danger' onclick='StockSearchManagment.DeleteStock(" + data.ItemId + ")'>Delete</button>"
}
]
});
},
error: function (err) {
alert(err);
}
});
});
}
2
Answers
I refered to this stack overflow question: Passing more than one value into a button in the server side Datatable Jquery using MVC
It's now working with render instead of defaultContext:
you can tag the button you generate with a "data" tag and read that from the onlick event.
the tag would look like:
<button … data-id="">
the onlick event would do this: var itemid = $(this).attr(‘data-id’).val();
and then you’d have the value to use.