2

enter image description here

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>
Alex Walker
  • 2,337
  • 1
  • 17
  • 32

1 Answers1

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.

Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185
  • @RolanAlgara, please note that my code is for DataTables 1.10 – Gyrocode.com Oct 15 '15 at 03:06
  • sir one more question, will this code work if the table is populated from database? – Rolan Algara Oct 15 '15 at 03:10
  • @RolanAlgara, code will only work for client-side processing mode only (`serverSide: false`) using either HTML or Ajax or JavaScript data source. For server-side processing mode (`serverSide: true`) you would need to implement the filtering on the server side. – Gyrocode.com Oct 15 '15 at 03:30
  • Yes! Made almost the same demo yesterday (you beat me in time :) - but I think `if (e.keyCode == 13) {` is better (so you can type the search term and then hit enter) and I just `lowercase`'d the searchTerm and `data[x]`, I believe regex is a little bit slower (and not nessecary) – davidkonrad Oct 15 '15 at 07:42
  • @RolanAlgara - you should mark the answer as accepted - you will earn 2 reps and a new shiny [**Scholar**](http://stackoverflow.com/help/badges/10/scholar)-badge :) – davidkonrad Oct 15 '15 at 07:45
  • @davidkonrad, you're could be right about `toLowerCase()`, it may be faster. Regarding `if (e.keyCode == 13)` it's could be a great option for larger datasets. I have corrected my code per your suggestions. – Gyrocode.com Oct 15 '15 at 10:36
  • @RolanAlgara, I have corrected my code to improve performance. – Gyrocode.com Oct 15 '15 at 10:37