0

I want to filter the list using datatables dropdown list on required column like 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 is

$('.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"
            }
        } );

how can i implement to show drop downfilter on this .

1

2 Answers 2

2

I think you read this tutorial might be you can get it

Click Here For Multi Dropdown Filter

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to have only selected columns dropdown filter.It can be achieved

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

3 Comments

I used this but i got error:DataTables warning (table id = 'DataTables_Table_0'): Cannot reinitialise DataTable. To retrieve the DataTables object for this table, pass no arguments or see the docs for bRetrieve and bDestroy
This was already initialized in footer but i want in only one list.
@vijay kumar if you want only one list that is city in your case . so you should use if(column[0]==2){ to have city drop down only. you should see plunker link how it is done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.