4

I'm using DataTables to show a list of messages.

I need a way to get a specific row of data based off an id being passed to the page on the Query string.

ex. www.webpage.com?id=2

I already have the id in a jQuery variable. Now I just need to access the DataTable row associated with that id.

Basically, I need to reference the row, without having clicked on it.

Any suggestions?

Silverwulf
  • 109
  • 1
  • 2
  • 10
  • have you tried the `fnSelect()` function from dataTables? – dnagirl Dec 11 '12 at 18:53
  • I haven't ... what does that one do? I did look over their API list but I don't see that one currently listed? Is it new or old perhaps? – Silverwulf Dec 11 '12 at 18:55
  • Do you just need a reference to the element or do you mean the row's data in an array or something? – Gigo Dec 11 '12 at 18:59
  • That depends Gigo ... on what DataTables needs, in order to reference the data in that row. Currently, I'm using var aData = oTable.fnGetData(nTr); to get the data when I'm actually clicking on the row. So I need a way to replace the nTr, which is just "this" when I click on the row. – Silverwulf Dec 11 '12 at 19:03
  • @CraigThompson: apologies, fnSelect is a function in TableTools, which is a dataTables add-on. I use it so often, I don't differentiate. Here's the reference: http://www.datatables.net/extras/tabletools/api – dnagirl Dec 11 '12 at 19:30
  • Oh, I see. Maybe I'll have to look into that if I don't get one of these others to work. Thank you for the information :-) – Silverwulf Dec 11 '12 at 19:33

4 Answers4

8

One way could be to use fnGetPosition and fnGetData

var rowIndex = table.fnGetPosition( $("some selector").closest('tr')[0] );
//some selector = should select some hidden element inside a row that 
//contains the relevant id
var aData = table.fnGetData( rowIndex  );
alert(aData[0]);// will show first column data

Here a working jsfiddle example of an external button that selects row with specific ID

Another example that select the row with specific ID on page load(ready)jsfiddle example N#2

Take a look at the function

$("#wow").click(function() {

    var rowIndex = table.fnGetPosition($("#example input[value=\'TridentVal\']").closest('tr')[0]);
    alert(rowIndex);
    var aData = table.fnGetData(rowIndex);
    alert(aData[0]); // will show first column data
});​

This is the way to select an input with relevant data... :

$("#example input[value=\'TridentVal\']")

example is table id , replace TridentVal with the needed ID

Daniel
  • 36,833
  • 10
  • 119
  • 200
  • This looks promising ... how would I go about referencing the specific row I need using fnGetPosition? For Example ... the row where ID = 2? – Silverwulf Dec 11 '12 at 19:07
  • Do I just need to say .closest('tr')[2] ? – Silverwulf Dec 11 '12 at 19:08
  • nope, leave the `.closest('tr')[0]` as it is... do you have a column that holds the id of the row ? if not add some hidden `input` (`style="display:none"`) with `value="ID_OF_THE_ROW"`... – Daniel Dec 11 '12 at 19:11
  • yes, the first column in the Datatable is a hidden column containing the messageID – Silverwulf Dec 11 '12 at 19:12
  • I'm just not familiar with the way I would replace "some selector" with the appropriate row id. – Silverwulf Dec 11 '12 at 19:19
  • @CraigThompson , I have updated my answer with working example... http://jsfiddle.net/vedmack/CMR5Y/ – Daniel Dec 11 '12 at 19:36
  • As I said before, there is no button ... there is no click. There is only an ID that corresponds to a table row with the same id number in a hidden column named 'MessageID' – Silverwulf Dec 11 '12 at 22:55
  • posted another solution - that do the same on page load http://jsfiddle.net/vedmack/CMR5Y/3/ , You better explain yourself better next time in your question... – Daniel Dec 12 '12 at 06:23
3

First you have to get your table row using jQuery.

var $rowNode = $('#myTable').find('tbody tr:eq(0)').get(0);

Then you use fnGetData to get the row data.

var data = table.fnGetData($rowNode);

fnGetData acccepts a row node or an integer, so you can pass the row index as parameter too.

demo

Ricardo Alvaro Lohmann
  • 26,031
  • 7
  • 82
  • 82
1

If you use the TableTools extension, then you can use fnSelect(). From the docs:

$(document).ready( function () {
    $('#example1').dataTable( {
        "sDom": 'T<"clear">lfrtip',
        "oTableTools": {
            "sRowSelect": "single"
        }
    } );

    // Select the first row in the table automatically
    var oTT = TableTools.fnGetInstance( 'example1' );
    oTT.fnSelect( $('#example tbody tr')[0] );
});

You'd of course want to modify the selector so it chooses the row that has your id.

dnagirl
  • 20,196
  • 13
  • 80
  • 123
0

fnGetData is now obsolete. this documents the cell().data() access pattern in the current version https://datatables.net/reference/api/cell().data()

xeo
  • 807
  • 2
  • 7
  • 25