jQuery DataTables and Columnfilterwidget Reset all filters button

孤者浪人 提交于 2019-12-20 07:18:40

问题


I am new to Javascript.So my question is a little silly.

I was looking for Reset all filter button for Columnfilterwidget and found this code.

$.fn.dataTableExt.oApi.fnResetAllFilters = function (oSettings, bDraw/default true/) {
for(iCol = 0; iCol < oSettings.aoPreSearchCols.length; iCol++) {
oSettings.aoPreSearchCols[ iCol ].sSearch = '';
}
$('.filter-term').remove();
oSettings.oPreviousSearch.sSearch = '';
if(typeof bDraw === 'undefined') bDraw = true;
if(bDraw) this.fnDraw();
}

I need to bind it to a button to make it work.

$(document).ready(function(){
$("button").click(function(){
 $.fn.dataTableExt.oApi.fnResetAllFilters = function (oSettings, bDraw/default true/) {
    for(iCol = 0; iCol < oSettings.aoPreSearchCols.length; iCol++) {
    oSettings.aoPreSearchCols[ iCol ].sSearch = '';
    }
    $('.filter-term').remove();
    oSettings.oPreviousSearch.sSearch = '';
    if(typeof bDraw === 'undefined') bDraw = true;
    if(bDraw) this.fnDraw();
    }

});
});

But it doesn't work, all i get on button click is my page get's refreshed. What i am doing wrong here???

UPDATED

$(document).ready(function(){
$("button").click(function(e){e.preventDefault();})
$("button").click(function(){
console.log("afterbutton");
 $.fn.dataTableExt.oApi.fnResetAllFilters = function (oSettings, bDraw) {
console.log("insidefunction");
    for(iCol = 0; iCol < oSettings.aoPreSearchCols.length; iCol++) {
    oSettings.aoPreSearchCols[ iCol ].sSearch = '';
    }
    $('.filter-term').remove();
    oSettings.oPreviousSearch.sSearch = '';
    if(typeof bDraw === 'undefined') bDraw = true;
    if(bDraw) this.fnDraw();
    }

});
});

Now page is not refreshing, also code is not woking, The console only shows till afterbutton message i click on button.

Is there anything wrong with this code?

Thank you so much for the reply, as per your suggestion i've updated my code(I took button click event outside the $(document).ready(function())

$(document).ready(function(){
    $.fn.dataTableExt.oApi.fnResetAllFilters = function (oSettings, bDraw) {
        for(iCol = 0; iCol < oSettings.aoPreSearchCols.length; iCol++) {
        oSettings.aoPreSearchCols[ iCol ].sSearch = '';
        }
        $('.filter-term').remove();
        oSettings.oPreviousSearch.sSearch = '';
        if(typeof bDraw === 'undefined') bDraw = true;
        if(bDraw) this.fnDraw();
        }

    } );

    // button click event
    $("button").click(function(e){
            e.preventDefault();
            // 'myDataTable' is the name you gave the datatable at initialisation - oTable or similar
            table.fnResetAllFilters();
      });

This still refreshes my page on button click, But if i take button click event inside $(document).ready(function() then i get error as table.fnResetAllFilters(); is not a function. table = $('#example').DataTable({ this is how i initialize the Datatable.


回答1:


You need to add the preventDefault() to your original button click listener, you've actually added another one.

Modify your code so it looks like this:

$(document).ready(function(){
   $("button").click(function(e){
      e.preventDefault();
      console.log("afterbutton");
      ...

It also looks like you've included the function definition inside your button click code.

It needs to look something more like this:

$(document).ready(function(){
// function definition
 $.fn.dataTableExt.oApi.fnResetAllFilters = function (oSettings, bDraw/default true/) {
    for(iCol = 0; iCol < oSettings.aoPreSearchCols.length; iCol++) {
    oSettings.aoPreSearchCols[ iCol ].sSearch = '';
    }
    $('.filter-term').remove();
    oSettings.oPreviousSearch.sSearch = '';
    if(typeof bDraw === 'undefined') bDraw = true;
    if(bDraw) this.fnDraw();
    }
});

// button click event
$("button").click(function(e){
        e.preventDefault();
        // 'myDataTable' is the name you gave the datatable at initialisation - oTable or similar
        myDataTable.fnResetAllFilters();
  });
});


来源:https://stackoverflow.com/questions/24034076/jquery-datatables-and-columnfilterwidget-reset-all-filters-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!