selectgroovydrop-down-menugsp

How to disable specific options in a select in Groovy Server Pages (gsp)


I have the below select tag in my gsp which is working fine.

<q:select id="myItem" 
      name="myItem" 
      from="${items}" 
      optionKey="id" 
      value="${item?.id}"
      optionValue="${{it?.name)}}"
      noSelection="${['': '']}" />

But I need to disable couple of options within the select based on its status attribute.

items = [{
          id: 1,
          name: 'test',
          status: 'available'
         },
         {
          id: 2,
          name: 'john',
          status: 'booked'
         },
         {
          id: 3,
          name: 'sans',
          status: 'available'
         }];

I want the option item with status booked to be shown as readonly or disabled. How to achieve this.


Solution

  • You just need to add an optionDisabled attribute like:

    optionDisabled="${{it?.status == 'booked'}}
    

    to your <g:select>.

    EDIT: for anyone coming to this later, note @HansMaulwurf comment below noting that you'll need to use optionKey as well.