Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatables get row Data without clicking on it

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?

like image 738
Silverwulf Avatar asked Dec 15 '22 16:12

Silverwulf


1 Answers

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

like image 119
Daniel Avatar answered Dec 26 '22 18:12

Daniel