3

I have a DataTables Table, and I want to be able to get the value of the first td, when the tr is clicked on. I have set the visibility of this td to false.

Edit: because the two answers so far are assuming I can click on the cell I want. I cannot click on the cell I need the value of.

$(document).ready(function() {

    var table = $('#example').DataTable({ 
        select: false,
        "columnDefs": [{
            className: "ID", 
            "targets":[0],
            "visible": false,
            "searchable":false
        }]
    });//End of create main table



    $('#example tbody').on( 'click', 'tr', function () {

        cellValue =     //code to get the cell value 
        console.log(cellValue);

    });

});

I have seen a lot of examples using the older DataTables method, fnGetColumnData, but am not sure how to implement the newer cell.data().

Can anyone help me out?

blackandorangecat
  • 1,246
  • 5
  • 18
  • 37

1 Answers1

5

To achieve expected result and get hidden column data by using row(this).data()

  $('#example tbody').on( 'click', 'tr', function () {
     alert(table.row( this ).data()[0]); 
  });

http://codepen.io/nagasai/pen/kXyazm

Above mentioned code will return complete row data both hidden and visible data and mention the position of the hidden column position

Naga Sai A
  • 10,771
  • 1
  • 21
  • 40
  • So that's a nice piece of code for retrieving the cell that I am clicking on. However, I can not click on the cell for whos value I want... The cell I want, is hidden. I would like to be able to click on a row, and get the value of the first cell. – blackandorangecat Jun 30 '16 at 00:34
  • for getting only first value , use below option $('#example tbody').on( 'click', 'tr', function () { console.log($(this)[0].children[0].innerHTML) ; }); – Naga Sai A Jun 30 '16 at 00:42
  • That works, if the html is there. Because I am using the DataTable plugin to set `"visible": false', the element is not displayed. I think the only way to accomplish this is to use one of the methods provided by DataTables - I just don't know how to implement it! – blackandorangecat Jun 30 '16 at 00:51
  • Take a look at how I am implementing the table, in the JS in my original post – blackandorangecat Jun 30 '16 at 00:53
  • @blackandorangecat i have updated the answer with the changes in the coepen - http://codepen.io/nagasai/pen/kXyazm – Naga Sai A Jun 30 '16 at 01:19
  • In this example, i am fetching hidden column data - Name using row(this).data(). Hope this is helpful for you :) – Naga Sai A Jun 30 '16 at 01:21