我在使用wordpress中的服务器端处理时遇到了bootstrap datatable 的问题 . 我创建了一个具有以下 datatable 代码的模板 . 当我调用它返回的函数时:
{“sEcho”:0,“iTotalRecords”:“2452”,“iTotalDisplayRecords”:“2452”,“aaData”:[[“34”,“David”,“Carlson”,“carlson996 @ msn.com”, “博尔德”, “科罗拉多”, “USA”, “303”, “1”, “(NULL)”],[ “37`”, “测试”, “测试”, “teste@test.net”,”试验”, “NC”, “USA”, “0”, “0”, “测试”]
但 datatable 除了"Processing"外没有打印任何内容 . 然后我检查了控制台并复制了网址并粘贴到网址栏中它返回0.并在控制台中说
无法读取未定义的属性“长度”
这是我的数据表代码
jQuery(document).ready(function() {
jQuery('#testing').DataTable({
"bFilter":true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "http://localhost/site/wp-admin/admin-ajax.php?action=fn_my_ajax_dataloader",
"aaSorting": [[0, 'desc']],
]
});
} );
这是我在function.php中的Wordpress函数
add_action('wp_ajax_fn_my_ajax_dataloader', 'fn_my_ajax_dataloader');
function fn_my_ajax_dataloader()
{
global $wpdb;
$aColumns = array( "MemberID","FirstName","LastName","Email","City",
"State","Country","Phone","MemberStatus","SpouseName"
);
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "MemberID";
/* DB table to use */
$sTable = "wp_member_table";
/*
* Paging
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
intval( $_GET['iDisplayLength'] );
}
/*
* Ordering
*/
$sOrder = "";
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i
{
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
{
$sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."` ".
($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
/*
* Filtering
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here, but concerned about efficiency
* on very large tables, and MySQL's regex functionality is very limited
*/
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i
{
$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
/* Individual column filtering */
for ( $i=0 ; $i
{
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
/*
* SQL queries
* Get data to display
*/
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
";
//$rResult = mysql_query( $sQuery ) or die(mysql_error());
$rResult = $wpdb->get_results($sQuery, ARRAY_A);
/* Data set length after filtering */
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = $wpdb->get_results($sQuery, ARRAY_A);
$iFilteredTotal = $rResultFilterTotal[0]['FOUND_ROWS()'];
//print_r($iFilteredTotal);die;
/* Total data set length */
$sQuery = "
SELECT COUNT($sIndexColumn)
FROM $sTable
";
//$rResultTotal = mysql_query( $sQuery ) or die(mysql_error());
$rResultTotal = $wpdb->get_results($sQuery, ARRAY_A);
//$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $rResultTotal[0]['COUNT(MemberID)'];
//print_r($iTotal);die;
/*
* Output
*/
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
foreach($rResult as $aRow)
{
$row = array();
for ( $i=0 ; $i
{
//if ( $aColumns[$i] == "version" )
//{
/* Special output formatting for 'version' column */
//$row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
//}
//else if ( $aColumns[$i] != ' ' )
//{
/* General output */
$row[] = $aRow[ $aColumns[$i] ];
//}
}
$output['aaData'][] = $row;
}
//print_r($output);die;
echo json_encode( $output );
}