Get value from first cell in a selected row using v1.10 beta 2
Get value from first cell in a selected row using v1.10 beta 2
MikeS
Posts: 113Questions: 1Answers: 0
I'm using the code found in the "Row Selection and Deletion (Single Row)" example as a start for something I'd like to do in my own code. The example code is:
[code]
$(document).ready(function() {
var table = $('#example').DataTable();
$('#example tbody').on( 'click', 'tr', function () {
if ( $(this).hasClass('selected') ) {
$(this).removeClass('selected');
}
else {
table.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
} );
$('#button').click( function () {
table.row('.selected').remove().draw( false );
} );
} );
[/code]
I would like to determine the value of the first cell of the selected row because it contains an ID that I need. How can I get that value? I have a feeling that table.row('.selected'). is going to do it but I can't figure it out. I've looked at the API and scanned this forum and am stuck trying to find the answer.
Unfortunately, I don't have an external site to point you to an example :(
[code]
$(document).ready(function() {
var table = $('#example').DataTable();
$('#example tbody').on( 'click', 'tr', function () {
if ( $(this).hasClass('selected') ) {
$(this).removeClass('selected');
}
else {
table.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
} );
$('#button').click( function () {
table.row('.selected').remove().draw( false );
} );
} );
[/code]
I would like to determine the value of the first cell of the selected row because it contains an ID that I need. How can I get that value? I have a feeling that table.row('.selected'). is going to do it but I can't figure it out. I've looked at the API and scanned this forum and am stuck trying to find the answer.
Unfortunately, I don't have an external site to point you to an example :(
This discussion has been closed.
Replies
table.cell('.selected', 0).data()
[/code]
will get you the first select which has a row with a class `selected` and the first cell in the row ( http://next.datatables.net/reference/api/cell() ).
If you want the node, use the `cell().node()` method and then you can read the `id` parameter from it.
Allan
So...in the button click function, if I delete the line of code to remove the selected row, and substitute alert(table.cell('.selected', 0).data());, the value in the first cell of the selected row will be displayed?
How would I get an array of cell data from multiple rows with the class .seleted?
This obviously doesn't work but it's the most I could come up with..
var cellData = [];
var rowCount = Table.cell('.checked')indexes().length;
for(var i=0;i<rowCount;i++) {
cellData.push(Table.cell('.checked', 0).data());
}
Does:
do what you are looking for?
Allan
Thanks a lot Allan.
table.cell('.selected', 0).data()
Thats it!
when clicking on a particular row i want to get the data in first cell to a jsp page.how can i do it?
Does that above not work for you? If not, please link to a test page, as per the forum rules, showing the issue so it can be debugged.
Allan