Get row data when I click on a column

Get row data when I click on a column

jasjas Posts: 2Questions: 0Answers: 0
edited February 2011 in General
Hi all,


I am trying to get the show a dialog when the user clicks on the first column in the table. What I would like to do is to get the data from that row and display it in the dialog. Unfortunately, I am able to do all of the above except that I wind up showing the dialog when ANY column is clicked... How do I check which column is clicked?

thanks,
Jas

[code]

$('#dt tbody tr').live('click', function () { showDlg(this); });

function showDlg(this_) {
// TODO: Check if the click was on the first column..
// How?


var aData = $('#dt).dataTable().fnGetData(this_);
key = aData[1];
$("#showKeyDlg").dialog("open");
}

[/code]

Replies

  • jasjas Posts: 2Questions: 0Answers: 0
    Anyone? anyone?
  • GregPGregP Posts: 500Questions: 10Answers: 0
    edited February 2011
    You're binding the click to the wrong element. Your jQuery selector is finding all rows ('tr') inside all table bodies ('tbody') inside all elements with the class 'dt' ('#dt').

    So instead of binding the click to the row and THEN checking to see if it was the first column that was clicked, you should just bind it to the first column instead.

    [code]
    $('td:nth-child(1)').live ..etc
    [/code]

    or if you need all the specificity (will depend on the complexity of your page and what elements are within #dt),

    [code]
    $('#dt tbody tr td:nth-child(1)').live ..etc
    [/code]

    The problem with that is that "this" is no longer the table row. So to capture the row data, you'll have to do additional traversal. this.parent() should be enough.
  • 28.vivek.s28.vivek.s Posts: 69Questions: 0Answers: 0
    look at this discussion...

    http://datatables.net/forums/comments.php?DiscussionID=3656&page=1#Item_3
  • ikselaiksela Posts: 13Questions: 0Answers: 0
    edited February 2011
    [code]
    $("#dt tbody td").live('click',function(){
    var aPos = $('#dt').dataTable().fnGetPosition(this);
    var aData = $('#dt').dataTable().fnGetData(aPos[0]);
    // at this point aData is an array containing all the row info, use it to retrieve what you need.
    });
    [/code]
This discussion has been closed.