Own suggestion list

Hi,

is there any possibility to open suggestion list at focus in empty auto complete component? So similar as in Google, when you start to searching, it shows you your previous searches:
https://puu.sh/BINYN/897638ecfa.png

I know I can call showPopup, but I do not know how how to set my own suggestion list.

Regards,
Marko

3 Replies

PO Prince Oliver Syncfusion Team October 12, 2018 07:40 AM UTC

Hi Marko, 

Thank you for contacting Syncfusion support. 

We have achieved your requirement for displaying suggestion list upon focusing an empty auto complete component, using the focus event in the control. We have used the filtering and change events to get the typed and selected words and stored them in the browser’s local storage. Then using the focus event, we have displayed the stored list as suggestions. Kindly refer to the following code snippet. 

let atcObj2: AutoComplete = new AutoComplete({ 
  dataSource: countries, 
  fields: { value: 'Name' }, 
  placeholder: 'e.g. Australia', 
  sortOrder: 'Ascending',  
  filterType: 'StartsWith',   
  change: ()=>{ 
  localStorage.setItem("value", atcObj2.value); 
    if(localStorage.getItem('value') !== 'null'){suggestList.push(localStorage.getItem('value')); 
      suggestList = suggestList.filter(function(item, pos, self) { 
        return suggestList.indexOf(item) == pos; 
      }); 
    } 
  }, 
  focus: ()=>{  
    if(suggestList.length >0){ 
    (atcObj2.dataSource as any) = suggestList; 
    atcObj2.dataBind(); 
    let keyEventArgs: any = { preventDefault: (): void => {            }, action: 'down', keyCode: 40, type: null }; 
    (atcObj2 as any).onFilterUp(keyEventArgs); 
    (atcObj2 as any).popupObj.element.classList.add('e-suggestion'); 
    } 
  }, 
  filtering: (e: FilteringEventArgs) => { 
      let query: Query = new Query(); 
      query = (e.text !== '') ? query.where('Name', 'startswith', e.text, true) : query; 
      e.updateData(countries, query); 
      (atcObj2 as any).popupObj.element.classList.remove('e-suggestion'); 
  } 
}); 
atcObj2.appendTo('#country'); 

We have attached the sample for your reference, please find the sample at the following location: https://stackblitz.com/edit/mv2fks-qs3ywn 

Please let us know if you require any further assistance on this. 

Regards, 
Prince 



MA Marko October 17, 2018 11:47 AM UTC

Hi,

thank you for this sample code. It will be in great help.

Regards,
Marko


PO Prince Oliver Syncfusion Team October 17, 2018 11:52 AM UTC

Hi Marko, 

Most Welcome. We are glad to help you. 

Regards, 
Prince 


Loader.
Up arrow icon