add a button to each row and catch click event

add a button to each row and catch click event

trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

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

  • jonnebuljonnebul Posts: 6Questions: 0Answers: 0

    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>';
    }
    },

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    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

  • allanallan Posts: 63,192Questions: 1Answers: 10,412 Site admin
    Answer ✓

    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

  • trucmuche2005trucmuche2005 Posts: 71Questions: 22Answers: 2

    I got it :-) Thanks !! :-)

This discussion has been closed.