简体   繁体   English

jQuery DataTables和Columnfilterwidget重置所有过滤器按钮

[英]jQuery DataTables and Columnfilterwidget Reset all filters button

I am new to Javascript.So my question is a little silly. 我是Java语言的新手,所以我的问题有点傻。

I was looking for Reset all filter button for Columnfilterwidget and found this code. 我一直在寻找Columnfilterwidget的Reset all filter按钮,并找到了此代码。

$.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() )进行了按钮单击事件。

$(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. 这仍然会刷新按钮单击时的页面,但是如果我在$(document).ready(function()内接受按钮单击事件,那么我会得到table.fnResetAllFilters();错误,不是函数table.fnResetAllFilters(); table = $('#example').DataTable({这就是我初始化数据表的方式。

You need to add the preventDefault() to your original button click listener, you've actually added another one. 您需要将preventDefault()添加到原始的按钮单击侦听器,实际上您已经添加了另一个。

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();
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM