Individual column searching in header

Individual column searching in header

Yves HEBERTYves HEBERT Posts: 10Questions: 4Answers: 0

I'm using this code for have individual column searching in header :

$(document).ready( function () {
    // Setup - add a text input to each header cell with header name
    $('#example thead th').each( function () {
        var title = $(this).text();
        $(this).html( title+'<br><input type="text" placeholder="Search '+title+'" />' );
    } );
    // DataTable
    var table = $('#example').DataTable();
    // Apply the search
    table.columns().every( function () {
        var that = this;
        $( 'input', this.footer() ).on( 'keyup change clear', function () {
            if ( that.search() !== this.value ) {
                that
                    .search( this.value )
                    .draw();
            }
        } );
    } );
} );

But when I click on a search field, the order of the column changes.
How can I disable this event when I click on <input type = "text" /> and keep the sort event if I click the name of the column (which is in the same cell)?

Thanks for your help.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,144Questions: 1Answers: 2,586
    Answer ✓

    Hi @Yves HEBERT ,

    This thread should help, it's asking the same thing.

    Cheers,

    Colin

  • Yves HEBERTYves HEBERT Posts: 10Questions: 4Answers: 0

    Thank you for your quick reply

  • shaleenyadavshaleenyadav Posts: 2Questions: 0Answers: 0
    edited August 2019

    @colin How to limit the text input to selected columns?
    $('#OpptyTable tfoot th').each( function () {
    var title = $(this).text();
    $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
    } );

  • kthorngrenkthorngren Posts: 20,308Questions: 26Answers: 4,769

    You can assign a class to each column to search. Here is an example:
    http://live.datatables.net/vuruhosa/1/edit

    Kevin

  • shaleenyadavshaleenyadav Posts: 2Questions: 0Answers: 0

    @kthorngren Awesome!! That is exactly I was looking for. I am just new to HTML/JScript so I am having some trouble figuring this out.
    Do you also know how to mix and match input text and select lists?
    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>' )
                } );
    
  • kthorngrenkthorngren Posts: 20,308Questions: 26Answers: 4,769

    You would use one class for the text inputs to loop through and another for the select lists. Don't try to combine the loops just use modify the two to use different classes.

    Kevin

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    This example here has both select lists and input elements to filter - you just need to combine the two examples.

    Cheers,

    Colin

This discussion has been closed.