0

I have Multiple attributes in my component and need each to iterate along with each other. I can only iterate each attribute values separately as of now.

 Component
===========
<aura:component  access="global" controller ="ChatterFeedController">
<!--Attribute-->
<aura:attribute name = "groupattid" type="String" /> 
<aura:attribute name = "groupatt" type="String[]" /> 
<aura:attribute name = "actorphoto" type="String[]"  /> 
<aura:attribute name = "actorName" type="String[]"  /> 
<aura:attribute name = "attachPreview" type="String"  />   
<aura:attribute name = "attachment" type="String[]" /> 
<aura:handler name="init" action="{!c.userFeedItem}" value="{!this}"/>

 // Something like this
<div class ="slds">
 <aura:iteration>
 {!v.actorphoto}
 {!v.actorName}
  <br/>
 {!v.groupatt}
 {!v.attachment}
 </aura:iteration>
</div>
 </aura:component>


       Controller
      ============
  ({

userFeedItem: function(component, event, helper) {
    var userFeed = component.get("c.getFeedData");
    var groupID = component.get("v.groupattid");
     console.log("GroupID: " + groupID);
    userFeed.setParams({"groupId" : groupID});
    userFeed.setCallback(this,function(response){
    var state = response.getState();
        if(state==="SUCCESS"){
           var jtext      = response.getReturnValue();
           var groupatt   = component.get("v.groupatt");
           var actorName  = component.get("v.actorName");
           var actorPhoto = component.get("v.actorphoto");
           var attach     = component.get("v.attachment");
           var attachpreview = component.get("v.attachPreview");
    for (var i=0; i<jtext.elements.length; ++i){ 
        var actor = jtext.elements[i].actor;
        var body = jtext.elements[i].body;
        var item = jtext.elements[i].capabilities; 
          if(body && body.messageSegments.length){ 
                 groupatt.push(body.text);
               actorName.push(actor.name);
               actorPhoto.push(actor.photo.smallPhotoUrl);

          }
      component.set("v.groupatt",groupatt);
      component.set("v.actorName",actorName);
      component.set("v.attachment",attach);
      component.set("v.actorphoto",actorPhoto);
      console.log("attach"+attach);
    }

 } else {
            console.log('Problem getting feed, response state: ' + state);
        }    
    });
    $A.enqueueAction(userFeed);
}
  • I can see from your controller that groupatt, actorName, actorPhoto will have same number of items. Can you confirm that the other attribute attachment will also have same number of items? – Santanu Halder Apr 19 '17 at 6:03
  • Seems to me this begs for a different approach.....maybe... – Eric Apr 19 '17 at 6:57
  • Hello @SantanuHalder. No . Attachments may or may not be present. So it does not mean the number of items and attachment is same. But that is something that I have anticipated earlier and just added here to be changed later. My concern is only with the other attributes. – Brav Apr 19 '17 at 8:59
1

This seems to be not possible with the current approach.

You can change your approach slightly to this:

1. Instead of segregating the individual values from the jText.element, you can keep it like the way it is. I am guessing its a list of wrapper class instances, let's say the wrapper class name is: PersonInfo.

2. Declare a single attribute instead of 3 or 4 different lists.

<aura:attribute name = "personList" type="PersonInfo[]" />

3. Improvise your setCallback to this:

userFeed.setCallback(this,function(response){
  var personListLocal = component.get('v.personList');
  var state = response.getState();
  if(state==="SUCCESS"){
    var jtext= response.getReturnValue();
    for (var i=0; i<jtext.elements.length; ++i){ 
      personListLocal.push(jtext.elements[i]);
    }
    component.set('v.personList', personListLocal);
  }
}

4. Then loop through the personList in your component and access its values.

<aura:iteration items="{!v.personList}" var="person">
  {!person.actor}, {!person.body.text}, {!person.capabilities}
</aura:iteration>

Let me know if you have any questions.

  • Thanks @Santanu It worked perfectly.. One more thing.. The feed body I am getting is not properly displayed in the UI in proper format. It comes as a single string which is understandable as I am passing it to an attribute of String[] type. So instead of coming like this: < This is just for a test. 1. Paragraph 1 2. Paragraph 2 3. Paragraph 3 > it is coming like this: <This is just for a test. 1. Paragraph 1 2. Paragraph 2 3. Paragraph 3>. Is there any way to display it in proper format as which it was written? – Brav Apr 19 '17 at 19:40

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.