recover first column value

recover first column value

ocin35ocin35 Posts: 15Questions: 2Answers: 0

hi,
$('#items tbody').on( 'click', 'td', function () {
alert( table.cell( this ).data() );
} );

with this code alert displays the contents of the clicked cell, but how does the first column value get back if you click on any cell in the row?

Please

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,310Questions: 26Answers: 4,948

    Here is one option:

    table.cell( $( this ).closest('tr').find('td:first-child')).data()
    

    For example:
    http://live.datatables.net/hopupepa/1/edit

    You may be able to build a more efficient jQuery selector.

    Kevin

  • ocin35ocin35 Posts: 15Questions: 2Answers: 0

    thank you so much :)

  • ocin35ocin35 Posts: 15Questions: 2Answers: 0

    last question please to display alert only if clicks in column 3?

    $('#items tbody').on( 'click', 'td', function () {

  • ocin35ocin35 Posts: 15Questions: 2Answers: 0

    i found a solution!!

    $('#items tbody').on( 'click', 'td', function () {
    if (table.column( $( this )).index() == 1) {
    jsID = table.cell( $( this ).closest('tr').find('td:first-child')).data();
    alert (jsID);
    }
    } );

  • ocin35ocin35 Posts: 15Questions: 2Answers: 0

    Sorry but the function does not work if column 0 is hidden and works if column 0 is visible - how to do it? please

    table.cell( $( this ).closest('tr').find('td:first-child')).data()

  • kthorngrenkthorngren Posts: 21,310Questions: 26Answers: 4,948
    Answer ✓

    See if this does what you want:
    http://live.datatables.net/hopupepa/2/edit

    Kevin

  • ocin35ocin35 Posts: 15Questions: 2Answers: 0

    it does not work
    I continue my research, if you have another idea? thank you

  • ocin35ocin35 Posts: 15Questions: 2Answers: 0
    edited March 2019

    I found the solution
    the following code shows in alert the value of the hidden column only if the click is done in the first column displayed - thanks

    $('#items').on('click', 'td', function (e) {
    if (table.column( $( this )).index() == 1) { //first column only
    var data1 = table.row( $(this).parents('tr') ).data()["ID"]; //hidden column
    alert(data1);
    }
    } );

This discussion has been closed.