You need to sign in to do that
Don't have an account?
Connor Cain
Lighting-Combobox returning null when selecting a value
hello everyone,
We have a Lightning Web Component with 3 Comboboxes. I built out the first one using the code from the Lighting Web Component resource page and it works great. I then copied and pasted the code (while changing the value names and labels as to not cause an error) but the two new Combo Boxes arent working. when selecting a value the ComboBox returns a null value rather than the initial ' ' value and doesnt select the value I wanted to select.
We have a Lightning Web Component with 3 Comboboxes. I built out the first one using the code from the Lighting Web Component resource page and it works great. I then copied and pasted the code (while changing the value names and labels as to not cause an error) but the two new Combo Boxes arent working. when selecting a value the ComboBox returns a null value rather than the initial ' ' value and doesnt select the value I wanted to select.
Please share the code snippet.
Cheers!!!
lightning:combobox is an input element that enables single selection from a list of options. The result of the selection is displayed as the value of the input.A
widget that provides an input field that is read-only, accompanied with a dropdown list of selectable options.In this example, once the user selects the Department
from the contact, it will get all the contacts and display as the table . Please refer to the following code as it may be helpful in solving your problem:
Apex Class:
public class ContactsController {
@AuraEnabled
public static List<String> getDepartment () {
Set<String> uniqDepartment = new Set<String>();
List<Contact> results=[Select Id, Department from Contact];
for(Contact c : results){
if(c.Department!=null){
uniqDepartment.add(c.Department);
}
}
List<String> finalResult = new List<String>();
finalResult.addAll(uniqDepartment);
finalResult.sort();
return finalResult;
}
@AuraEnabled
public static List<Contact> getContactsByDept (String deptName) {
List<Contact> results=[Select Id,FirstName,LastName,Email,Department from Contact where Department=:deptName];
return results;
}
}
Lightning Component:
<aura:component controller="ContactsController">
<aura:attribute name="statusOptions" type="List" default="[]"/>
<aura:attribute name="contacts" type="Contact[]" default="[]"/>
<aura:handler name="init" value="{! this }" action="{! c.loadOptions }"/>
<lightning:combobox aura:id="selectItem" name="status" label="Status"
placeholder="Choose Status"
value="new"
required="true"
dropdownAlignment="right"
variant="standard"
messageWhenValueMissing="You Must Select the Value"
onchange="{!c.handleOptionSelected}"
options="{!v.statusOptions}"/>
<table class="slds-table slds-table--bordered">
<thead>
<th>
LastName
</th>
<th>
FirstName
</th>
<th>
Email
</th>
</thead>
<tbody>
<aura:iteration items="{!v.contacts}" var="row">
<tr>
<td>
{!row.LastName}
</td>
<td>
{!row.FirstName}
</td>
<td>
{!row.Email}
</td>
</tr>
</aura:iteration>
</tbody>
</table>
</aura:component>
Controller:
({
loadOptions: function (cmp, event, helper) {
var options = [
];
var action = cmp.get("c.getDepartment");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var arr = response.getReturnValue() ;
arr.forEach(function(element) {
options.push({ value: element, label: element });
});
cmp.set("v.statusOptions", options);
}
});
$A.enqueueAction(action);
},
handleOptionSelected: function (cmp, event) {
var action = cmp.get("c.getContactsByDept");
action.setParams({deptName:event.getParam("value")});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
cmp.set("v.contacts" , response.getReturnValue() ) ;
}
});
$A.enqueueAction(action);
}
})
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
Combobox 1 is working fine but 2 and 3 are passing null values into my apex code
HTML: JS