Row click event -- need to work with one of the cell content of the clicked row

Row click event -- need to work with one of the cell content of the clicked row

satya_vsatya_v Posts: 3Questions: 0Answers: 0
edited June 2012 in DataTables 1.8
Hey friends,

Currently we are implementing the jquery data table 1.8.*
we are able to reach the row click event, still we are facing problem with accessing the one content of a cell
can some body advice me for the resolution

Thanks,
Satya.

Replies

  • semseosemseo Posts: 9Questions: 0Answers: 0
    edited June 2012
    Hi,
    I don't know if this is what you need but maybe it will help.
    I have users selecting rows and the value of the first column is added/removed from an array. It is the aData[1] that specifies the value in the first column.

    oTable = $('#contactList').dataTable({
    "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
    // user selected rows
    if ( jQuery.inArray(parseInt( aData[1] ), gaiSelected) != -1 ){
    $(nRow).addClass('row_selected');
    }
    return nRow;
    }
    });


    $('#contactList tbody tr').live('click', function () {
    var aData = oTable.fnGetData( this );
    var iId = aData[1];
    iId = parseInt(iId,10);
    if ( jQuery.inArray(parseInt(iId), gaiSelected) == -1 ){
    gaiSelected[gaiSelected.length++] = parseInt(iId,10);
    $(this).addClass('row_selected');
    }else{
    gaiSelected = jQuery.grep(gaiSelected, function(value) {
    return value != iId;
    });
    if ( $(this).hasClass('row_selected') ){
    $(this).removeClass('row_selected');
    }
    }

    });

    I found most of this solution in other posts on this forum.
  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    As semseo points out, fnGetData is going to be the best way here I think. One thing to note is that in 1.9 you can pass fnGetData a TD/TH cell and get the data just for that cell, while in 1.8- you need to give fnGetData a TR element and you'll get back an array (or object, depending on your data source).

    Allan
  • satya_vsatya_v Posts: 3Questions: 0Answers: 0
    Thanks Semseo and Allan for your response, its working now
  • jpakulskijpakulski Posts: 2Questions: 0Answers: 0
    You can use the delegated click event used above.
    It might be easier to do something like this though:

    [code]
    $('#contactList').dataTable({
    fnRowCallback: function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
    // Row click
    $(nRow).on('click', function() {
    console.log('Row Clicked. Look I have access to all params, thank You closures.', this, aData, iDisplayIndex, iDisplayIndexFull);
    });

    // Cell click
    $('td', nRow).on('click', function() {
    console.log('Col Clicked.', this, aData, iDisplayIndex, iDisplayIndexFull);
    });
    }
    });
    [/code]
This discussion has been closed.