How to add drop-down filter on jQuery datatables plugin on single column?

I want to add a drop-down filter on dataTables, in only one column. I added the code below, but it adds drop-down lists in all columns :

$('#dataTables-example').DataTable({
    responsive: true,
    "pageLength": 100,
    "lengthMenu": [100, 250, 500, "All"],
    initComplete: function () {
        this.api().columns().every( function () {
        var column = this;
        $('#dataTables-example .head .head_hide').html('');

        var select = $('<select id="formfilter" class="filterdropdown"><option value="">'+$(column.header()).text()+'</option></select>')
            .appendTo( $(column.header()).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>' )
        });
    }); 
});
+4
source share
1 answer

Decision

Edit

this.api().columns().every( function () {

to

this.api().columns(1).every( function () {

where 1is the index of the column with a zero value.

NOTES

  • To add a filter to specific columns, use the format instead columns([1,3]).
  • , , , . .
+6

Source: https://habr.com/ru/post/1654769/