3

I have a datefilter for datatables using a calendar image and it works great. But when I clear the date it does not show all dates.

How can i make a button to show all dates which would clear the date filter?

Any help/advice on this would be great, thank you in advance.

// The plugin function for adding a new filtering routine
$.fn.dataTableExt.afnFiltering.push(
        function(oSettings, aData, iDataIndex){
            var dateStart = parseDateValue($("#dateStart").val());
            // aData represents the table structure as an array of columns, so the script access the date value 
            // in the first column of the table via aData[0]
            var evalDate= parseDateValue(aData[3]);

            if (evalDate == dateStart ) {
                return true;
            }
            else {
                return false;
            }

        });

// Function for converting a mm/dd/yyyy date value into a numeric string for comparison (example 08/12/2010 becomes 20100812
function parseDateValue(rawDate) {
    var dateArray= rawDate.split("/");
    var parsedDate= dateArray[1] + dateArray[0] + dateArray[3];
    return parsedDate;
}



$(function() {
    // Implements the dataTables plugin on the HTML table
    var $oTable= $("#example").dataTable( {
        "iDisplayLength": 20,
        "oLanguage": {
            "sLengthMenu": 'Show <select><option value="25">25</option><option value="50">50</option><option value="100">100</option><option value="200">200</option></select>'
        },
        "bJQueryUI": true,
        "aoColumns": [
                null,
                null,
                null,
                { "sType": "date" }
        ]                   
        }); 


    // The dataTables plugin creates the filtering and pagination controls for the table dynamically, so these 
    // lines will clone the date range controls currently hidden in the baseDateControl div and append them to 
    // the feedbackTable_filter block created by dataTables
    $dateControls= $("#baseDateControl").children("div").clone();
    $("#feedbackTable_filter").prepend($dateControls);

    // Implements the jQuery UI Datepicker widget on the date controls
    $('.datepicker').datepicker(
        {dateFormat: 'DD, d MM, yy', showOn: 'button', buttonImage: '../images/calendar.jpg', buttonImageOnly: true}
    );      

    // Create event listeners that will filter the table whenever the user types in either date range box or
    // changes the value of either box using the Datepicker pop-up calendar
    $("#dateStart").keyup ( function() { $oTable.fnDraw(); } );
    $("#dateStart").change( function() { $oTable.fnDraw(); } );

});

1 Answer 1

4

Well, have you tried:

$.fn.dataTableExt.afnFiltering.push(
        function(oSettings, aData, iDataIndex){
            var dateStart = parseDateValue($("#dateStart").val());
            //if filter is empty return everything
            if(dateStart === ''){
                return true;
            }
            // aData represents the table structure as an array of columns, so the script access the date value 
            // in the first column of the table via aData[0]
            var evalDate= parseDateValue(aData[3]);

            if (evalDate == dateStart ) {
                return true;
            }
            else {
                return false;
            }

        });

if this doesn't work can you post an example on jsfiddle?

EDIT - ok the problem was with parseDateValue() which was expecting a date create with /. Since you want an exact match, you can omit parseDateValue()

// The plugin function for adding a new filtering routine
$.fn.dataTableExt.afnFiltering.push(
    function(oSettings, aData, iDataIndex){
        var dateStart = $("#dateStart").val();
        //if filter is empty return everything
        if(dateStart === ''){
            return true;
        }
        // aData represents the table structure as an array of columns, so the script access the date value
        // in the first column of the table via aData[0]
        var evalDate= aData[3];

        if (evalDate == dateStart ) {
            return true;
        }
        else {
            return false;
        }

    });

fiddle here http://jsfiddle.net/eMZtV/1/

Sign up to request clarification or add additional context in comments.

5 Comments

Hi thanks for the response, i did try that before but did not work. Here is jsfiddle: jsfiddle.net/eMZtV
brilliant works like a charm, thank you. How can i get it to clear the date when i type in the search box? Also can i have an image to say show all which would clear the date box
thank you, although it does not update the dable on clear input.
Brilliant all works brilliantly. Thank you very much for your time.
This Answer helped me alot! Thank you @Nicola Peluchetti

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.