Search code examples
cakephpdropdownbox

CakePHP Custom Dropdown Menu


I have a dropdown list and the items are quite a lot (over 20 items).

I am using CakePHP and this is how I code my dropdown list:

<?php
     echo '<div class="form-group">';
     echo $this->Form->input('type', array(
         'class' => 'form-control',
         'id' => 'listType',
         'options' => array(
                'Item 1' => 'Item 1',
                'Item 2' => 'Item 2',
                'Item 3' => 'Item 3'),
         'empty' => '--Choose Item Type--',
         'label' => false
     ));
     echo '</div>';
?>

Now, there are some items in my dropdown list which have sub-items. Is there any way to do that?

For example, just like this kind of dropdown:

Fruits
   Apple
   Banana
   Orange
   Grape
Vegetables
   Carrot
   Potato
Chips
Sausage

I want ALL items EXCEPT with sub-items to be clickable. I also want ALL sub-items to be clickable.

So in short, items 'Fruits' and 'Vegetables' cannot be clicked, while the rest can be clicked. And is there any way to put a divider?

Thank you! It would be better if someone could provide an example using CakePHP format.


Solution

  • Ok, after a few experimentation I finally figured out how to do it.

    <?php
         echo '<div class="form-group">';
         echo $this->Form->input('type', array(
             'class' => 'form-control',
             'id' => 'listType',
             'options' => array(
                    'Item 1' => 'Item 1',
                    'Item 2' => array(
                         'Subitem 1' => 'Subitem 2',
                         'Subitem 2' => 'Subitem 2'
                    ),
                    'Item 3' => 'Item 3'),
             'empty' => '--Choose Item Type--',
             'label' => false
         ));
         echo '</div>';
    ?>