Today, there are netizens asking how to add a custom button in the upper left corner of the TreeView, in the query of Odoo's own module, found in the Purchase_requisition, and this module is also applied to the custom View_mode case, so take it out today to analyze , interested children shoes can refer to the instructions to see the details in the module.
First, the module displays the effects such as:
This is in the tender order, if a product has more than one RFQ, the completion of the bidding order, the system will display this interface, you need to confirm a final valid inquiry, this interface needs to open the configuration parameters as shown in the project will be displayed.
Careful analysis of the source code, this view is defined in the Purchase_requisition_view.xml, the main content is as follows:
<record id="Purchase_order_line_tree_tender"Model="Ir.ui.view"> <field name="name">purchase.order.line.tree.tender</field> <field name="Model">purchase.order.line</field> <field eval="1"Name=" Priority"/> <field name="Arch"Type="XML"> <tree string="Purchase Order Lines"Create="false"colors="Blue:state = = ' confirmed '; gray:state = = ' Cancel '"> <field name="name"/> ...... </tree> </field> </record>
Then we define an action for this view, the main contents are as follows:
<record id="Purchase_line_tree"Model="Ir.actions.act_window"> <field name="name">bid lines</field> <field name="Res_model">purchase.order.line</field> <field name="Context">{"search_default_groupby_product": true,}</field> <field name="View_type">form</field> <field name="View_mode">tree_purchase_order_line_compare</field> <field name="view_id"Te ="Purchase_order_line_tree_tender"/> <field name="search_view_id"Te ="Purchase.purchase_order_line_search"/> </record>
Mainly see above action definition, View_mode node is put tree_purchase_order_line_compare, and ordinary tree,form is not the same, this new View_mode where come?
We continue to look at the contents of Addons/purchase_requisition/static/src/js/web_addons.js, and one of the main contents is as follows:
Instance.web.views.add ('Tree_purchase_order_line_compare','Instance.web.purchase_requisition.CompareListView'); Instance.web.purchase_requisition.CompareListView=Instance.web.ListView.extend ({init:function () {var self=This ; This._super.apply (this, arguments); This.on ('list_view_loaded', this, function () {if(self.)__parentedparent. $el. Find ('. Oe_generate_po'). Length = =0) {var button= $("<button type= ' button ' class= ' Oe_button oe_highlight oe_generate_po ' >generate po</button>"). Click (This.proxy ('Generate_purchase_order')); Self.__parentedparent. $el. Find ('. Oe_list_buttons'). Append (button); } }); }, Generate_purchase_order:function () {var self=This ; New Instance.web.Model (Self.dataset.model). Call ("Generate_po", [Self.dataset.context.tender_id,self.dataset.context]). Then (function (result) {self. ViewManager.ActionManager.history_back (); }); }, });
The first line of the Add method is to associate a new View_mode name with a custom Instance.web.purchase_requisition.CompareListVIew, and the new View_mode is inherited from the web. ListView, so it has all the properties and method definitions of the original ListView. In the new View_mode init method, a new button is added via jquery, and the button's Click event Response Generate_purchase_order method is specified, and this method is defined on the following side, triggering the purchase in this method. The Generate_po method in _requisition.py, after execution and fallback to the original page that opened the view.
The above includes JS Add custom button, and respond to the method flow in the Py, and also use the inheritance of View_mode, provide a reference for customizing the Richer view.
Finally, by the way, record the code that opens this view is:
defOpen_product_line (Self, CR, UID, IDS, context=None):"""This opens product line view to view all lines from the different quotations, groupby default by product and partner To show comparaison between supplier Price @return: The product line tree view""" ifContext isNone:context={} res= Self.pool.get ('Ir.actions.act_window'). for_xml_id (CR, UID,'purchase_requisition','Purchase_line_tree', context=context) res['Context'] =Context Po_lines= Self.browse (CR, UID, IDS, context=context) [0].po_line_ids res['Context'] = { 'search_default_groupby_product': True,'search_default_hide_cancelled': True,'tender_id': Ids[0],} res['Domain'] = [('ID','inch', [line.id forLineinchPo_lines]) returnRes
Odoo8 Add custom buttons to the upper left corner of the TreeView and generate custom View_mode from inheritance