Can anyone explain why my "Restore" button doesn't clear column filters?

Can anyone explain why my "Restore" button doesn't clear column filters?

tangerinetangerine Posts: 3,365Questions: 39Answers: 395
edited July 12 in Free community support

Can anyone explain why my "Restore" button doesn't clear column filters?
You can see this fail at www.dbopm.com. Select any table and try a (foot-of-table) column search. Then click the "Restore" button.
This is the button code. The "clear filters" part is at the bottom.

$(document).ready(function () {
    
$.fn.dataTable.ext.buttons.restore = {

    className: "restoreButton",
    action: function ( e, dt, node, config ) {

    // Remove query string from url.
    var uri = window.location.toString();

    if (uri.indexOf("?") > 0) {
        var clean_uri = uri.substring(0, uri.indexOf("?"));
        window.history.replaceState({}, document.title, clean_uri);
    }

    var info = dt.page.info();
  
    if (! info.serverSide) {
        alert('"Restore" is not suitable for client-side processing.\nDeploy the "Refresh" button instead.' );
    return;
    } 
 
    // Clear any filters which may be present in the session.
       $.ajax({
          url: "Base/clearSessionFilters",
          type: "POST",
          data: {
              filter: controller.toLowerCase()
          }
       }) 

.fail(function (jqXHR, exception) { 
     var msg = '';
        if (jqXHR.status === 0) {
            msg = 'Not connect.\n Verify Network.';
        } else if (jqXHR.status == 404) {
            msg = 'Requested page not found. [404]';
        } else if (jqXHR.status == 500) {
            msg = 'Internal Server Error [500].';
        } else if (exception === 'parsererror') {
            msg = 'Requested JSON parse failed.';
        } else if (exception === 'timeout') {
            msg = 'Time out error.';
        } else if (exception === 'abort') {
            msg = 'Ajax request aborted.';
        } else {
            msg = 'Uncaught Error.\n' + jqXHR.responseText;
        }

    alert(msg);
    })

.done(function () { 

    // Reset the table caption.
    // The caption may have been altered by the application of a filter,
    // or the original caption differs from the controller name. 
    var caption;

    caption = rebuildCaption(controller);

    document.getElementById("caption").innerHTML = caption;   
      
    // Retrieve initial sort sequence from session storage and re-draw the table.
    var initialSort = sessionStorage.getItem('initialSort');
    var srt = JSON.parse(initialSort);

    // Do we have a multi-column sort? (Currently we are only testing for two columns.)
    if (typeof srt[1] !== 'undefined') {
        dt
            .order(srt[0],srt[1])
            .draw();   
    } else {
        dt
            .order(srt[0])
            .draw();   
    }  

    // Now clear individual column filters.
    dt
        .columns()
        .search( "" )
        .draw();

    }); // .done(function () { 
                                
    } // action

Any suggestions appreciated.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    Answer ✓

    Datatables doesn't know about the column search text inputs. You will need to clear them using Javascript or jQuery methods. This works from the console:

    $('input.column_search').val("");
    

    Try adding it on line 84.

    Kevin

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Thanks very much, Kevin, that's working for me.
    However, it doesn't work on my local version which has DT's latest updates. The upgrade gave me a lot of problems and I'm reluctant to upgrade my production version because the site would become unusable. So I have lot of figuring out to do!
    Thanks again.
    Martin

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    Possibly the jQuery selector needs changed. Inspect the input elements to find an appropriate selector. I tried this, more generic selector, on your site and it also worked:

    $('input', 'tfoot').val("");
    

    Kevin

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Thanks, your second suggestion is working for me locally. However, the "restore" function is failing with some other errors. Basically I have probably missed some failings previously, and now I don't know which of many problems were caused by the upgrade. I guess I'll have to dump some stuff on Alan's desk.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Send it over :).

    Allan

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Will do, Allan, thank you.

Sign In or Register to comment.