My lightning component's controller has an array named newResources and using aura:iteration to show a table based on the values in it. There is a method in js "removeNewResourceColumn" which removes one element from that array and then the aura:itration updates the view with one less row but when the method removes the last element of array the aura iteration view is not getting updated.

E.g. If I have 8 records in the array then the table will show 8 rows. If removeNewResourceColumn removes 6th element the table will show 7 records but if removeNewResourceColumn removes 8th element the table still shows 8 records. Has anyone ever faced this problem?

Here is the code snippet

Lightning Component

    <table class="slds-table slds-table_bordered slds-table_striped">
        <aura:iteration items="{!v.newResources}" var="newResource" indexVar="index">
            <td data-label="FirstName">
                <div class="slds-truncate">
                    <div class="slds-form-element__control" style="width:95%">
                        <div aura:id="nameField" class="slds-show">
                            <ui:inputText class="slds-input" value="{!newResource.FirstName}" placeholder="" disabled=""/>
            //It has more columns like first name


var RecordIndexStr = event.getSource().get("v.value");
var RecordIndexInt = parseInt(RecordIndexStr);
var newResources = component.get("v.newResources");

newResources.splice(RecordIndexInt, 1);

  • Can you add your minimal required code in your question by editing it so that community can have look what's going wrong with that? May 6 '19 at 7:46
  • Hey, @sanketkumar Added the code snippet. May 6 '19 at 8:08

Actually you have to use

component.set("v.newResources", JSON.parse(JSON.stringify(newResources));

Before setting that value.

In aura iteration we have to change the reference to memory to reflect the changes.

Or you can use slice function which returns a new array

newArray = newResources.slice(1, LengthofArray);
component.set("v.newResources", newArray);

Did you properly declare newResources as an Aura Attribute in your component ?

You should have <aura:attribute name="newResources" type="List"/> within your component.

  • 1
    Yes it is declared that is why it is working for the removal of any start or mid element. May 6 '19 at 9:45

On what basis are you deciding whether you want to delete 1st or 8th element in the list?? Keep in mind that List is based on zero-based indexing, so if you want to refer to last element then Size of the list minus 1 will give you last element.

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.