Skip to content

Instantly share code, notes, and snippets.

@Sunil02kumar
Created February 26, 2017 02:55
Show Gist options
  • Select an option

  • Save Sunil02kumar/0bf29d8f56f9ded7986276f11a4f7b78 to your computer and use it in GitHub Desktop.

Select an option

Save Sunil02kumar/0bf29d8f56f9ded7986276f11a4f7b78 to your computer and use it in GitHub Desktop.
<aura:component >
<lightning:button label="Create Button" onclick="{!c.createButtonDynamically}" variant="brand"/>
<lightning:button label="Destroy All buttons" onclick="{!c.removeButtonDynamically}" variant="destructive"/>
<br/>
<div style="margin:auto; height:600px; width:400px; border:2px ridge red;" aura:id="newtag">
{!v.body}
</div>
</aura:component>
({
createButtonDynamically : function(component, event, helper) {
var db=component.find("newtag");
$A.createComponent(
"ui:button",
{
"label":"New Button"+db.get("v.body").length,
"press": component.getReference("c.showPressedButtonLabel")
},
function(bn)
{
var bdy=db.get("v.body");
bdy.push(bn);
db.set("v.body",bdy);
}
);
},
removeButtonDynamically : function(component, event, helper){
component.find("newtag").set("v.body",[]);
},
showPressedButtonLabel : function(component, event, helper){
alert('You pressed:'+event.getSource().get("v.label"));
}
})
<aura:application extends="force:slds" >
<c:CreateAndDestroyComp />
</aura:application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment