Using .search(this.value, false, false) is matching multiple

Using .search(this.value, false, false) is matching multiple

KeiSenpaiKeiSenpai Posts: 21Questions: 8Answers: 0

I am reading a JSON file into my DataTables. I have a dropdown filter that filters column 1, but it is not doing an exact match and pulling say '123' & '1234' when I only want '123' back.

    $('#commodityFilter').on('change', function () {
        if (this.value === 'ABC & ABCD') {
            tableAll.columns(1).search("ABC|ABCD", true, false).draw();
        } else {
            tableAll.columns(1).search(this.value, false, false).draw();
        }
    } ); 

I ensured that smart & regex is off. Am I missing something? I even tried providing regex for exact match.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    Answer ✓

    If you want to search for 123 specifically and ignore rows with 1234 then you will need a regex search. There are various regex operators you can use. One option is to perform a regex search like this:

    tableAll.columns(1).search('^' + this.value +'$', true, false).draw();
    

    If 123 is entered the search would look like this:

    tableAll.columns(1).search('^123$', true, false).draw();
    

    You can experiment with this example.

    Kevin

Sign In or Register to comment.