You need to sign in to do that
Don't have an account?

lightning:select to Iterate over list of strings
I have a list of strings that I want to display on my component. The issue I am currently having is actually displaying the values in the attribute.
My attribute is: <aura:attribute name="groupnumbers" type="group_structure__c[]" />
My select field is:
I am certain that my option value is the issue. How do I display the attribute when it is just a list of strings? Also, can I set a default?
Thank you!!!!
Fred
My attribute is: <aura:attribute name="groupnumbers" type="group_structure__c[]" />
My select field is:
<lightning:layoutItem padding="around-small"> <!-- Create a dropdown menu with options for Section code--> <lightning:select aura:id="selectGroupNum" label="GroupNum" name="sourceGroupNum" onchange="{!c.handleSelect}"> <aura:iteration items="{!v.groupnumbers}" var="gs"> <option value="{!gs}">{!gs.label}</option> </aura:iteration> </lightning:select> </lightning:layoutItem>
I am certain that my option value is the issue. How do I display the attribute when it is just a list of strings? Also, can I set a default?
Thank you!!!!
Fred
I trust you are doing very well.
Please use <option value="{!gs}" text="{!gs}" />
Below is the sample code to dynamically fetch picklist values and insert the records. Picklist values are List of string.
Custom Object: Registration_Form__c
Component:
Controller:
CSS:
Apex 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.
Thanks and Regards,
Khan Anas
All Answers
I trust you are doing very well.
Please use <option value="{!gs}" text="{!gs}" />
Below is the sample code to dynamically fetch picklist values and insert the records. Picklist values are List of string.
Custom Object: Registration_Form__c
Component:
Controller:
CSS:
Apex 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.
Thanks and Regards,
Khan Anas
Here is a solution to your Problem:
---------Component--------
<aura:component controller="AuraUtility">
<aura:attribute name="fieldList" type="String[]"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<div class="slds-m-vertical_xx-large slds-m-horizontal_xx-large">
<aura:if isTrue="{!v.fieldList.length > 0}">
<table class="slds-table slds-table_bordered slds-table_cell-buffer" >
<thead>
<tr class="slds-text-title_caps">
<th scope="col" >
<div class="slds-truncate" title="Field Name"><b>Field Name</b></div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="Fields Name" >
<lightning:select name="select1">
<aura:iteration items="{!v.fieldList}" var="fld" >
<option value="{!fld}" text="{!fld}"></option>
</aura:iteration>
</lightning:select>
</td>
</tr>
</tbody>
</table>
</aura:if>
</div>
</aura:component>
---------Controller_JS--------
({
doInit : function(component, event, helper) {
console.log('helper Calling>>>>>>>');
helper.displayField_helper(component, event, helper);
}
})
---------Helper_JS--------
({
displayField_helper : function(component, event, helper) {
console.log('>>>>>>>>>>>>>Enter in FieldFunc.');
var action = component.get("c.displayFieldLabel");
action.setCallback(this, function(response) {
if (response.getState() == "SUCCESS") {
var allValues = response.getReturnValue();
console.log('Fields List>>>>>>>>>>>>.'+allValues);
component.set("v.fieldList", allValues);
console.log('Fields List>>>>>>>>>>>>.'+component.get("v.fieldList"));
}
});
$A.enqueueAction(action);
}
})
--------Aura Utility_Apex---------------
public class AuraUtility {
@AuraEnabled
public static List< String > displayFieldLabel() {
List<string> lstfield =new list<string>();
Schema.SObjectType objType = Schema.Account.getSObjectType();
Schema.DescribeSobjectResult objResult = objType.getDescribe();
Map<String, Schema.SObjectField> mapOfField = objResult.fields.getMap();
for(String s : mapOfField.keyset()){
lstfield.add(mapOfField.get(s).getDescribe().getLabel());
}
system.debug('List of Fields>>>>>>'+ lstfield);
lstfield.sort();
return lstfield;
}
}
Please mark this as Best Answer if you find this solution helpful.
Thank You
Ajay Dubedi
If you use this code, it will display the first item in the picklist.
If you want 'None' in the picklist, you can use below code:
Regards,
Khan Anas
Fred