select.ftl

 1   
 2   <#-- Macro: select
 3   
 4   Description: Generates a select input field with options.
 5   
 6   Parameters:
 7   - name (string): the name of the select field.
 8   - id (string, optional): the ID of the select field.
 9   - class (string, optional): additional classes to apply to the select field.
 10   - items (list): a list of options for the select field. Each option is an object with "name" and "code" properties.
 11   - default_value (string, optional): the default value for the select field.
 12   - size (string, optional): the size of the select field (e.g. "sm", "md", "lg").
 13   - sort (boolean, optional): whether to sort the options alphabetically by name.
 14   - multiple (integer, optional): the number of visible options when the select field is expanded. If set to a value greater than 1, the select field allows multiple selections.
 15   - params (string, optional): additional parameters to add to the select field.
 16   - title (string, optional): the title attribute of the select field.
 17   - disabled (boolean, optional): whether the select field is disabled.
 18   - tabIndex (integer, optional): the tab index of the select field.
 19   - maxLength (integer, optional): the maximum length of each option name.
 20   - mandatory (boolean, optional): whether the select field is mandatory.
 21   
 22   -->
 23   <#macro select name id=name class='form-select' items='' default_value='' size='' sort=false multiple=0 params='' title='' disabled=false tabIndex=0 maxLength=0 mandatory=false>
 24   <#if propagateMandatory?? && propagateMandatory ><#local mandatory = true /></#if>
 25   <select id="${id}" name="${name}" class="<#if size!=''> form-control form-control-${size}</#if><#if class!=''> ${class}</#if>" <#if (multiple &gt; 0)>multiple size="${multiple}"</#if><#if (tabIndex &gt; 0)> tabindex="${tabIndex}"</#if><#if params!=''> ${params}</#if><#if title!=''> title="${title}"</#if><#if mandatory> required</#if><#if disabled> disabled</#if>>
 26   <#if items?has_content>
 27   <#if sort=true>
 28   <#list items?sort_by("name") as item>
 29   <#if default_value="${item.code}">
 30   	<option selected="selected" title="${item.name}" value="${item.code}" <#if !item.name?has_content>label="${i18n("portal.util.labelEmpty")}"</#if>>${item.name}</option>
 31   <#else>
 32   	<option value="${item.code}" title="${item.name}" <#if !item.name?has_content>label="${i18n("portal.util.labelEmpty")}"</#if>>${item.name}</option>
 33   </#if>
 34   </#list>
 35   <#else>
 36   <#if maxLength gt 0>
 37   	<#list items as item>
 38   	<#if maxLength < item.name?length >
 39   		<#assign item_new = "..." + "${item.name?substring(item.name?length-maxLength+3)}" >
 40   	<#else>
 41   		<#assign item_new = "${item.name}">
 42   	</#if>
 43   	<#if default_value="${item.code}">
 44   		<option selected="selected" title="${item.name}" value="${item.code}" >${item_new}</option>
 45   	<#else>
 46   		<option title="${item.name}" value="${item.code}" >${item_new}</option>
 47   	</#if>
 48   	</#list>
 49   <#else>
 50   	<#list items as item>
 51   	<#if default_value="${item.code}">
 52   		<option selected="selected" title="${item.name}" value="${item.code}" <#if !item.name?has_content>label="${i18n("portal.util.labelEmpty")}"</#if>>${item.name}</option>
 53   	<#else>
 54   		<option title="${item.name}" value="${item.code}" <#if !item.name?has_content>label="${i18n("portal.util.labelEmpty")}"</#if>>${item.name}</option>
 55   	</#if>
 56   	</#list>
 57   </#if>
 58   </#if>
 59   <#else>
 60   <#nested>
 61   </#if>
 62   </select>
 63   </#macro>