我有我的DataTable,正确地从我的数据库返回数据,过滤确实有效,但我想逐列过滤数据。
这是我的代码,请帮我解释如何分别过滤每列的数据。
我正在尝试在数据表上实现多个搜索框(JSON数据即将到来并通过“sAjaxSource”从服务器端填满表格),请参阅以下代码了解详细信息。
<input type="hidden" name="url" id="url" value="ajaxRequest.php" />
<script src="datatable/datatable.js" ></script>
<script src="datatable/jeditable.js"></script>
<script src="datatable/jvalidate.js"></script>
<script src="datatable/datatable_editor.js"></script>
<script type="text/javascript" charset="utf-8">
jQuery(document).ready( function () {
jQuery.fn.dataTable.ext.pager.full_numbers = function ( page, pages ) {
function _range( len, start ) {
var out = [];
var end;
if ( start === undefined ) {
start = 0;
end = len;
}
else {
end = start;
start = len;
}
for ( var i=start ; i<end ; i++ ) {
out.push( i );
}
return out;
}
function _numbers ( page, pages ) {
var
numbers = [],
buttons = jQuery.fn.dataTable.ext.pager.numbers_length,
half = Math.floor( buttons / 2 ),
i = 1;
if ( pages <= buttons ) {
numbers = _range( 0, pages );
}
else if ( page <= half ) {
numbers = _range( 0, buttons-2 );
}
else if ( page >= pages - 1 - half ) {
numbers = _range( pages-(buttons-2), pages );
} else {
numbers = _range( page-half+2, page+half-1 );
}
numbers.DT_el = 'span';
return numbers;
}
return ['previous', _numbers(page, pages), 'next'];
}
var table = jQuery('#expample').DataTable({
pagingType: "full_numbers",
"bServerSide": true,
"bProcessing": true,
"bLengthChange": false,
"bInfo": false,
"sAjaxSource": jQuery('#url').val()
})
});
</script>
<table cellpadding="3" cellspacing="0" border="0" style="width: 67%; margin: 0 auto 2em auto;">
<thead>
<tr>
<th>Target</th>
<th>Search text</th>
<th>Treat as regex</th>
<th>Use smart search</th>
</tr>
</thead>
<tbody>
<tr id="filter_global">
<td>Global search</td>
<td align="center"><input type="text" class="global_filter" id="global_filter"></td>
<td align="center"><input type="checkbox" class="global_filter" id="global_regex"></td>
<td align="center"><input type="checkbox" class="global_filter" id="global_smart" checked="checked"></td>
</tr>
<tr id="filter_col1" data-column="0">
<td>Column - Name</td>
<td align="center"><input type="text" class="column_filter" id="col0_filter"></td>
<td align="center"><input type="checkbox" class="column_filter" id="col0_regex"></td>
<td align="center"><input type="checkbox" class="column_filter" id="col0_smart" checked="checked"></td>
</tr>
<tr id="filter_col2" data-column="1">
<td>Column - Position</td>
<td align="center"><input type="text" class="column_filter" id="col1_filter"></td>
<td align="center"><input type="checkbox" class="column_filter" id="col1_regex"></td>
<td align="center"><input type="checkbox" class="column_filter" id="col1_smart" checked="checked"></td>
</tr>
<tr id="filter_col3" data-column="2">
<td>Column - Office</td>
<td align="center"><input type="text" class="column_filter" id="col2_filter"></td>
<td align="center"><input type="checkbox" class="column_filter" id="col2_regex"></td>
<td align="center"><input type="checkbox" class="column_filter" id="col2_smart" checked="checked"></td>
</tr>
<tr id="filter_col4" data-column="3">
<td>Column - Age</td>
<td align="center"><input type="text" class="column_filter" id="col3_filter"></td>
<td align="center"><input type="checkbox" class="column_filter" id="col3_regex"></td>
<td align="center"><input type="checkbox" class="column_filter" id="col3_smart" checked="checked"></td>
</tr>
<tr id="filter_col5" data-column="4">
<td>Column - Start date</td>
<td align="center"><input type="text" class="column_filter" id="col4_filter"></td>
<td align="center"><input type="checkbox" class="column_filter" id="col4_regex"></td>
<td align="center"><input type="checkbox" class="column_filter" id="col4_smart" checked="checked"></td>
</tr>
<tr id="filter_col6" data-column="5">
<td>Column - Salary</td>
<td align="center"><input type="text" class="column_filter" id="col5_filter"></td>
<td align="center"><input type="checkbox" class="column_filter" id="col5_regex"></td>
<td align="center"><input type="checkbox" class="column_filter" id="col5_smart" checked="checked"></td>
</tr>
</tbody>
</table>
<table id="example" class="wp-list-table widefat fixed pages display">
<thead>
<tr>
<th scope="col" id="no" class="manage-column column-no" ><a href="#_"><span>S.NO</span></a></th>
<th scope="col" id="name" class="manage-column column-name" ><a href="#_"><span>Name</span></a></th>
<th scope="col" id="city" class="manage-column column-city" ><a href="#_"><span>City</span></a></th>
<th scope="col" id="address" class="manage-column column-add" ><a href="#_"><span>Address</span></a></th>
</tr>
</thead>
</table>
<?php
include('../../../wp-config.php');
global $wpdb;
/*
* DataTables example server-side processing script.
*
* Please note that this script is intentionally extremely simply to show how
* server-side processing can be implemented, and probably shouldn't be used as
* the basis for a large complex system. It is suitable for simple use cases as
* for learning.
*
* See http://datatables.net/usage/server-side for full details on the server-
* side processing requirements of DataTables.
*
* @license MIT - http://datatables.net/license_mit
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
// DB table to use
$table = $wpdb->prefix.'table_name';
// Table's primary key
//print_r($_REQUEST);
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => 'id', 'dt' => 0 ),
array( 'db' => 'name', 'dt' => 1 ),
array( 'db' => 'city', 'dt' => 2 ),
array( 'db' => 'address', 'dt' => 3 )
);
/*
* 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
*/
$aColumns = array( 'name','city','address');
$aColumnsID = array( 'id');
/*
* 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<count($aColumns) ; $i++ )
{
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
{
$sWhere .= $aColumns[$i]." LIKE '%". $_GET['sSearch'] ."%' OR ";
}
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
/*
* 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<intval( $_GET['iSortingCols'] ) ; $i++ )
{
if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
{
$sOrder .= $aColumnsID[ intval( $_GET['iSortCol_'.$i] ) ]."
".($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
}
}
$sOrder = substr_replace( $sOrder, "", -2 );
if ( $sOrder == "ORDER BY" )
{
$sOrder = "";
}
}
if( $sWhere ) {
$getResponse = $wpdb->get_results("SELECT * FROM ".$table." ".$sWhere." $sOrder $sLimit",ARRAY_A);
} else {
if( $where != "" ) {
$getResponse = $wpdb->get_results("SELECT * FROM ".$table." $sOrder $sLimit",ARRAY_A);
} else {
$getResponse = $wpdb->get_results("SELECT * FROM ".$table." $sOrder $sLimit",ARRAY_A);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/
foreach( $getResponse as $key=>$rows ) {
//print($rows);
$data[] = array(
'id' => $rows['id']-1,
'name' => $rows['name'],
'city' => $rows['city'],
'address' => $rows['address']
);
}
if( $sWhere ) {
$count = $wpdb->get_results( 'SELECT COUNT(id) as total_count FROM '.$table." ".$sWhere,ARRAY_A );
} else {
$count = $wpdb->get_results( 'SELECT COUNT(id) as total_count FROM '.$table,ARRAY_A );
}
$response = array(
'draw' => isset ( $_GET['draw'] ) ?
intval( $_GET['draw'] ) :
0,
'recordsTotal' => $count[0]['total_count'],
'recordsFiltered' => $count[0]['total_count'],
'data' => data_output($columns,$data)
);
echo json_encode($response);
function data_output ( $columns, $data )
{
$out = array();
for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
$row = array();
for ( $j=0, $jen=count($columns) ; $j<$jen ; $j++ ) {
$column = $columns[$j];
// Is there a formatter?
if ( isset( $column['formatter'] ) ) {
$row[ $column['dt'] ] = $column['formatter']( $data[$i][ $column['db'] ], $data[$i] );
}
else {
$row[ $column['dt'] ] = $data[$i][ $columns[$j]['db'] ];
}
}
$out[] = $row;
}
return $out;
}
function pluck ( $a, $prop )
{
$out = array();
for ( $i=0, $len=count($a) ; $i<$len ; $i++ ) {
$out[] = $a[$i][$prop];
}
return $out;
}