Why is my search returning only one result?

Why is my search returning only one result?

iamjonoiamjono Posts: 3Questions: 1Answers: 0

I've written the following, meant to take from my customers search field a string such as "CRG, SAV, DAB, FXE" and return all rows with the FAA airport ID's.

    $.fn.dataTable.ext.search.push(
        function(settings, searchData, index, rowData, counter) {
            var airportid = searchData[0];
            var a = airportid.toUpperCase();
            
            var terms = $('#searchme').val().toUpperCase();
            // Return all rows if search is blank
            if(terms === '') return true;
            var arr = terms.split(',');
            var filtered = arr.filter(e =>  e.trim());

            if(filtered.includes(a)) return true;
            return false;
        }
    );

Triggered by:

    $('body').on('click', '.executeSearch', function(){
        table.draw();
    });

How come, however, it's only getting the first, in the case of the test string above, CRG?

Thanks!

This question has an accepted answers - jump to answer

Answers

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

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. 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

  • iamjonoiamjono Posts: 3Questions: 1Answers: 0

    Thanks Colin, yeah sorry I thought it might be easy to spot.
    I worked up an example, extracted the info from the system and simplified everything. The issue presents itself still...
    live.datatables.net/jexukepi/1/edit?html,css,js,output

    Thanks!
    Jono

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

    It's because the trim isn't working, the second value has a " XXX" - i.e. a leading space, you can confirm that by searching for "ACV,ACY" without a space. It might be better to refactor the code so that it doesn't need a comma, as that might confuse the users, and just search on "ACV ACY", and you can split on the space then,

    Colin

  • iamjonoiamjono Posts: 3Questions: 1Answers: 0

    Ah, that's exactly what it is!
    Wasn't seeing the wood for the trees, thanks :)

Sign In or Register to comment.