【问题标题】:in codeigniter using datatables how to show dropdown filter for required column在使用数据表的codeigniter中如何显示所需列的下拉过滤器
【发布时间】:2017-02-02 05:19:06
【问题描述】:

我想使用所需列(如城市)上的数据表下拉列表来过滤列表

<table class="table table-striped table-bordered bootstrap-datatable datatable">
    <thead>
        <tr>
            <th>Sl.no</th>
            <th>Principle</th>
            <th>City</th>
            <th>Branch Name</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php $i=1; foreach($branchDetails as $branch) {?>
        <tr>
            <td><?php echo $i;?></td>
            <td class="center"><?php echo $branch->principle_name;?></td>
            <td class="center"><?php echo $branch->city_name;?></td>
            <td class="center"><?php echo $branch->branchName;?></td>
            <td class="center">
                <a href="<?php echo base_url();?>admin/Branch/edit/<?php echo $branch->branchId ; ?>">
                    <i class="icon-edit"></i>  
                </a>
                <!--<a href="#" data-id="<?php echo $branch->branchId;?>">
                    <i class="icon-trash"></i> 
                </a>-->
            </td>
        </tr>
        <?php $i++; } ?>
    </tbody>
</table>  

数据表js是

$('.datatable').dataTable({
            "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span12'i><'span12 center'p>>",
            "sPaginationType": "bootstrap",
            "oLanguage": {
            "sLengthMenu": "_MENU_ records per page"
            }
        } );

我如何实现在此显示下拉过滤器。

【问题讨论】:

标签: php codeigniter datatables


【解决方案1】:

我想你读了这个教程也许你能明白

Click Here For Multi Dropdown Filter

【讨论】:

    【解决方案2】:

    如果你想只有选择的列下拉过滤器。可以实现

    $(document).ready(function() {
    $('#example').DataTable( {
        initComplete: function () {
            this.api().columns().every( function () {
                var column = this;
                if(column[0]==1){ /* is the second column you want to have dropdown filter */
                    var select = $('<select><option value=""></option></select>')
                    .appendTo( $(column.footer()).empty() )
                    .on( 'change', function () {
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val()
                        );
    
                        column
                            .search( val ? '^'+val+'$' : '', true, false )
                            .draw();
                    } );
    
                    column.data().unique().sort().each( function ( d, j ) {
                        select.append( '<option value="'+d+'">'+d+'</option>' )
                    } );
                }
            } );
        }
    } );
    } );
    

    Working example in plunker

    【讨论】:

    • 我使用了这个,但我得到了错误:DataTables 警告(表 id = 'DataTables_Table_0'):无法重新初始化 DataTable。要检索此表的 DataTables 对象,请不传递任何参数或查看 bRetrieve 和 bDestroy 的文档
    • 这已经在页脚中初始化,但我只想要一个列表。
    • @vijay kumar 如果您只想要一个列表,那就是您的情况下的城市。所以你应该使用if(column[0]==2){ 来只下拉城市。你应该看到 plunker 链接它是如何完成的
    猜你喜欢
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 2019-08-28
    • 2018-04-23
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多