Is it possible to default the value of a column filter?

Is it possible to default the value of a column filter?

TomBajzekTomBajzek Posts: 164Questions: 37Answers: 1

I have a Datatable with a column filter that uses a select element for the filter. I'd like to set a default filter, for this column so that it selects the overwhelmingly desired value for a particular column. I think I can figure out how to program a default for this filter parameter, but I don't want to do that if it's not necessary. I thought I should ask if there is a way to set a default value for a filter within the API that I might have missed. My filter code is basically what is in the example page, except that I've added a class selector (.cFilter) to control which columns have filters:

            this.api().columns('.cFilter').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
                        .cache( 'search' )
                        .sort()
                        .unique()
                        .each( function ( d ) {
                            if (d.length > 0) {
                                select.append( $('<option value="'+d+'">'+d.substr(0,9)+'</option>') );
                            }
                } );
            } );

Is there a built-in way to do this, or should I just write some more code? I just don't want to write new code if this is a feature that I've missed.

Thanks,
Tom

This question has an accepted answers - jump to answer

Answers

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

    Hi @TomBajzek ,

    Yep, you can see up an initial column-based filter with searchCols. See example here.

    Cheers,

    Colin

  • TomBajzekTomBajzek Posts: 164Questions: 37Answers: 1

    Colin,

    I think your example might be with an earlier version of DataTables, but I think I adapted it correctly, as it now seems to work as I wanted. This was simpler and clearer that code I envisioned needing to write otherwise.

    Many thanks for your suggestion,
    Tom

  • jmbishopjmbishop Posts: 4Questions: 2Answers: 0

    As a follow on to this question, can you exclude a search term also?
    The issue I have is that I want to initially return on the term, "PL Tulsa" But I also have a value in the db for "Non PL Tulsa" that is being returned as well because it meets my search criteria. I would like to initially only display the "PL Tulsa" and exclude "Non PL Tulsa."

    Thanks,
    Jerrod

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    You would need to use a regex search with a search string like this ^PL Tulsa.

    Kevin

This discussion has been closed.