0

I'm using a for:each directive to render my data table. I'm also utilizing lightning-input to fire my onChange events and fetch the elements for that particular row.

Long story short, whenever I fire the onChange event, the event targets the very first row instead of its own:

enter image description here

I checked, and my event.target.dataset as it turns out returns undefined. Records already saved in the database are unaffected, but new rows created and aren't saved yet are. If such, what would be an alternative to event.target.dataset? How exactly can one correct this?

I need to keep it on the onChange event, so no row selections (currentTarget), sadly.

I made this playground, which I hope would better explain the issue I'm facing. The code in the playground's simplified but shows pretty much the same problem.

Thanks in advance!

1

You didn't actually add an ID anywhere, so find finds the first element and causes the problem on hand.

Here's the changes I made to your playground:

@track myList = [{Id:0, FirstName__c : "Nomen", LastName__c : "Niesco"},
          {Id: 1, FirstName__c : "Name", LastName__c : "Unbekannt"}];

add(){
     this.myList = [...this.myList, {id: this.myList.length+1, FirstName__c: '', LastName__c: ''}];
}

Note: The Id is just temporary until the record is saved, then obviously you'd replace it with the correct value from the database. The point is, you need a unique value for the ID so you can identify it.

2
  • Thanks, @sfdcfox. Does this mean I'm truly restricted to only save one record at a time? Until I save the newest line, I won't be able to fill-up or save the rest? I imagine others have this kind of functionality. What would be a viable workaround for this? – Sid Fortunato May 22 '20 at 1:54
  • @SidFortunato In my example, I generate a placeholder Id (it's just the current index); in real code, I'd probably set the index as a different key and use that instead. – sfdcfox May 22 '20 at 1:56

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.