Jquery 数据表初始化发生了两次

Jquery 数据表初始化发生了两次,jquery,twitter-bootstrap,datatable,jquery-datatables,Jquery,Twitter Bootstrap,Datatable,Jquery Datatables,我有一个数据表,其响应如下所示: <table class="table table-hover responsive datatable" data-scroll=".oc- scroll" id="example"> <thead> <tr> <th>col1</th> <th>col2</th> <th>col3</th>

我有一个数据表,其响应如下所示:

<table class="table table-hover responsive datatable" data-scroll=".oc-  scroll" id="example">
  <thead>
     <tr>
        <th>col1</th>
        <th>col2</th>
        <th>col3</th>
        <th>col4</th>
        <th>col5</th>
        <th>Actions</th>                           
     </tr>
  </thead>
  <tfoot>
    <tr>
        <th>col1</th>
        <th>col2</th>
        <th>col3</th>
        <th>col4</th>
        <th>col5</th>
        <th></th>                           
    </tr>
  </tfoot>   
  <tbody>
    <tr>
        <th>col1</th>
        <th>col2</th>
        <th>col3</th>
        <th>col4</th>
        <th>col5</th>
        <th>Edit</th>                           
    </tr>
    <tr>
        <th>col1</th>
        <th>col2</th>
        <th>col3</th>
        <th>col4</th>
        <th>col5</th>
        <th>Edit</th>                           
    </tr>
    <tr>
        <th>col1</th>
        <th>col2</th>
        <th>col3</th>
        <th>col4</th>
        <th>col5</th>
        <th>Edit</th>                           
    </tr>
    <tr>
        <th>col1</th>
        <th>col2</th>
        <th>col3</th>
        <th>col4</th>
        <th>col5</th>
        <th>Edit</th>                           
    </tr>
   </tbody>
 </table>

可乐
可乐
可乐
可乐
可乐
行动
可乐
可乐
可乐
可乐
可乐
可乐
可乐
可乐
可乐
可乐
编辑
可乐
可乐
可乐
可乐
可乐
编辑
可乐
可乐
可乐
可乐
可乐
编辑
可乐
可乐
可乐
可乐
可乐
编辑
我已经使用下面的js对datatable应用了一个列过滤器

var tableID = "example";
var colIdxIgnore = "6";

if(tableID != '') {

    // Setup - add a text input to each footer cell
    $('#' + tableID + ' tfoot th').each( function () {
        var title = $('#' + tableID + ' thead th').eq( $(this).index() ).text();
        if ($(this).index() != colIdxIgnore) {
            $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
        }
    } );

    // DataTable
    var table = $('#' + tableID).DataTable();
    // Apply the search
    if(table.columns().eq(0) != null){
        table.columns().eq( 0 ).each( function ( colIdx ) {         
            $( 'input', table.column( colIdx ).footer() ).on( 'keyup change', function () {
                table
                    .column( colIdx )
                    .search( this.value )
                    .draw();
            } );
        } );
    }
  table.destroy();

}
var tableID=“示例”;
var colIdxIgnore=“6”;
如果(表ID!=''){
//设置-向每个页脚单元格添加文本输入
$('#'+tableID+'tfoot th')。每个(函数(){
var title=$('#'+tableID+'thead th').eq($(this.index()).text();
if($(this).index()!=colIdxIgnore){
$(this.html(“”);
}
} );
//数据表
var table=$('#'+tableID).DataTable();
//应用搜索
if(table.columns().eq(0)!=null){
table.columns().eq(0).each(函数(colIdx){
$('input',table.column(colIdx).footer()).on('keyup change',function(){
桌子
.列(colIdx)
.search(this.value)
.draw();
} );
} );
}
表1.destroy();
}
还有另一个js文件,它也会再次初始化datatable。下面是指向该js文件的链接-[js文件]

现在,如果我有一个超过12-15列的datatable,并在浏览器中打开datatable页面,一些列将被隐藏,这是预期的。如果我点击一行,隐藏的列就会显示出来。但是由于datatable的双重初始化,隐藏列显示两次,如下面的屏幕截图所示


提前感谢。

为什么要两次初始化表?默认的application.js文件初始化一次。然后在columnfilterjs文件中,我正在使用/重新创建Datatable(var table=$('#'+tableID).Datatable();),这就是它初始化两次的原因。在不同调用上初始化同一Datatable不是一个好主意,它会影响性能。抱歉,请使用已初始化的datatable对象进行搜索。我对此一无所知。你能帮我吗?第一次初始化由列过滤器完成,然后在application.js file.dude中,您的application.js文件被最小化