Search code examples
grailsgsp

How to assign an id in a dropdown menu to be able to know what was selected


I have a drop down menu with three selections. Three of them directs me to the same controller and same action, however I would like each to have different id assigned to be able to do different things depending on what the id is inside the controller.

So for example if I have three selections

<li> choice1 </li>
<li> choice2 </li>
<li> choice3 </li>

and a controller action list

def list = {

    if(choice1 was selected)
        //do something
    else if (choice2 selected)
        //do something else
}

I want to be able to do different things depending on what the choice was.

Here is my GSP code

<ul class="dropdown-menu" role="menu">
    <li><g:link controller='Document' action='list'>apple</g:link></li>
    <li><g:link controller='Document' action='list'>bluberry</g:link></li>
    <li><g:link controller='Document' action='list'>strawberry</g:link></li>
    <li class="divider"></li>
    <li><g:link resource="gm">v4sa</g:link></li>
</ul>

Solution

  • You can pass additional parameters to the link using the params attribute:

    <ul class="dropdown-menu" role="menu">
        <li><g:link controller='Document' action='list' params='[fruit:"apple"]'>apple</g:link></li>
        <li><g:link controller='Document' action='list' params='[fruit:"blueberry"]'>bluberry</g:link></li>      
        <li><g:link controller='Document' action='list' params='[fruit:"strawberry"]'>strawberry</g:link></li>    
        <li class="divider"></li>
        <li><g:link resource="gm">v4sa</g:link></li>
    </ul>
    

    which will add ?fruit=apple (or whatever) to the end of the link and make the value accessible as params.fruit in the controller.