add a button to each row and catch click event
add a button to each row and catch click event
Hello,
I'm adding rows to my datatable using Ajax like this :
$.ajax({
type : 'GET',
url: 'scripts/mds.php',
data: "operation=list",
dataType: 'json',
success: function(response)
{
Table.clear();
var btn="<button type='button' id='askbutton' class='btn btn-block btn-primary'>ASK</button>";
$.each(response, function(idx, obj) {
Table.row.add([ obj["id"],
obj["name"],
btn]);
});
Table.draw();
},
error: function(xhr, status, error) {
console.log("error :"+xhr.responseText);
}
});
It works perfectly : a button is added to each line. But now, when a button is fired, I would like to make a call to some PHP function and pass some arguments (like obj["id"]) to identify the clicked button, and then replace the clicked button by a text which would show if the PHP function worked or if an error occured...
How can I achieve this ? Could you please help me ?
Many thanks in advance !
T.
This question has an accepted answers - jump to answer
Answers
Hello, hope this will help you.
You could add columns property, and on each column you could map your data object by name. And also you have access to whole data from object called "full"
And in object data - u get current column object. So here i making link with id from other column (full.memberId). U could create function for button, and use it with OnClick event in button properties. And add all needed objects to this function.
columns: [
{
data: "name",
render: function (data, type, full, meta) {
return '<a href="' + somUrl+ "?id=" + full.memberId + '">' + data + '</a>';
}
},
I'm really sorry but I don't understand enough...
Would you please write some details more ? I don't understand what I should modify and how... :-s A simple example would be very very appreciated, if possible... :-s
Have a look at the renderer documentation - that's got full details of how to write renderers and a few examples.
If you are struggling with that, link to the page showing what you've tried and we can take a look.
Allan
I got it :-) Thanks !! :-)