user2505078
user2505078

Reputation:

Datatable select row issue

I have a datatable with values. I need to get the row data which is selected. So I'm trying to get a row like demo code. But I'm getting an error "TypeError: oTable.$ is not a function".

$(document).ready(function() {
var oTable;
oTable = $("#products").dataTable({...});

oTable.$('tr').click( function () {
var data = oTable.fnGetData( this );});

But the same code without .$('tr') function is working good like below code. So what is the problem in oTable.$('tr').click()...this is way example given.

oTable.click(function() { }); // working fine

Upvotes: 0

Views: 1026

Answers (2)

TimSPQR
TimSPQR

Reputation: 2984

This is some code that I use to select a single row from a large table:

$("#patienttable tr").click(function() {
       var passthis = $(this).find("#localid").html();
       $.post("php/setsessionvariable.php",
                                       {sessionval: passthis}      );
       window.location.href = 'http://xxx.xxxxxxx/xxx/xxx.php';

When the row is clicked it grabs a single ID from the row, and passes it to a php script that makes that ID the session variable.

So I wonder if yours might work better if you put the name of the table in the parentheses.

Let's see what others might say.

Upvotes: 0

Richard
Richard

Reputation: 4415

oTable is a DataTables object, not a jQuery object. So you can't use oTable.$('tr') (which is wrong regardless).

I'm not sure how you are pin-pointing the tr you are looking for, but if you want to trigger a click event, you will need to get the jQuery object

var row = $("table#products tr[id=123]");
row.click();

Upvotes: 1

Related Questions