I’m trying to get data from the first column of the selected row,
but instead I’m annoyingly keep getting “undefined”!
Here’s my index page:
$(document).ready(function() {
var mainDataTable = $("#mainDataTable").DataTable({
"pagingType" : "full_numbers",
"processing" : true,
"serverSide" : true,
"jQueryUI" : true,
"ajax" : "/JsonData",
"columns" : [
{ "data" : "caller" },
{ "data" : "event" },
{ "data" : "receiver" },
{ "data" : "timestamp" }
]
});
$("#mainDataTable tbody").on("dblclick", "tr", function () {
var data = mainDataTable.row().data();
$("#modalDialogBody").html(
'<table class="display jqueryAllCallsDataTable" id="allCalls"><thead><tr>' +
'<th>Timestamp</th><th>Talk Duration</th><th>Receiver</th><th>Type</th></tr>' +
'</thead><tbody><!-- Data will go here --></tbody></table>');
$("#modalDialogTitle").text(data[0] + "#: All Calls");
$("#allCalls").DataTable({
"pagingType": "full_numbers",
"processing": true,
"serverSide": true,
"jQueryUI": true,
"ajax": {
"url": "/JsonData",
"data": function (d) {
d.orderByTimestampDesc = true;
d.callerId = data[0];
}
},
"searching": false,
"ordering": false,
"columns": [
{"data": "timestamp"},
{"data": "talkDuration"},
{"data": "receiver"},
{"data": "type"}
]
});
$("#modalDialog").modal();
});
$("#mainDataTable tbody").on("click", "tr", function() {
var data = mainDataTable.row().data();
$("#modalDialogBody").html(
'<table class="display jqueryCallDetailsDataTable" id="callDetails"><thead>' +
'<tr><th>Caller</th><th>Event</th><th>Receiver</th><th>Timestamp</th></tr>' +
'</thead><tbody><!-- Data will go here --></tbody></table>');
$("#modalDialogTitle").text(data[0] + "#: Regular/Cancelled call");
$("#callDetails").DataTable({
"pagingType": "full_numbers",
"processing": true,
"serverSide": true,
"jQueryUI": true,
"ajax": {
"url": "/JsonData",
"data": function (d) {
d.callerId = data[0];
}
},
"searching": false,
"ordering": false,
"columns": [
{"data": "caller"},
{"data": "event"},
{"data": "receiver"},
{"data": "timestamp"}
]
});
$("#modalDialog").modal();
});
});
Can someone review this JavaScript and tell me, what I’m doing incorrectly?
By the way, if you care, modal dialog is done using Twitter Bootstrap.
These are my included scripts:
<script type="text/javascript" src="/js/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="/css/jquery.dataTables.min.css"/>
<script type="text/javascript" src="/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="/css/dataTables.jqueryui.min.css"/>
<script type="text/javascript" src="/js/dataTables.jqueryui.min.js"></script>
<link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="/css/bootstrap-theme.min.css"/>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
3
Answers
You need to specify which row you want by providing row selector.
When using inside row click handler you can simply use
this
.When you are using a JSON source
data()
will always return one or more object literals on the formtable.row().data()
will return 1 such literal (the very first row) thusdata[0]
is undefined –data.caller
however will contain thecaller
value from the row returned.Use this code. It is working fine here.
HTML Code
Js Code