Filtering sql query by bootstrap-datepicker values
Filtering sql query by bootstrap-datepicker values
I have 2 bootstrap datepickers(#start,#end) and a Datatable which returns 150.000 rows from MS SQL Server via PHP.I want by default the start date to be 7 days earlier nad the end date to be Today.
I need to find a way to filter the Sql Query (in a PHP file) with WHERE condition between #start and #end datepicker values.If i use no filter the returned JSON is 60 MBs which freezes the browser and consequently i can't even filter the Datatable in the client.
I have already used $.fn.dataTable.ext.search.push to filter the Datatable on the client and it works ,but i want to filter the JSON output from the Sql Query before it returns to the browser.
How can i dynamically pass these date values via ajax.url in the Sql Query which is in the backend PHP file?Is serverside processing the only way or it can be done with the following code?
var table = $('#example').DataTable({
//"serverSide": true,
"ajax" : { url: 'queries.php?q=employees_full&start='+start'&end='+end ,
dataType: "json",
dataSrc: '' },
"autoWidth": true,
"deferRender": true ,
"columns": [
{ "data": "username" },............
});
var curdate = moment().subtract(7, 'day').toDate();
$( '#end' ).datepicker( 'setDate', new Date() );
$( '#start' ).datepicker( 'setDate', curdate );
var start = $('#start').val();
var end = $('#end').val();
$("#start").datepicker({ language: 'el', autoclose: true,
onchangeDate: function () {
var start = $('#start').val();
table.ajax.url('queries.php?q=employees_full&start='+start'&end='+end); //do i miss a + here??
//table.ajax.reload();
table.draw(); }});
$("#end").datepicker({ language: 'el', autoclose: true,
onchangeDate: function () {
var end = $('#end').val();
table.ajax.url('queries.php?q=employees_full&start='+start'&end='+end); //do i miss a + here??
//table.ajax.reload();table.draw(); }});
$('#start, #end').change(function () {
//table.ajax.url('queries.php?q=employees_full&start='+start+'&end='+end);
// table.ajax.reload();
table.draw();
});
1)What is the difference between table.ajax.reload() and table.draw ? Which one should i use in my case?
2)i also get an error in JSON output"Conversion failed when converting date and/or time from character string." .Probably the #start datepicker value is undefined,i don't know why??
3)Is the order of the commands right?When/how many times should i get the value of $('#start') and $('end')?What is wrong with my code?
4)The dates in the Datepicker are in Format dd/mm/yyy but in the Sql Query queries.php?q=employees_full are in Format yyyy-mm-dd.I use prepared statements and have used the following:
WHERE sql_date between (CONVERT(date,SUBSTRING(CONVERT(varchar, ?, 100), 4, 3) +SUBSTRING(CONVERT(varchar, ?, 100), 1, 3)+SUBSTRING(CONVERT(varchar, ?, 100), 7, 4)))
and
$stmt = sqlsrv_prepare( $conn, $sql , array(&$_GET["start"],&$_GET["start"],&$_GET["start"]));
Answers
Hi @mihalisp ,
A few things there.
ajax.data
to send additional information to the server scriptsajax.reload()
is used to ask the server to resend the data, which is the same asdraw()
ifserverSide
is enabled. If all the data is on the client, andajax
isn't set, thenajax.reload()
will do nothing.For the remaining ones, it would be useful to see the code. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin
Thank you Colin.
1.If i understand what you are saying ,i can use this
to pass the 2 date values from the datepickers to my php sql query
In the sql query in php file the code should be like this:
2.In my case i don't use Serverside processing in the Datatable,but the table.draw command works fine when the bootstrap datepicker changes dates.Why .draw works here?Can i have serverside processing enabled without using ssp.class.php
After a lot of research,i ended up to this.
I initialize the bootstrap datepickers and with the ajax.data option i pass the parameters(date objects) to the php Sql Query.
1)Is it better to pass the parameters as date objects instead of strings so that the conversion and comparison in the Sql Query is easier?
2)Should the initialisation of the Datepickers be before or after the Datatable?
3)Should i use 2 events .change and .on('changedate') for the datepickers so that both values to have been set before the initialisation of the datatable?
When i load the page , I get error : input1 is not defined
and after that when i pick different dates,i get TypeError: table is undefined.
I still can't find the right order of the commands i should use.
Any help would be appreciated!
In the PHP Sql Query queries.php?q=employees_full i prepare the statement with
and
What am i missing?
I think i am very close to a final solution.
I will post it as soon as i make it work!
Good to hear. Post a link or create a test case if you want any suggestions or help with any of it.
Well,'data:...' was the key to the parameters i pass to the sql query.
and the sql query:
Any comments?
input1
looks redundant, but other than that, it looks fine .Allan