[英]JQuery Datatables display hidden values
我要完成的工作是:使用數據表,我想顯示從數據庫中提取的一些信息,然后,使用數據表隱藏的行詳細信息,我想顯示其余信息。 第一部分已經完成並且工作正常,但是第二部分並沒有取得任何成功。
這是返回json代碼的php代碼(我認為它工作正常):
<?php
try {
$conn = require_once 'dbConnect.php';
$sql = "SELECT email, lastName, firstName, dateRegistered, state FROM Users";
$result = $conn->prepare($sql) or die ($sql);
if(!$result->execute()) return false;
if($result->rowCount() > 0) {
$json = array();
while($row = $result->fetch()){
$json[] = array(
'email' => $row['email'],
'lastName' => $row['lastName'],
'firstName' => $row['firstName'],
'dateRegistered' => $row['dateRegistered'],
'state' => $row['state']
);
}
$response = array(
"iTotalRecords" => strval(count($json)),
"iTotalDisplayRecords" => strval(count($json)),
"aaData" => $json
);
echo json_encode($response);
}
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
?>
在我的數據表中,我想顯示除狀態以外的所有信息,狀態將被隱藏。
/*
* Created by: Sebastian Bonilla
* Date: 12-12-2013
* Version: 0.9
*/
/* Formating function for row details */
function fnFormatDetails ( oTable, nTr )
{
// Creates the details block
var aData = oTable.fnGetData( nTr );
var sOut = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">';
// I tried to access states with aData[5] but it didn't work. With any value it says 'undefined'
sOut += '<tr><td>State:</td><td>' + aData[5] + '</td></tr>';
sOut += '<tr><td>Link to source:</td><td>Could provide a link here</td></tr>';
sOut += '<tr><td>Extra info:</td><td>And any further details here (images etc)</td></tr>';
sOut += '</table>';
// return a html formatted string with the data to display
return sOut;
}
$(document).ready(function() {
/*
$('#datatables').dataTable( {
"bProcessing": true,
"sAjaxSource": 'process.php'
} );
*/
// Create object oTable which is a datatable
// makes an ajax call to process.php and populate the headers
var oTable = $('#datatables').dataTable( {
"aaSorting": [[2, 'asc']],
"bProcessing": true,
"bServerSide": true,
"sPaginationType": "full_numbers",
"sAjaxSource": "process.php",
"aoColumns": [
{
"mData": null,
"aTargets": [0],
"bSerchable": false,
"bSortable": false,
"sDefaultContent": '<div class="expand /">',
"sWidth": "30px"
},
{
"mDataProp": "email",
"aTargets": [1],
"bSearchable": true,
"bSortable": true
},
{
"mDataProp": "lastName",
"aTargets": [2],
"bSearchable": true,
"bSortable": true
},
{
"mDataProp": "firstName",
"aTargets": [3],
"bSearchable": true,
"bSortable": true
},
{
"mDataProp": "dateRegistered",
"aTargets": [4],
"sClass": "center",
"bSearchable": true,
"bSortable": true
}
]
} );
$('#datatables tbody').on('click', 'td div.expand', function () {
var nTr = $(this).parents('tr')[0];
if (oTable.fnIsOpen(nTr)) {
$(this).removeClass('open');
oTable.fnClose(nTr);
} else {$.fn.dataTableExt.sErrMode = 'throw' ;
$(this).addClass('open');
oTable.fnOpen(nTr, fnFormatDetails(oTable, nTr), 'details');
}
});
});
因此,正如我所說,我能夠顯示所有想要的值,但是在fnFormatDetails函數中時,我無法訪問和顯示狀態。
有什么建議么?
謝謝。
沒關系,我只是能夠弄清楚!
它是這樣工作的:不用使用像aData [5]這樣的索引訪問該字段,而是使用該字段的名稱:aData ['state']完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.