reset filter on filtered result set

reset filter on filtered result set

sonicbugsonicbug Posts: 26Questions: 0Answers: 0
edited May 2011 in General
Hi Allan:

Since I added additional logic to my js, the input filter no longer works as expected. This only happens when I have the option selected that I added to the select list. For example, I have added logic (an option called "most recent" to the select list) that shows records that were posted today or yesterday - which works great. however, I noticed that when I try to search for a data I get "no records to display" message. In other words the filter (input box) breaks when I have the "most recent" option selected, but works fine when any of the default options are selected. I guess I need a way of telling datatables to reset the filter on input:focus for the filter. Please see code below:




Any suggestions are greatly appreciated.

Thanks in advance.

Replies

  • sonicbugsonicbug Posts: 26Questions: 0Answers: 0
    [code]
    $(function () {

    var rand = "";
    var str = "";


    /* grab today's date */
    var dateObj = new Date();
    var year = dateObj.getFullYear();
    var monthNames = ["January", "February", "March", "April", "May", "June", "July",
    "August", "September", "October", "November", "December"];
    var month = monthNames[dateObj.getMonth()];
    var day = dateObj.getDate();
    var yesterday = day - 1;

    /* get string equilvent of date */
    str_year = '';
    str_year += year;
    str_month = '';
    str_month += month;
    str_day = '';
    str_day += day;
    str_yesterday = '';
    str_yesterday += yesterday;



    //rand = $.random(5000);
    /* grab data and place it into an array */
    $.get("forecast_centre/csv/tab_ui_1/daily_flood_reports/daily_flood_reports.csv" + "?" + randomize(), function (data) {
    csv_flood_reports = $.csv()(data);


    /*
    for (var x in csv_flood_reports) {
    str += "\n" + csv_flood_reports[x];
    }
    console.log("Test Results: " + "\n" + str);
    */

    /* generate a table - dataTable */
    $('#flood_reports').html('');
    var oTable = $('#daily_flood_reports_table').dataTable({
    "iDisplayLength": 9999,
    //"iCookieDuration": 0,
    "oLanguage": {
    "sSearch": "Search Flood Reports by date:",
    "sLengthMenu": 'Show ' +
    'most recent' +
    '10' +
    '25' +
    '50' +
    '100' +
    'All' +
    ' entries'
    },
    "sPaginationType": "full_numbers",
    "aaSorting": [[0, "desc", 0], [1, "desc", 0], [2, "desc", 0]],
    "aaData": csv_flood_reports,
    "aoColumns": [
    { "sTitle": "Year" }, // "sType": "date"
    {"sTitle": "Month", "sType": "month" },
    { "sTitle": "Day" },
    { "sTitle": "English" },
    { "sTitle": "Français" }
    ]
    });
    oTable.fnFilter(str_year, 0); // year
    oTable.fnFilter(str_month, 1); // month
    oTable.fnFilter(str_day, 2); // day

    var empty = $("#daily_flood_reports_table tr td").hasClass('dataTables_empty'); // determine if table is empty
    //alert(empty);
    var flag;
    if (empty === true) {

    oTable.fnFilter(str_year, 0); // year
    oTable.fnFilter(str_month, 1); // month
    oTable.fnFilter(str_yesterday, 2); // yesterday

    $('#daily_flood_reports_sl option:selected').addClass('yesterday'); // add class yesterday to option
    flag = $('#daily_flood_reports_sl option:selected').attr('class');
    $(".dataTables_info").hide(); // do not display information about table
    } else {

    oTable.fnFilter(str_year, 0); // year
    oTable.fnFilter(str_month, 1); // month
    oTable.fnFilter(str_day, 2); // today

    $('#daily_flood_reports_sl option:selected').addClass("today");
    flag = $('#daily_flood_reports_sl option:selected').attr('class');
    $(".dataTables_info").hide();
    }

    // handle onchange event
    $("#daily_flood_reports_sl").change(function (evt) {
    evt.preventDefault();
    var op = $("#daily_flood_reports_sl option:selected").val();
    if (op === '9999' && flag === 'yesterday') {
    oTable.fnFilter(str_year, 0); // year
    oTable.fnFilter(str_month, 1); // month
    oTable.fnFilter(str_yesterday, 2); // yesterday
    $(".dataTables_info").hide();
    } else if (op === '9999' && flag === 'today') {
    oTable.fnFilter(str_year, 0); // year
    oTable.fnFilter(str_month, 1); // month
    oTable.fnFilter(str_day, 2); // today
    $(".dataTables_info").hide();
    } else {
    oTable.fnFilter('', 0);
    oTable.fnFilter('', 1);
    oTable.fnFilter('', 2);
    $(".dataTables_info").show();
    }
    });

    });

    });

    [/code]
This discussion has been closed.