我有数据表使用从服务器使用JSON数据的方法,并插入 jquery.dataTables.yadcf.js 来创建过滤器。
我的问题:我无法在此处应用range_date。但其他人工作正常。
有人可以帮我吗?我将非常感激!
提前致谢,Guruh
以下是我的截图:
我在date_created字段中输入日期
这是来自萤火虫
这是我的代码: 的 HTML
<div id="dynamic">
<table cellpadding="0" cellspacing="0" border="0"class="display" id="example">
<thead>
<tr>
<th width="20%">ccode</th>
<th width="25%">city</th>
<th width="25%">longitude</th>
<th width="15%">latitude</th>
<th width="10%">date_created</th>
<th width="5%">show</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="4">Loading data from server</td>
</tr>
</tbody>
<tfoot>
<tr>
<th width="20%">ccode</th>
<th width="25%">city</th>
<th width="25%">longitude</th>
<th width="15%">latitude</th>
<th width="10%">date_created</th>
<th width="5%">show</th>
</tr>
</tfoot>
</div>
这是我的 Js
$(document).ready(function() {
'use strict';
var oTable = $('#example').dataTable( {
"bStateSave": true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "scripts/json.php",
"aoColumns": [
{ "mData": "ccode" },
{ "mData": "city" , "sClass" : "left"},
{ "mData": "longitude" },
{ "mData": "latitude" },
{ "mData": "date_created" },
{ "mData": "active" }
],
"sServerMethod": "POST",
"fnServerParams": function ( aoData ) {
aoData.push( { "name": "ccdode", "value": "my_value" } );
},
} ).yadcf([
{column_number : 0},
{column_number : 1, text_data_delimiter: ",", filter_type: "auto_complete"},
{column_number : 2, text_data_delimiter: ",", filter_type: "auto_complete"},
{column_number : 3, text_data_delimiter: ",", filter_type: "auto_complete"},
{column_number : 4, filter_type: "range_date"},
{column_number : 5, data: ["Y", "N"], filter_default_label: "Select Y/N"}
]);
} );
这是JSON:
{
"sEcho": 3,
"iTotalRecords": "1000",
"iTotalDisplayRecords": "1000",
"aaData": [
{
"ccode": "1",
"city": "Azusa",
"longitude": "-169.14213877050466",
"latitude": "-1.3856648237232747",
"date_created": "02-07-2014",
"active": "N"
},
{
"ccode": "2",
"city": "Ber/keley",
"longitude": "-15.017872122538762",
"latitude": "38.688940015230145",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "3",
"city": "Hawaiian Gardens",
"longitude": "-101.38987014525152",
"latitude": "-64.80444101812495",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "4",
"city": "Visalia",
"longitude": "-10.591368907170363",
"latitude": "29.623179439279042",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "5",
"city": "Vernon",
"longitude": "143.19045912656412",
"latitude": "-77.62525761370941",
"date_created": "04-06-2014",
"active": "N"
},
{
"ccode": "6",
"city": "Huntington Beach",
"longitude": "71.21455652709275",
"latitude": "33.116647136978",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "7",
"city": "Oceanside",
"longitude": "44.98838778597249",
"latitude": "16.44800050127145",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "8",
"city": "Corning",
"longitude": "-102.38650166780123",
"latitude": "-65.24394675193176",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "9",
"city": "San Fernando",
"longitude": "-56.53495746604091",
"latitude": "38.5346252565036",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "10",
"city": "Calabasas",
"longitude": "-46.792450689702264",
"latitude": "30.68512166915569",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "11",
"city": "San Diego",
"longitude": "82.80392648304678",
"latitude": "-28.717255280645162",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "12",
"city": "Santa Maria",
"longitude": "-120.02771516153311",
"latitude": "28.496009377002906",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "13",
"city": "Hermosa Beach",
"longitude": "-144.40605392510207",
"latitude": "-89.07654935580064",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "14",
"city": "Palm Springs",
"longitude": "83.07759482737896",
"latitude": "-45.35603641599401",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "15",
"city": "Wasco",
"longitude": "-112.34512507402464",
"latitude": "-39.90065249226572",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "16",
"city": "Arvin",
"longitude": "-141.19484737338607",
"latitude": "19.830285023148377",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "17",
"city": "Costa Mesa",
"longitude": "-118.49507287338899",
"latitude": "7.513624741943076",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "18",
"city": "Belvedere",
"longitude": "-31.85176765892828",
"latitude": "-3.8283838161222405",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "19",
"city": "Duarte",
"longitude": "24.225348936031935",
"latitude": "-54.93776485510166",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "20",
"city": "Crescent City",
"longitude": "149.20314619747393",
"latitude": "40.009834252730855",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "21",
"city": "Rocklin",
"longitude": "146.44484037552638",
"latitude": "-32.30893625076091",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "22",
"city": "Maywood",
"longitude": "-51.75221537956844",
"latitude": "35.527697679065625",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "23",
"city": "Highland",
"longitude": "-1.7760828416806476",
"latitude": "-29.990338116028326",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "24",
"city": "Menifee",
"longitude": "-72.67717919567336",
"latitude": "59.82855451204614",
"date_created": "04-06-2013",
"active": "N"
},
{
"ccode": "25",
"city": "Artesia",
"longitude": "143.2917103009467",
"latitude": "-69.60801730285395",
"date_created": "04-06-2013",
"active": "N"
}
]
}
这是 json.php :
<?php
$aColumns = array( 'ccode', 'city', 'longitude', 'latitude', 'date_created', 'active' );
$sIndexColumn = "ccode";
$sTable = "cities";
$gaSql['user'] = "root";
$gaSql['password'] = "";
$gaSql['db'] = "largedata";
$gaSql['server'] = "localhost";
function fatal_error ( $sErrorMessage = '' )
{
header( $_SERVER['SERVER_PROTOCOL'] .' 500 Internal Server Error' );
die( $sErrorMessage );
}
if ( ! $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) )
{
fatal_error( 'Could not open connection to server' );
}
if ( ! mysql_select_db( $gaSql['db'], $gaSql['link'] ) )
{
fatal_error( 'Could not select database ' );
}
$sLimit = "";
if ( isset( $_POST['iDisplayStart'] ) && $_POST['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".intval( $_POST['iDisplayStart'] ).", ".
intval( $_POST['iDisplayLength'] );
}
$sOrder = "";
if ( isset( $_POST['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i<intval( $_POST['iSortingCols'] ) ; $i++ )
{
if ( $_POST[ 'bSortable_'.intval($_POST['iSortCol_'.$i]) ] == "true" )
{
$iColumnIndex = array_search( $_POST['mDataProp_'.$_POST['iSortCol_'.$i]], $aColumns );
$sOrder .= "`".$aColumns[ $iColumnIndex ]."` ".
($_POST['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
$sWhere = "";
if ( isset($_POST['sSearch']) && $_POST['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( isset($_POST['bSearchable_'.$i]) && $_POST['bSearchable_'.$i] == "true" )
{
$sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_POST['sSearch'] )."%' OR ";
}
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( isset($_POST['bSearchable_'.$i]) && $_POST['bSearchable_'.$i] == "true" && $_POST['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$iColumnIndex = array_search( $_POST['mDataProp_'.$i], $aColumns );
$sWhere .= $aColumns[$iColumnIndex]." LIKE '%".mysql_real_escape_string($_POST['sSearch_'.$i])."%' ";
}
}
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
$sQuery = "
SELECT COUNT(".$sIndexColumn.")
FROM $sTable
";
$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
$output = array(
"sEcho" => intval($_POST['sEcho']),
"iTotalRecords" => $iTotal,
"iTotalDisplayRecords" => $iFilteredTotal,
"aaData" => array()
);
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$row = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( $aColumns[$i] == "version" )
{
$row[ $aColumns[$i] ] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
}
else if ($aColumns[$i] == 'date_created')
{
$row[ $aColumns[$i] ] = date( 'd-m-Y', strtotime($aRow[ $aColumns[$i] ]) );
}
else if ( $aColumns[$i] != ' ' )
{
$row[ $aColumns[$i] ] = $aRow[ $aColumns[$i] ];
}
}
$output['aaData'][] = $row;
}
echo json_encode( $output );
?>