Javascript 无法为具有服务器端JSON的数据表应用范围\日期
我有datatables,它使用一种使用来自服务器的JSON数据的方法,还使用pluginjquery.datatables.yadcf.js来创建过滤器 我的问题:我无法在此应用范围\日期。但其他人工作得很好 有人能帮我吗?我将非常感激 先谢谢你,古鲁 这是我的截图:Javascript 无法为具有服务器端JSON的数据表应用范围\日期,javascript,php,jquery,json,jquery-datatables,Javascript,Php,Jquery,Json,Jquery Datatables,我有datatables,它使用一种使用来自服务器的JSON数据的方法,还使用pluginjquery.datatables.yadcf.js来创建过滤器 我的问题:我无法在此应用范围\日期。但其他人工作得很好 有人能帮我吗?我将非常感激 先谢谢你,古鲁 这是我的截图: 我在date_created字段中键入了日期 这是firebug的 这是我的密码: HTML <div id="dynamic"> <table cellpadding="0" cellspacing="0
我在date_created字段中键入了日期 这是firebug的 这是我的密码: 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>
以下是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 );
?>
您能告诉我们如何将日期值传递给查询吗?如果可能的话,将查询添加到问题中。好的。我已经添加了json.phpYes,我看到了,表中创建的数据类型是什么?如果其日期
或日期时间
,则将该格式与您正在使用的格式一起使用将不会在查询中起作用。您输入的格式类似于d-m-Y
或m-d-Y
,如果mysqlYes中的字段是date
或dattime
格式,则需要将其转换为Y-m-d
,然后再与DB进行比较。我的输入格式是d-m-Y,数据类型是varchar。在我将其转换为json->date('d-m-Y',strotime($aRow[$aColumns[$I]])之前,我已经在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 );
?>