I am working in LWC requirements where I have a list of records using this list I need to create inputs text. Once I loop through the list in LWC 'HTML component' my input texts are coming as aspected but while I enter the value in the text box the same value is updated to the 2nd element of the array. Below is my code.
<template for:each={selectedCons} for:item="oli" for:index="index">
<template for:each={olilist} for:item="oli2" for:index="ids">
<tr key={oli.index}>
<td>{index}</td>
<td>
<lightning-input name='oliUnitPrice' access-key={index} type='Currency' id={index} onchange={changeHandler} value={oli2.UnitPrice}>
</lightning-input>
</td>
<td>
<lightning-input name='oliQuantity' access-key={index} type='Number' id={index} onchange={changeHandler} value={oli2.Quantity}>
</lightning-input>
</td>
<td>
</td>
</tr>
</template>
If I enter 7 the 2nd rows also gets updated with 7.
My Js Code below.
@track selectedCons;
@track olilist = [
{
Name: '',
ProductCode: '',
UnitPrice: '',
Quantity: '',
Product2Id : ''
}
];
showContacts() {
this.bShowModal = true;
this.selectedCons = [];
let selectedRows = this.template.querySelectorAll('lightning-input');
// based on selected row getting values of the contact
for(let i = 0; i < selectedRows.length; i++) {
if(selectedRows[i].checked && selectedRows[i].type === 'checkbox') {
this.selectedCons.push({
Name: selectedRows[i].value,
Id: selectedRows[i].dataset.id,
ProductCode : selectedRows[i].value,
Product2Id : selectedRows[i].value,
UnitPrice : selectedRows[i].value
})
}
}
}
changeHandler(event){
// alert(event.target.id.split('-'));
console.log('Access key2:'+event.target.accessKey);
console.log('id:'+event.target.id);
console.log('value:'+event.target.value);
console.log('name :'+event.target.name);
if(event.target.name==='oliUnitPrice'){
this.olilist[event.target.accessKey].UnitPrice = event.target.value;
console.log('name 2 :'+event.target.name);
}
else if(event.target.name==='oliQuantity'){
this.olilist[event.target.accessKey].Quantity = event.target.value;
console.log('name 2 :'+event.target.name);
}
console.log("Olilist"+JSON.stringify(this.olilist));
}
'selectedCons' is working properly. I am accessing it with 'accesskey'. Once deleting the 2nd rows element, getting below error.
Please help me in this!