在codeigniter中使用数据表如何显示所需列的下拉过滤器

时间:2017-02-02 05:19:06

标签: php codeigniter datatables

我想使用像city

这样的必填栏上的数据表下拉列表来过滤列表
<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>  

datatables 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"
            }
        } );

我如何实现显示drop downfilter。

2 个答案:

答案 0 :(得分:2)

  

我认为你阅读本教程可能是你可以得到它

Click Here For Multi Dropdown Filter

答案 1 :(得分:1)

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

$(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