Then you can also test these functions in a similar way:
Code: Select all
function nuLabelLeftAligned(include, exclude, offsetTop = 0, offsetLeft = 'auto', maxWidth = null, gap = 10) {
include = include || nuSERVERRESPONSE.objects.map(obj => obj.id);
exclude = exclude || [];
for (let i = 0; i < include.length; i++) {
if (jQuery.inArray(include[i], exclude) == -1) {
$element = $('#' + include[i]);
$label = $('#' + 'label_' + include[i]);
let leftPosition;
// Determine optimal left position
if (offsetLeft === 'auto') {
// Method 1: Calculate based on actual label width
let labelWidth = $label.outerWidth();
if (labelWidth === 0) {
// If label not rendered yet, use text length estimation
let labelText = $label.text();
let estimatedWidth = labelText.length * 8; // rough estimation: 8px per character
labelWidth = Math.min(estimatedWidth, maxWidth || 200);
}
leftPosition = $element.nuCSSNumber('left') - labelWidth - gap;
} else if (typeof offsetLeft === 'string' && offsetLeft.includes('%')) {
// Method 2: Percentage-based positioning relative to element width
let percentage = parseFloat(offsetLeft.replace('%', '')) / 100;
let elementWidth = $element.outerWidth();
leftPosition = $element.nuCSSNumber('left') - (elementWidth * percentage);
} else {
// Method 3: Fixed offset (original behavior)
leftPosition = $element.nuCSSNumber('left') + offsetLeft;
}
let cssProperties = {
'top': $element.nuCSSNumber('top') + offsetTop,
'left': leftPosition,
'text-align': 'left'
};
// Add max-width and text wrapping if specified
if (maxWidth !== null && maxWidth > 0) {
cssProperties['max-width'] = maxWidth + 'px';
cssProperties['white-space'] = 'normal';
cssProperties['word-wrap'] = 'break-word';
cssProperties['overflow-wrap'] = 'break-word';
}
$label.css(cssProperties);
$element.attr('data-nu-label-position', 'left');
}
}
}
Alternative approach: Calculate optimal positioning for all labels collectively
Code: Select all
function nuLabelLeftAlignedOptimal(include, exclude, offsetTop = 0, maxWidth = null, gap = 10) {
include = include || nuSERVERRESPONSE.objects.map(obj => obj.id);
exclude = exclude || [];
// First pass: collect all label widths
let labelWidths = [];
let maxLabelWidth = 0;
for (let i = 0; i < include.length; i++) {
if (jQuery.inArray(include[i], exclude) == -1) {
$label = $('#' + 'label_' + include[i]);
let labelWidth = $label.outerWidth();
if (labelWidth === 0) {
// Estimate width if not rendered
let labelText = $label.text();
labelWidth = Math.min(labelText.length * 8, maxWidth || 200);
}
labelWidths.push(labelWidth);
maxLabelWidth = Math.max(maxLabelWidth, labelWidth);
}
}
// Second pass: position all labels using the maximum width found
let labelIndex = 0;
for (let i = 0; i < include.length; i++) {
if (jQuery.inArray(include[i], exclude) == -1) {
$element = $('#' + include[i]);
$label = $('#' + 'label_' + include[i]);
// Use consistent positioning based on widest label
let leftPosition = $element.nuCSSNumber('left') - maxLabelWidth - gap;
let cssProperties = {
'top': $element.nuCSSNumber('top') + offsetTop,
'left': leftPosition,
'text-align': 'left',
'width': maxLabelWidth + 'px' // Ensure consistent width
};
// Add max-width and text wrapping if specified
if (maxWidth !== null && maxWidth > 0) {
cssProperties['max-width'] = Math.min(maxLabelWidth, maxWidth) + 'px';
cssProperties['white-space'] = 'normal';
cssProperties['word-wrap'] = 'break-word';
cssProperties['overflow-wrap'] = 'break-word';
}
$label.css(cssProperties);
$element.attr('data-nu-label-position', 'left');
labelIndex++;
}
}
}
nuLabelLeftAligned (Dynamic positioning)
Best practices implemented:
- Auto-calculation: Set offsetLeft = 'auto' to automatically calculate position based on actual label width
- Percentage-based: Use offsetLeft = '50%' for positioning relative to element width
- Gap parameter: gap parameter for consistent spacing between label and element
- Fallback estimation: If label isn't rendered yet, estimates width based on text length
nuLabelLeftAlignedOptimal (Consistent alignment)
Best for forms with multiple fields:
- Two-pass approach: First calculates all label widths, then positions them consistently
- Uniform alignment: All labels align to the same left position (based on widest label)
- Consistent width: All labels get the same width for perfect alignment
Usage Examples:
Auto-calculate optimal distance
nuLabelLeftAligned(null, null, 0, 'auto', 120, 5);
Percentage-based positioning
nuLabelLeftAligned(null, null, 0, '75%', 100);
Consistent alignment for all labels (best for forms)
nuLabelLeftAlignedOptimal(null, null, 0, 150, 8);
Best Practices Summary:
- Use 'auto' for individual optimal positioning
- Use percentage for responsive layouts
- Use nuLabelLeftAlignedOptimal for forms where you want perfect label alignment
- Set appropriate gap values (5–15px typically works well)
- Consider maxWidth to prevent labels from being too wide on different screen sizes
The optimal approach depends on your specific use case — individual dynamic positioning vs. consistent form alignment.