2

I am having a lightning web component which iterates over a list and displays values in a list

HTML:

<template>
<template for:each={myList} for:item="myRecord">
    <div class="slds-truncate" title={rowSip.rowSIP.E2E_Volume_Target_FFE__c}>
        <lightning-input type="text"  name="input2" value={myRecord.Name} onchange={updateValues}></lightning-input>
    </div>
</template>

Javascript:

updateValues(event) {
    console.log(this.myList);
}

Problem:

In the 'updateValues' , I am consoling the list. But it has the old value.

Expected:

The list should give the modified value.

What could be the problem ? Or how to get the updated list values in Javascript ?

1 Answer 1

7

There is no way binding, thus you have to update the element in your list yourself. You can do that defining an attribute in your lightning-input and do array.find in the orignal list and then update that selected element.

<template>
    <template for:each={myList} for:item="myRecord">


        <lightning-input type="text" key={myRecord.Id} data-id={myRecord.Id} name="input2"  value={myRecord.Name} onchange={updateValues}></lightning-input>

    </template>
</template>

JS:

@track myList = [{Name: "MEOW" ,Id : 'I1'} , {Name: "Bark" ,Id : 'I2'}];



    updateValues(event){
        let foundelement = this.myList.find(ele => ele.Id == event.target.dataset.id);
        foundelement.Name = event.target.value;
        this.myList = [...this.myList];

        console.table(JSON.stringify(this.myList));



    }
8
  • console.log(this.myList); is outputting the old value. How to get the new value ? Jun 12, 2019 at 10:33
  • there is no two way binding, you have to manually edit list and update the value for your index. Jun 12, 2019 at 10:40
  • How can I atleast get the selected row ? Jun 12, 2019 at 10:42
  • @JunaidPKhader updated answer. Jun 12, 2019 at 11:46
  • 1
    Have you added data-id in the lightning-input Jun 12, 2019 at 12:19

You must log in to answer this question.

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