Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
<aura:component > <aura:attribute name="jsonArray" type="Object[]" /> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <table class="slds-table slds-table--bordered"> <tbody> <aura:iteration items="{!v.jsonArray}" var="row"> <tr> <td> {!row.color}</td> <td> {!row.value}</td> </tr> </aura:iteration> </tbody> </table> </aura:component>
({ doInit : function(component, event, helper) { var jsonObj =[ { color: "red", value: "#f00" }, { color: "green", value: "#0f0" }, { color: "blue", value: "#00f" }, { color: "cyan", value: "#0ff" }, { color: "magenta", value: "#f0f" }, { color: "yellow", value: "#ff0" }, { color: "black", value: "#000" } ]; component.set("v.jsonArray", jsonObj); } })
<aura:attribute name="people" type="Map[]" default = "[{ to: [ { name: 'ToDave', email: 'dave@ToEmail' }, { name: 'ToBob', email: 'bob@ToEmail' }], from: [ { name: 'FromDave', email: 'dave@FromEmail' }], cc: [ { name: 'CcSue', email: 'sue@CcEmail' }, { name: 'CcMary', email: 'mary@CcEmail' }] }]"/>
var jsonBigObj = [{ to: [ { name: "cmpToDave", email: "cmpDave@ToEmail" },{ name: "cmpToHilary", email: "cmpHilary@ToEmail" } ], from: [ { name: "cmpFromDave", email: "cmp@fromEmail" } ], cc: [ { name: "cmpCcWendy", email: "cmpWendy@ccEmail" },{ name: "cmpCcCruella", email: "cmpCruella@ccEmail" }] }]; component.set("v.people", jsonBigObj);
<table class="slds-table slds-table--bordered" title="multi level array"> <thead>People</thead> <tbody> <aura:iteration items="{!v.people}" var="person"> <aura:iteration items="{!person.to}" var="level2to"> <tr> <td>{!level2to.name}</td><td>{!level2to.email}</td> </tr> </aura:iteration> <aura:iteration items="{!person.from}" var="level2from"> <tr> <td>{!level2from.name}</td><td>{!level2from.email}</td> </tr> </aura:iteration> <aura:iteration items="{!person.cc}" var="level2cc"> <tr> <td>{!level2cc.name}</td><td>{!level2cc.email}</td> </tr> </aura:iteration> </aura:iteration> </tbody> </table>
All Answers
Firstly, I found the recommendation (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_attr_types_object.htm) that Map[] should be used instead of Object[].
and found the syntax for using in default:
Or, working from your example, to create it in the controller:
And to iterate in a table:
I am hitting a api which is responding me a Json Array. I am writing the code in Apex class in lightning component.
Apex code:
String op=resp.getBody(); List<Object> result = (List<Object>)JSON.deserializeUntyped(op);
return result;
Helper class:
var returnb = response.getReturnValue(); component.set("v.jsonArray", returnb);
In cmponent:
<aura:attribute name="jsonArray" type="Object[]"/> <aura:if isTrue= "{!not(empty(v.jsonArray))}" >
<table class="slds-table slds-border_left slds-border_right">
<tbody>
<aura:iteration items="{!v.jsonArray}" var="row">
<tr>
<td> {!row.id} </td>
this row.id is not printing & returning an error.
The json response is something like:
[{"id": "wwkj", "name": "loki" }, {"id": "wyht", "name": "ann" }, . . . . ]
Please let me know what's wrong with this.