0

I have a LWC that displays anywhere from 1 to n lightning-combobox components. Since the number of combo boxes needed is unknown, the combo boxes are within a for:each directive:

    <template for:each={refFields} for:item="fld">
       <lightning-combobox key={fld.id} name={fld.inpt} label={fld.label} value={fld.inpt} 
          placeholder={fld.ph} options={fld.options} onchange={updateInputs} 
          variant="label-inline"></lightning-combobox>
   </template>

Everything is working fine except that when I set fld.inpt to a previously saved value it does not display in the combobox as the selected value even though the value of fld.inpt matches one of the values in fld.options. Instead the combobox looks like nothing has been selected yet.

I believe the issue is due to the fact that the combobox is within the for:each because I have other comboboxes on the same LWC and setting those previously selected values does display correctly.

Has anyone done this before and come across the same issue?

Thank you

3
  • Did you remember to use @track on the refFields?
    – sfdcfox
    Commented Apr 28, 2023 at 23:38
  • very difficult to suggest your mistakes unless you put sample code.....i think you might have to modify the refFIelds in local variable and initialize the local variable value to refFields at the end of the method. I do not think other combobox values will interfere any other combobox functionality. Everything is on its own.
    – Pratap M
    Commented Apr 29, 2023 at 3:01
  • @sfdcfox Yes, the refFields var is marked with "@track". I posted this issue on Trailblazer community too and someone made a suggestion there that looks promising. I'll try it today and if it fixes the issue I will post the solution back here too.
    – mtbIt
    Commented May 1, 2023 at 19:47

1 Answer 1

0

I ended up figuring this out. Basically it was a timing issue. The selectable options in the combo box are the lookup relationship fields from an sObject that the user selects. At the same time, any previously saved values are loaded into the fields. I expected the combo box options would be loaded by the time the saved values are loaded but it turned out not to be the case. So I had to move some things around to ensure that things were loaded in the correct order.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .