Get row data when I click on a column
Get row data when I click on a column
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]
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]
This discussion has been closed.
Replies
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.
http://datatables.net/forums/comments.php?DiscussionID=3656&page=1#Item_3
$("#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]