Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autosuggestion is not working using canjs

I am trying to to create auto suggest element manually. I am using canjs for this prupose.

Following code I have tried so far:

list.filter( function( item, index, list ) {
   if(item.includes(searchText) && searchText != ''){
     //css hide and show classes for match
   }
   else{
      // css show for unmatched results
   }
})

In above code I am facing two problems:

  1. includes does not work in all browsers. For that I have tried match,
    contains and sub-string but they could not help me.

  2. includes working in chrome but when I entered the string whose substring is not contained by the last element of list it will not
    work because filter will keep searching from all the elements.

Is there any mistake I am doing?

I want to it to run in all the browser.

Thank you.

like image 319
Dipesh Raichana Avatar asked Nov 09 '22 12:11

Dipesh Raichana


1 Answers

String.prototype.includes() with one argument is equivalent to applying !!~ operators to String.prototype.indexOf(), and the latter one works in all browsers. So your test line can be this:

if(!!~item.indexOf(searchText) && searchText !== '’){

like image 107
air_hadoken Avatar answered Nov 14 '22 22:11

air_hadoken