dev noob here --
My company has been using this aura/lightning component on many of our lightning pages and it works very well. We borrowed the idea from the example in the link below. It's extremely useful to add fields to layouts this way. We can add multiple components to a single lightning page, and hide/show the component based on component visibility filters, which is awesome! Makes page layout work much easier.
My only complaint is that whenever we have an odd number of fields to display, the last field spans the length of 2 columns, making the UI look wonky. What can I add to this existing component and/or controller to prevent odd number of fields from spanning 2 columns?
I realize that I could simply change the number of columns to something other than 2, but I'm looking for a solution that works specifically with 2 column.
I imagine the code may need to check to make sure the number of columns selected is 2, check if there is an odd number of fields, and then prevent the last field from spanning 2 columns. Thanks for any suggestions!
Example of odd number of columns
Sample code
https://github.com/philldrum/Layout_Component
Component
<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId,force:hasSObjectName" access="global" >
<aura:attribute name="sObjectName" type="String" />
<aura:attribute name="fieldNamesString" type="String" default="Name" access="global" />
<aura:attribute name="fieldNames" type="String[]" access="global" />
<aura:attribute name="mode" type="String" access="global" default="view" />
<aura:attribute name="columns" type="Integer" access="global" default="2" />
<aura:attribute name="header" type="String" access="global" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:if isTrue="{!v.header == null || !v.header == '' }">
<lightning:recordForm recordId="{!v.recordId}"
objectApiName="{!v.sObjectName}"
fields="{!v.fieldNames}"
columns="{!v.columns}"
mode="{!v.mode}" />
<aura:set attribute="else">
<lightning:accordion allowMultipleSectionsOpen="true" activeSectionName="A">
<lightning:accordionSection name="A" label="{!v.header}" class="sectionHeader">
<lightning:recordForm recordId="{!v.recordId}"
objectApiName="{!v.sObjectName}"
fields="{!v.fieldNames}"
columns="{!v.columns}"
mode="{!v.mode}" />
</lightning:accordionSection>
</lightning:accordion>
</aura:set>
</aura:if>
</aura:component>
Controller
({
doInit : function(component, event, helper) {
let newFieldsArray = [];
let fieldNamesString = component.get("v.fieldNamesString");
if(fieldNamesString !== null && fieldNamesString !== undefined){
let fieldsArray = fieldNamesString.split(",");
for(let field of fieldsArray){
field = field.trim();
newFieldsArray.push(field);
}
component.set("v.fieldNames", newFieldsArray);
}
}
})