Filter datatables using displayed format values rather not by DB format
Filter datatables using displayed format values rather not by DB format
Using datatables in web application and successfully able to display all information from MySQL DB.
1) While Storing Date field in DB i use
Y-m-d
format and while displayd-m-Y
. When search in Datatables Example:22-03-2016
is available in DB but not able to filter in Datatables and Getting Error Message as No matching records found. Its because of incorrect format. How to achieve this in DataTables.
2) Storing status in DB as
1
or0
. While Displaying in DataTables status formatted as Active or In-Active how to filter this column in Datatables.
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url": "scripts/post.php",
"type": "POST"
},
"columns": [
{ "data": "first_name" },
{ "data": "last_name" },
{ "data": "position" },
{ "data": "office" },
{ "data": "start_date" },
{ "data": "salary" },
{ "data": "status" }
]
} );
} );
This is how i format DataTables after fetching from DB.
$columns = array(
array( 'db' => 'first_name', 'dt' => 'first_name' ),
array( 'db' => 'last_name', 'dt' => 'last_name' ),
array( 'db' => 'position', 'dt' => 'position' ),
array( 'db' => 'office', 'dt' => 'office' ),
array(
'db' => 'start_date',
'dt' => 'start_date',
'formatter' => function( $d, $row ) {
return date( 'd-m-Y', strtotime($d));
}
),
array(
'db' => 'salary',
'dt' => 'salary',
'formatter' => function( $d, $row ) {
return '$'.number_format($d);
}
),
array(
'db' => 'status',
'dt' => 'status',
'formatter' => function( $d, $row ) {
return ($d===1)?'Active':'In-Active';
}
)
);
Note:- Since I'm using DataTables Server Side Processing I don't know how to provide JsFiddle or link to live code