1

I want to get values of a row in DataTables when a click on the button corresponding to this row, but when using this code i get undefined:

The buttons are being added like this:

function getdata()
    {
     $pdrs = Pdrs::select('ID_Piece', 'Designation', 'Status');
     $result = DataTables::of($pdrs)
            ...
            ->addColumn('action', function($pdr){
            return '<a href="#" class="btn btn-xs btn-primary Ajouter_au_panier" id="'.$pdr->ID_Piece.'"><i class="glyphicon glyphicon-shopping-cart"></i> Ajouter au panier</a>';})
        ->make(true);   
    return $result ;
    }

the trigger is:

$(document).on('click', '.Ajouter_au_panier', function(){
    var table = $('#pdr_table').DataTable();
    console.log( table.row( this ).data() );  
});
2
  • 2
    What is this? We need to see much more of your code in order to help you. Commented Feb 7, 2019 at 8:00
  • Does this because adding the buttons manually? Commented Feb 7, 2019 at 8:51

1 Answer 1

3

If you put the trigger in the <a> tag inside the cell, you need to reference the parent cell instead of the <a>.

$(document).on('click', '.Ajouter_au_panier', function(e){
    var table = $('#pdr_table').DataTable();
    console.log( table.row( $(this).closest("td") ).data() );  
});

UPDATE / EDITED: To get the contents of a specific column, use the cells() function with the row and column as params in an object. I.e. for column 2:

$(document).on('click', '.Ajouter_au_panier', function(e){
    var table = $('#pdr_table').DataTable();
    var rowId = table.row( $(this).closest("td") ).index();
    console.log( table.cells({ row: rowId, column: 2 }).data()[0] );  
});

UPDATE 2 / EDITED: If you want to get the contents of the same column where the link was clicked, you can do it like this:

$(document).on('click', '.Ajouter_au_panier', function(e){
    var table = $('#pdr_table').DataTable();
    var rowId = table.row( $(this).closest("td") ).index();
    var colId = table.column( $(this).closest("td") ).index();
    console.log( table.cells({ row: rowId, column: colId }).data()[0] );  
});
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, thank you this is working, how do i filter this to get only the value of a specific column?
I get undefined when doing console.log( table.row( $(this).closest("td") ).data()[2] );
Do you have 3 columns and data in the 3rd column? Btw. it's good practice to approve the answer when the original question has been answered, not after asking more questions.
Yes i have that's the result when doing only data() {ID_Piece: "2", Designation: "Ecran", Status: "Indisponible", checkbox: "<input type="checkbox" name="pdr_checkbox[]" class="pdr_checkbox" value="2" />", action: "<a href="#" class="btn btn-xs btn-primary Ajouter_…yphicon-shopping-cart"></i> Ajouter au panier</a>"}
undefined is the result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.