The problem
You want to create a contextual menu, which shows either: - When users click on a link.
- When the position the mouse over a certain area.
You also want to be able to react to users selecting an entry from the menu, either by: - Setting the value of a node in an instance.
- Running an action.
|
|
 |
| Using a dialog |
Using a menu |
The solution
There are two ways of creating contextual menus:
- Using a dialog
- Using a menu
| Result | Steps | Benefits | Drawbacks | | Using a dialog |
| - Define an
xxforms:dialog. - Use the attribute
appearance="minimal" so the dialog doesn't get a title bar. - With
xxforms:show, show the dialog when users click on the link and specify the id of the trigger in the neighbor attribute, so the dialog is positioned next to the trigger. - In the dialog, when users make a selection, either store a value in an instance or run the desired actions.
| You can fully control the content of the dialog. In this example the dialog contains only links, but it could contain a more sophisticated UI. | If you just want a simple list, using a dialog makes your code more complicated. Also it requires one more click, as they need to click on the link to show the dialog. | | Using a menu |
| - Use
xforms:select1 as if you wanted to create a drop-down, with the list of items specified with an xforms:itemset. - Use
appearance="xxforms:menu" on the xforms:select1 to have the control rendered as a menu. The appearance="xxforms:menu" is designed to create menus similar to those you have in desktop applications. Those in general have multiple top-level items (File, Edit, View...). In this case, you want to have just one top-level item; in this example: "Select state". Your itemset needs to contain that top-level item, and it needs to be defined in node that is parent of the node corresponding to the other items, as in:
<xforms:instance id="state-itemset">
<states
label="Select state"
value="">
<state value="ca"
label="California"/>
<state value="md"
label="Maryland"/>
<state value="va"
label="Virginia"/>
</states>
</xforms:instance>
| Your code is simple, the interface has a "modern" look, and is efficient as users only need one click to make a selection. | With a menu, the content of the menu is a list and can't be as sophisticated as what you can do with a dialog. |
Run it and get the source
|
|