How to search for an exact string in a jQuery Data

2020-07-26 11:33发布

问题:

how can i filter satisfactory only.

ive tried this script but its not working

<script>
$(document).ready(function() {
    tbl = $('#example').dataTable();
    tbl.fnFilter("^" + filter_value + "$");
});

$(document).ready( function() {
    $('#example').dataTable( {
        "oSearch": {"bSmart": false}
    } );
} )

oTable.fnFilter( "^"+TERM+"$", COLUMN , true); //Term, Column #, RegExp Filter
oSettings.aoPreSearchCols[ iCol ].sSearch = "^\\s*"+'1'+"\\s*$";
oSettings.aoPreSearchCols[ iCol ].bRegex = false;
oSettings.aoPreSearchCols[ iCol ].bSmart= false;
</script>

回答1:

SOLUTION

Use the code below for DataTables 1.10+ to perform exact match for all columns in the table:

var table = $('#example').DataTable();

$('.dataTables_filter input', table.table().container())
    .off('.DT')
    .on('keyup.DT cut.DT paste.DT input.DT search.DT', function (e) {

        // Uncomment this loop for large datasets for performance
        // to search only on ENTER key
        // if (e.keyCode == 13) {

            var term = $.trim(this.value).toLowerCase();
            if (term !== "") {
                $.fn.dataTable.ext.search.push(
                    function (settings, data, dataIndex) {
                        var isFound = false;
                        $.each(data, function (index, value) {
                            if (value.toLowerCase() === term.toLowerCase()) {
                                isFound = true;
                            }
                            return !isFound;
                        });

                        return isFound;
                    }
                );
            }

            table.draw();

            if (term !== "") {
                $.fn.dataTable.ext.search.pop();
            }

        // Uncomment this loop for large datasets for performance
        // to search only on ENTER key
        // }
});

DEMO

See this jsFiddle for code and demonstration.