Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Neeraj1005's avatar

How to get dataTabel header name under select dropdown filter?

In my project I'm using the datatable filter https://datatables.net/examples/api/multi_filter_select.html where I've created a filter option for specific column. But the problem with this I want to show the table header name instead of All how can I get this? can anyone helps me out of this problem. Here I've put the script code plz check it and also check this image what I actually wants? https://imgur.com/U759VBL

<script>
    $(document).ready(function() {
    $('#exampletable').DataTable( {
        initComplete: function () {
            this.api().columns([3,4,5,7]).every( function () {//THis is used for specific column
                var column = this;
                var select = $('<select class="mx-1"><option value="">All</option></select>')
                    // .appendTo( $(column.footer()).empty() )
                    .appendTo( '#filltertable' )
                    .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 ) {
                    var val = $('<div/>').html(d).text();
                    select.append( '<option value="'+val+'">'+val+'</option>' )
                } );
            } );
        }
    } );
} );
</script>
0 likes
1 reply
Neeraj1005's avatar
Neeraj1005
OP
Best Answer
Level 9

Now I've solved this problem https://stackoverflow.com/a/61927292/8455396

<script>
    $(document).ready(function() {
    $('#exampletable').DataTable( {
        "ordering": false,
        initComplete: function () {
            this.api().columns([2,3,4,8]).every( function (d) {//THis is used for specific column
                var column = this;
                var theadname = $('#exampletable th').eq([d]).text();
                var select = $('<select class="mx-1"><option value="'+d+'">'+theadname+': All</option></select>')
                    .appendTo( '#filtertable' )
                    .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 ) {
                    var val = $('<div/>').html(d).text();
                    select.append( '<option value="'+val+'">'+val+'</option>' )
                } );
            } );
        }
    } );
} );
</script>


Please or to participate in this conversation.