You need to sign in to do that
Don't have an account?
K Roy
Lightning Combobox Issue
Hello Team,
I have recently started working in lightning. I am trying to work on a scenario, where i will be displaying the Account name in a dropdown. On selecting an Account, it will show the corresponding Contacts associated with the account. To achieve this, i am using lightning:Combobox.
My Component:
<aura:component implements="flexipage:availableForAllPageTypes" access="global" controller ="AccountDropDownListController">
<aura:attribute name="accounts" type ="Account[]"/>
<aura:handler name = "init" value="{!this}" action="{!c.init}" />
<lightning:combobox name="general" label="Account List" placeholder="Select an Account" options="{! v.accounts }" onchange="{! c.handleChange }"/>
</aura:component>
Apex Controller:
public class AccountDropDownListController {
@AuraEnabled
public static List <String> getAccounts()
{
List<Account> lstAccount= [SELECT Id, name, industry, Type, NumberOfEmployees, TickerSymbol, Phone FROM Account ORDER BY createdDate ASC];
List<String> lstStrngAccount = new List<String>();
for(Account acc:lstAccount)
{
lstStrngAccount.add(acc.Name);
}
return lstStrngAccount;
}
}
Client Side Controller:
({
init: function (component, event, helper) {
helper.getAccountList(component);
},
handleChange: function (component, event,helper) {
// This will contain the string of the "value" attribute of the selected option
var selectedAccountValue = event.getParam("value");
alert("Account selected with value: '" + selectedAccountValue + "'");
}
})
Helper:
({
getAccountList: function(component) {
var action = component.get('c.getAccounts');
var self = this;
action.setCallback(this, function(actionResult) {
component.set('v.accounts', actionResult.getReturnValue());
var k = actionResult.getReturnValue();
var items = [];
for (var i = 0; i < k.length; i++) {
var item = {
"label": k[i] + " Option",
"value": k[i].toString(),
};
items.push(item);
}
component.set("v.accounts", items);
});
$A.enqueueAction(action);
}
})
What i am unable to do is, i am not able to click the Accounts, that i am selecting. How can i do that? Also after clicking, how can i get the corresponding contacts associated with those accounts?
Request you all to kindly guide me on this.
Regards,
Rahul
I have recently started working in lightning. I am trying to work on a scenario, where i will be displaying the Account name in a dropdown. On selecting an Account, it will show the corresponding Contacts associated with the account. To achieve this, i am using lightning:Combobox.
My Component:
<aura:component implements="flexipage:availableForAllPageTypes" access="global" controller ="AccountDropDownListController">
<aura:attribute name="accounts" type ="Account[]"/>
<aura:handler name = "init" value="{!this}" action="{!c.init}" />
<lightning:combobox name="general" label="Account List" placeholder="Select an Account" options="{! v.accounts }" onchange="{! c.handleChange }"/>
</aura:component>
Apex Controller:
public class AccountDropDownListController {
@AuraEnabled
public static List <String> getAccounts()
{
List<Account> lstAccount= [SELECT Id, name, industry, Type, NumberOfEmployees, TickerSymbol, Phone FROM Account ORDER BY createdDate ASC];
List<String> lstStrngAccount = new List<String>();
for(Account acc:lstAccount)
{
lstStrngAccount.add(acc.Name);
}
return lstStrngAccount;
}
}
Client Side Controller:
({
init: function (component, event, helper) {
helper.getAccountList(component);
},
handleChange: function (component, event,helper) {
// This will contain the string of the "value" attribute of the selected option
var selectedAccountValue = event.getParam("value");
alert("Account selected with value: '" + selectedAccountValue + "'");
}
})
Helper:
({
getAccountList: function(component) {
var action = component.get('c.getAccounts');
var self = this;
action.setCallback(this, function(actionResult) {
component.set('v.accounts', actionResult.getReturnValue());
var k = actionResult.getReturnValue();
var items = [];
for (var i = 0; i < k.length; i++) {
var item = {
"label": k[i] + " Option",
"value": k[i].toString(),
};
items.push(item);
}
component.set("v.accounts", items);
});
$A.enqueueAction(action);
}
})
What i am unable to do is, i am not able to click the Accounts, that i am selecting. How can i do that? Also after clicking, how can i get the corresponding contacts associated with those accounts?
Request you all to kindly guide me on this.
Regards,
Rahul
Greetings to you!
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Apex:
Component:
Controller:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
It worked for me too. Thanks for the solution. However, can you tell me, how can i remove the checkbox from the contacts table? I dont need it as of now.
It's my pleasure. I’m glad I was able to help!
You can use hideCheckboxColumn="true" attribute in lightning:datatable to disable the checkboxes.
Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.
Regards,
Khan Anas
Its working fine as expected. Thanks for the help. One more thing, if i want to delete the contacts one by one, based on the accounts, how to do that?
Can you kindly help on this?
Regards,
Rahul