0

I need to show the list that are retrieved from a thirdparty system (callout) using datalist options. I tried the logic in the this link https://blog.enree.co/2019/04/salesforce-lwc-autocomplete-magic-with-html-datalist.html. However, my dynamic array list is never displayed though I see the responses from Apex. I see the input and datalist are linked together using their ids.

html:

    <input id="addressInputbox"
            data-id="address-search"
            type="text"
            oninput={onChange}
            list="countries"
            class="slds-input"/>
            <datalist id="countries" >
                <template for:each={showaddressList} for:item='item'>
                    <option key={item.Rank} value={item.Address}>{item.Address}</option>
                </template>
            </datalist>

*.js:

               renderedCallback() {
    if (this.initialized) {
        return;
    }
    this.initialized = true;
   this.template.querySelector("input").setAttribute('list', listId);
    let listattr1 = this.template.querySelector('input').getAttribute('list');
    }



   onChange(event){
    this.value = event.target.value;
    this.showaddressList = '';
    if(this.value && this.value.length >= 4){
        console.log(this.value);
        fetchAddressList({addressText : this.value.trim()})
        .then(result => {
            //console.log('result-->'+result[0].address);
            if(result){
                result.forEach(element => {
                    let temp = {};
                    temp.Address = element.address;
                    temp.Rank = element.rank;
                    temp.Id = element.id;
                    console.log('element-->'+element.address+' '+element.id);
                    this.addressList[parseInt(element.rank)] = temp;
                    console.log('addressList-->'+this.addressList);
                });this.showaddressList = this.addressList;console.log('showaddressList-->'+this.showaddressList);
            }
            
        });

1 Answer 1

3

LWC tends to modify the ID attribute of elements to make sure they're globally unique. So, you need to arrange for the link to happen after the elements are rendered. Here's an example that works.

import { LightningElement } from "lwc";

export default class App extends LightningElement {

  connectedCallback() {
    setTimeout(
      ()=>this.template.querySelector('input')
      .setAttribute('list', this.template.querySelector('datalist').id));
  }
}

<template>
    <input />
    <datalist id="countries">
        <option>Italy</option>
        <option>Spain</option>
        <option>France</option>
        <option>USA</option>
        <option>England</option>
        <option>Belgium</option>
        <option>Brazil</option>
        <option>Mauritius</option>
        <option>Colombia</option>
        <option>Portugal</option>
        <option>Russia</option>
        <option>Mauritania</option>
    </datalist>
</template>
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.