Search code examples
grailsgsp

How to limit a multiple selection of a <g:select>


I want to limit a multiple selection to 1, because I want this format of list. I want to see all list always and don't want a dropdown list, and If I quit multiple="multiple" apears a dropdown list.

So, this is the code:

gsp:

<div class="field ${hasErrors(bean: cmd, field: 'subtypes', 'error')}">
    <label for="subtypes">
        <g:message code="requestSRO.requestSubtype.label"/>
    </label>
    <g:select name="subtypes" from="${requestSubtypes}" optionKey="code" multiple="multiple" optionValue="description" value="${cmd?.subtypes}" style="width: 100%;height: 100px" disabled="${!requestSubtypes ? true: false}"/>
</div>

I think <g:select> doesn't have a property to set the max limit of selections.


Solution

  • Don't use multiple="multiple" to change the way the select is displayed. Instead, set the size attribute to a number greater than 1.

    <g:select size="2" name="subtypes" from="${requestSubtypes}" optionKey="code" optionValue="description" value="${cmd?.subtypes}" style="width: 100%;height: 100px" disabled="${!requestSubtypes ? true: false}"/>
    

    If you want to display the entire list of subtypes, you could set size="${requestSubtypes.size()}".