Filter Datatable using date picker

Filter Datatable using date picker

AkkuAkku Posts: 1Questions: 1Answers: 0

I want to filter the data table based on the date range selected from date picker. For e.g I want to display all the data of a particular month. I don't have a start and end date column in my table. Just a due date column is there which need not to be tied to date filter. Is it possible?

Answers

  • fabioberettafabioberetta Posts: 74Questions: 23Answers: 4

    Hi Akku,

    I think you can.

    This is how I did it for a similar case. In my case I have a logs table and I want to select the log of a specific day.

    I have a date picker, when the datepicker changes the onChange event is intercepted and the following code is executed:

    $('#dpReferenceDate').on('changeDate', function(e) {                            
                    tableLogs.ajax.reload();
            });
    

    Essentially when the datepicker changes, the corresponding data table is reloaded.

    In order to instruct the dataTable to take the selected date the dataTable ajax property lis set as follows:

    tableLogs = $('#logs').DataTable({
                        
                ajax:{
                    url: '_php/dtLogs.php',
                    data: function (d) {
                            var today = new Date();
    
                            d.selectedDate = ($('#dpReferenceDate').val() ? $('#dpReferenceDate').val() : today.getDate() );
                    type: 'POST'
                },
    …
    
    

    This piece of code adds to the POST call to dtLogs.php the date that is contained into the datepicker (or the current client date if there is no date selected).

    The dtLogs.php code contains the following:

    $_selected_date = date('Y-m-d', strtotime($_POST['selectedDate']));
    
    $_selected_date_plus_one_day = date( 'Y-m-d 00:00:00', strtotime($_selected_date . $_span ));
    

    dtLogs.php collects the date that is sent via POST and calculates this date + 1 day.

    Finally the dtLogs.php uses the following where clause to make sure it selects the right logs (it selects all the logs between the selected date + 1 day).

        ->where( 'log.log_date_time', $_selected_date, '>=' )
        ->where( 'log.log_date_time', $_selected_date_plus_one_day, '<=' )
    

    Hope this helps,
    f

  • noname24noname24 Posts: 1Questions: 0Answers: 0

    Can i have your files pls ineed for my project

This discussion has been closed.