0

I am trying hands on in lwc . Got stuck while I want to do something which in aura was like:

<aura:iteration items="{!v.serverResult}" var="item" indexVar="i"> 
  <li role="presentation" class="slds-listbox__item" data-selectedIndex="{#i}" onclick="{!c.itemSelected}">
    <span id="{#'listbox-option-unique-id-'+i+1}" class="slds-media slds-listbox__option slds-listbox__option_entity slds-listbox__option_has-meta" role="option">
      <span class="slds-media__figure optionIcon">
        <span class="slds-icon_container">
          <span class="slds-assistive-text">{#v.objectName}</span>
        </span>
      </span>
      <span class="slds-media__body singleRow">
        <span class="optionTitle slds-listbox__option-text slds-listbox__option-text_entity">{#item.text}</span>
      </span>
    </span>
  </li>
</aura:iteration>

Anyone can help me how to replace data-selectedindex in lwc . Also what can i use to create an id just like in span of aura . As i came to know i cannot have expression or evaluation in lwc for same. how can i do span id="{#'listbox-option-unique-id-'+i+1}" in lwc.

  • Please take a look at the Salesforce documentation for migration from Aura to LWC. This should help you resolve this for yourself. – Phil W Apr 7 at 14:44
1

You can either choose to use an existing value (e.g. the record Id):

data-record-id={item.Id}

Or you can add in your own:

@wire(...)
wireHandler({data,error}) {
  if(data) {
    this.serverResult = data.map((record,index)=>({...record, index: `${index}`}));
  }
  if(error) {
    // do error handling
  }
}
| improve this answer | |
  • I don't have any id associated with item as in item.id . I thik I have to add my own as you mentioned.But as I asked how can i do somehting like id="{#'listbox-option-unique-id-'+i+1}" .Elements in this are using same i value and manipulate it to get unique id by adding or concatenating , How to achieve this in lwc? – Abha Apr 8 at 5:04
  • @Abha Like I said, you generate it in the JavaScript, not the expressions. I even included an example where I used index to create a unique value; you could make this as complicated as you'd like. – sfdcfox Apr 8 at 8:32
  • Thanks . I am trying out as you suggested . – Abha Apr 8 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.