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

MikeSMikeS Posts: 113Questions: 1Answers: 0
edited March 2014 in DataTables 1.10
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 :(

Replies

  • indymxindymx Posts: 63Questions: 3Answers: 0
    Are you trying to do something with the data only when the row is selected?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    [code]
    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
  • MikeSMikeS Posts: 113Questions: 1Answers: 0
    Thanks Allan. That did it for me.
  • OldCootOldCoot Posts: 3Questions: 1Answers: 0

    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?

  • bcraigbcraig Posts: 8Questions: 2Answers: 0

    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());

    }

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Does:

    var data = Table.cells('.checked').data();
    

    do what you are looking for?

    Allan

  • curiousBoycuriousBoy Posts: 1Questions: 0Answers: 0

    Thanks a lot Allan.

    table.cell('.selected', 0).data()
    Thats it!

  • RangikaRangika Posts: 1Questions: 0Answers: 0

    when clicking on a particular row i want to get the data in first cell to a jsp page.how can i do it?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    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

This discussion has been closed.