A bit of contrived method for getting hidden column value for a selected row

A bit of contrived method for getting hidden column value for a selected row

Focker513Focker513 Posts: 10Questions: 4Answers: 0

I am returning the first column(hidden) which is called "Id" of my table when a row is selected.
Here is my snippet:
$('#tblMyTable tbody').on('click', 'tr', function () {
var tr = $(this).closest('tr');
var row = table.row(tr);
var Id = table.row(row[0][0]).data().Id;
//call some ajax
});
Just curious if this is the best way or if there is a preferred method\ shortcut? I briefed through the docs and did see a cells attribute but did not see it in the debugger walking through the inherited attributes\methods.
Thanks!

This question has an accepted answers - jump to answer

Answers

  • Focker513Focker513 Posts: 10Questions: 4Answers: 0

    Ugh, sorry did not notice the syntax for posting code.

    $('#tblMyTable tbody').on('click', 'tr', function () { 
    var tr = $(this).closest('tr'); 
    var row = table.row(tr); 
    var Id = table.row(row[0][0]).data().Id; 
    //call some ajax
     });
    
  • allanallan Posts: 61,665Questions: 1Answers: 10,095 Site admin
    Answer ✓

    I would suggest simply:

    var Id = table.row( this ).data().Id;
    

    You don't need to use $(this).closest('tr') as this is already the tr element, based on your jQuery event. You can also use the node (i.e. this) as the row selector, so line 3 in your code above, and then passing in the index is redundant.

    Regards,
    Allan

  • Focker513Focker513 Posts: 10Questions: 4Answers: 0

    Thank you sir, it works perfectly.

  • Focker513Focker513 Posts: 10Questions: 4Answers: 0

    I hate to resurface this question but I have been informed I need to stick with the legacy release. What would be the equivalent for getting the hidden column value for the legacy library?

    I was looking at fnGetColumnData function but receiving errors that the method is not supported for the object

     $('#tblMyTable tbody').on('click', 'tr', function () {
                          
                alert($(this).fnGetColumnData(0));
    });
    
  • Focker513Focker513 Posts: 10Questions: 4Answers: 0

    Disregard found my answer with fnGetData()

    var row = table.fnGetData(this).ColName;
    
    
  • allanallan Posts: 61,665Questions: 1Answers: 10,095 Site admin

    Or with the 1.1- use row().data().

    Allan

This discussion has been closed.