Dynamic Selects - Adjusting Search Parameter to filter for ALL fields (or specific ones).

Dynamic Selects - Adjusting Search Parameter to filter for ALL fields (or specific ones).

aehrenwoaehrenwo Posts: 3Questions: 2Answers: 0
edited June 2018 in Free community support

I am trying to figure out how to amend this code so that I can have the dropdowns filter against "all fields" and not just the one that they were originally bound to based on the data on that column.

For example, I have a column called Sector (7 in the code) and I would like if someone selects that particular dropdown to have it search if ANY fields have that string.

Here is the code I used to create the original selects on the columns.

initComplete: function () {
            this.api().columns([4, 6, 7, 8, 9]).every( function () {
                var column = this;
                var select = $('<select><option value=""></option></select>')
                    .appendTo( $(column.header()) )
                    .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>' );
                } );
            } );
        }

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @aehrenwo ,

    If you want to search against all fields, then you just need to change that line 10, where it's doing a column search with column().search(), you would just do a table search instead, search(), so you would change

    column
    

    to be

    this.api()
    

    Cheers,

    Colin

  • aehrenwoaehrenwo Posts: 3Questions: 2Answers: 0

    This seems to make ALL of the dropdowns stop working. I want it to try to find a match in any of the fields? Any suggestions?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @aehrenwo ,

    We're happy to take a look, but it would help, as per the forum rules, if you could link to a running test case showing the issue so we can offer some help. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

This discussion has been closed.