Saturday, 19 March 2016

How to create Action Menu in Odoo

Hello guys how are you, I hope you will be fine and also enjoying this tutorial!!! In my previous post you learned about creating and installing a module in odoo.Today you will learn how to create an action menu. Before going to start our lesson we need to know about "Action Menus".



Action Menu:
      Menus are record in the ir.ui.menu table, and the action defines the behavior of the system in response to the action of the users. Action can be triggered by different ways, first one is by clicking on menu items for example we linked our menu with a specific action. Second is by clicking on button and the button exists on some views. There are different types of action (window, report, group etc...)

Window: This action will be used for opening a new window.
Report: For printing a report.
Group: Gather some action in one group.

How to create action menu:
       Menus are complex to declare so there is <menuitem> short cut to declare ir.ui.menu and connect it to the corresponding action menu.

<record model="ir.actions.act_window" id="action_list_ideas">
    <field name="name">Ideas</field>
    <field name="res_model">idea.idea</field>
    <field name="view_mode">tree,form</field>
</record>
<menuitem id="menu_ideas" parent="menu_root" name="Ideas" sequence="10"
          action="action_list_ideas"/>
 
 
The action must be declared before its corresponding menu in the XML file. Below screen shot shows menu and its corresponding action.

 

Action have (name, res_model and view_mode attributes), in the "res_model" attribute we will give the name of our model (classes) from which we will bind our action and in the "view_mode" we describe that our view will be tree, form or both.

Menuitem  have name, id, sequence, parent,string and action attributes. Here "id" is unique for each menu, "name" shows the name of our menu, "string" shows the name of your string which you want to display on menu. In the parent attribute we will give the name of "parent_menu_id", "sequence" attribute defines the sequence for the menu displaying in your view. Here the most important attribute is "action", in this attribute we will give the id of our action which we have already declared in the action (as shown in the above figure) here the name of action id is "action_list_ideas" and also the name of our menuitem action is "action_list_ideas". Remember the name of action attribute in <menuitem> must be same as the name of "id" in "<Action>".

Example:
   In this example we will create action menu, and our created menu will triggering the action. 
  1. Create an empty module, below screen shot shows the structure of the module.
     
    2. Now create example_action_menu.xml file in  module_name/views folder in your created module.
    /module_name/views/example_action_menu.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <openerp>
        <data>
            <!-- window action -->
            <!--
                The following tag is an action definition for a "window action",
                that is an action opening a view or a set of views
            -->
            <record model="ir.actions.act_window" id="example_list_action">
                <field name="name">Example List Action</field>
                <field name="res_model">example.model.name</field>
                <field name="view_type">form</field>
                <field name="view_mode">tree,form</field>
                <field name="help" type="html">
                    <p class="oe_view_nocontent_create">Create the first Example
                    </p>
                </field>
            </record>
    
            <!-- top level menu: no parent -->
            <menuitem id="main_example_menu" name="Main Example Menu"/>
            <!-- A first level in the left side menu is needed
                 before using action= attribute -->
            <menuitem id="example_menu" name="Example Menu"
                      parent="main_example_menu"/>
            <!-- the following menuitem should appear *after*
                 its parent example_menu and *after* its
                 action example_list_action -->
            <menuitem id="example" name="Example" parent="example_menu"
                      action="example_list_action"/>
            <!-- Full id location:
                 action="module_name.example_list_action"
                 It is not required when it is the same module -->
        </data>
    </openerp>
    
    3. Add your xml file in the data file of  __openerp.py__ file    
     
    In our case it should be:
    'data': [
            # 'security/ir.model.access.csv',
            'templates.xml',
            'views/example_action_menu.xml',
            ],
    Now this is the end of this tutorial, today you learned what is action menu and how to create action menus. In my next post you will learn about View (form, tree)  

No comments:

Post a Comment