I have a list in which every time I click the "Adicionar outro CID" button a new line with a field and a button "Remover" is added just below it:

Original image:

enter image description here

After clicking the button:

enter image description here

If I click the button more often, more equal lines are added:

enter image description here

The problem is that when I click on "Remover" a list always excludes a last independent quality line "Remover" I click. For example, if I click button 1, the third line is removed:

enter image description here

Part of the code that is created when I click on "Adicionar outro CID":

<lightning:layout multipleRows="true" class="slds-grid_vertical max-1">
            <aura:iteration items="{!v.listaCID}" indexVar="index">
                <c:LookupComponent objectName="CIDs__c" fieldName="Name" campos="Id, Name" campoordenacao="Name"
                  label="CID" labelitalico="(busque pelo nº do CID ou pelo nome da doença)"
                  messageLookup="Mostrar todos os CIDs com essa descrição"
                  placeholder="Digite ao menos 3 iniciais do CID ou nome da doença" />
                  <lightning:button name="{!index}" class="btnAddRemoveCID" title="Remover CID" type="button"
                  label="REMOVER" onclick="{!c.removeCIDsrow}"></lightning:button>     


    removeCIDsrow: function (component, event, helper) {
    helper.removeCIDsrow(component, event);



    removeCIDsrow: function (component, event) {
    var newlistaCID = component.get("v.listaCID");

    console.log('new listalenght =>' + newlistaCID.length);
    var toDeletIndex = event.getSource().get("v.name");

    newlistaCID.splice(toDeletIndex, 1);
    component.set("v.listaCID", newlistaCID);

The variable "toDeletIndex" is returning the correct value of the index I am deleting.

Sorry for the size of the text. Thank you.

  • If they all have the same data, how do you know it's the third one being deleted? – sfdcfox Jun 4 '19 at 19:55
  • @sfdcfox I use only the index to differentiate them. I'm new to this development part but I suspected that the list items having the same value but differents index values, javascript might not understand which one to exclude, even though I pass the index value, and always delete the last one. Where and how could I add values ​​to differentiate them? – user69416 Jun 4 '19 at 20:19
  • JavaScript's "splice" deletes the index you specify. As far as I can tell, it looks like it should be doing what you've told it to. I was asking how you know it's deleting the third one as opposed to the first. Since they're all identical values, how would you know which row was removed? – sfdcfox Jun 4 '19 at 20:23
  • @sfdcfox I use only the index although all values ​​are equal (empty) – user69416 Jun 4 '19 at 20:27
  • Try entering different values on the input fields, then remove the row. It should be more visually obvious if it's working right. – sfdcfox Jun 4 '19 at 20:31

Your Answer

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

Browse other questions tagged or ask your own question.