【问题标题】:jQuery Datatables: Individual column searching not workingjQuery Datatables:单个列搜索不起作用
【发布时间】:2016-09-04 22:36:36
【问题描述】:

我已包含此链接中的代码:https://datatables.net/examples/api/multi_filter.html

但它不能正常工作。显示搜索框,但在搜索框中键入详细信息时,数据不会加载。我将发布我包含在我的文件中的代码。请查看并验证。

任何帮助将不胜感激。谢谢。

代码

        <div class="col-md-12" style="max-height:300px; display:block; overflow:auto;" >
    <table id="big_table" class="table table-striped display table-bordered">
        <thead>
    <tr>
    <th>Column 1</th>
    <th>Column 2</th>
    <th>Column 3</th>
    <th>Column 4</th>
    <th>Column 5</th>
    <th>Column 6</th>
    <th>Column 7</th>
    <th>Column 8</th>
    <th>Column 9</th>
    <th>Column 10</th>
    </tr>
        </thead>
        <tfoot>
    <tr>
    <th>Column 1</th>
    <th>Column 2</th>
    <th>Column 3</th>
    <th>Column 4</th>
    <th>Column 5</th>
    <th>Column 6</th>
    <th>Column 7</th>
    <th>Column 8</th>
    <th>Column 9</th>
    <th>Column 10</th>
    </tr>
        </tfoot>        
<tbody>
    <?php foreach($array as  $arr) { ?>

    <tr>
    <td><?php echo $arr->column_1; ?></td>
    <td><?php echo $arr->column_2; ?></td>
    <td><?php echo $arr->column_3; ?></td>
    <td><?php echo $arr->column_4; ?></td>
    <td><?php echo $arr->column_5; ?></td>
    <td><?php echo $arr->column_6; ?></td>
    <td style="text-align:right;"><?php echo $arr->column_7; ?></td>
    <td style="text-align:right;"><?php echo $arr->column_8; ?></td>        
    <td><?php echo $arr->column_9; ?></td>
    <td><?php echo $arr->column_10; ?></td>
    </tr>       
    <?php } ?>
    </tbody>

JAVASCRIPT

<script>
$(document).ready(function() {
// including input
$('#big_table tfoot th').each( function () {
    var title = $(this).text();
    $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );
// datatable initialization plus exporting to excel     
var table = $('#big_table').DataTable( {
    dom: 'Bfrtip',
    buttons: [
        'excelHtml5'
    ],
    "bFilter": false,
    "bInfo": false,
} );
//search
table.columns().every( function () {
    var that = this;

    $( 'input', this.footer() ).on( 'keyup change', function () {
        if ( that.search() !== this.value ) {
            that
                .search( this.value )
                .draw();
        }
    } );
} );       

} );
</script>   

【问题讨论】:

  • 您在控制台中得到了什么?你也有身体吗?在您发布的 html 代码中它丢失并且代码不完整。
  • Uncaught ReferenceError: table is not defined
  • 好的,你也想要 tbody 的代码吗?
  • 你用的是哪个版本的datatable和jquery?
  • 版本号:1.10.11

标签: javascript jquery datatable


【解决方案1】:

数据表初始化部分中的bFilter 属性似乎使数据表的冲突不可搜索。根据数据表站点,如果您想单独搜索多个列,则应将此属性设置为 true。尝试以下代码进行数据表初始化,

var table = $('#big_table').DataTable( {
    dom: 'Bfrtip',
    buttons: [
        'excelHtml5'
    ],
    "bInfo": false,
} );

这应该对你有用。检查这个JSFIDDLE

如果要禁用(隐藏)数据表全局搜索过滤器,则应将 dom 设置为 lrtp。例如:dom: 'lrtp'

【讨论】:

  • @davidnoronha 如果您仍想禁用全局搜索,您可以使用dom:lrtp 属性。很高兴它的工作。 ;)
  • 我们在项目中尝试了完全相同的方法,但仍然无法正常工作;你能指点一下吗?我的队友已在此线程中发布了完整代码 -> stackoverflow.com/questions/54127446/…
猜你喜欢
  • 1970-01-01
  • 2016-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
相关资源
最近更新 更多