how-to-add-a-new-button-next-to-the-create-button-in-tree-and-form-views-in-odoo-18

How to Add a New Button Next to the Create Button in Tree and Form Views in Odoo 18?

In Odoo 18, customizing the user interface is essential for enhancing usability and workflow efficiency. One common requirement is adding custom buttons next to the default “Create” button in Tree and Form views. This feature allows developers to add quick actions, such as importing data, launching wizards, or automating specific tasks, directly from the view. In this blog, we’ll explore how to add a new button beside the Create button using XML and Python in Odoo 18.

Add a New Button or Custom Button in List View

Sometimes, you may need to add a custom button to a specific tree view, such as the header of the Sales Order or Product list view.

Create the templates file inside the static/src/xml directory

The ‘web.ListView.Buttons’ template, which defines the list view buttons, must be inherited to add a button to the list view. The ‘Create Order’ button and associated ‘t-on-click’ functionality will be included in this template.

				
					<?xml version="1.0" encoding="UTF-8"?>
<templates id="template" xml:space="preserve">
<t t-name="button_on_tree_view.ListView.Buttons" t-inherit="web.ListView">
     <xpath expr="//Layout/t[@t-set-slot='control-panel-create-button']" position="inside">
        <button type="button" class="btn btn-primary" style="margin-right: 10px;" t-on-click="OnClickOrder">
            Create Order
        </button>
    </xpath>
</t>
</templates>

				
			

Create a JS file inside the static/src/js directory

We can add functionality to our template and the default ListController component. A new ‘button_in_tree’ item can then be added to the view register accordingly.

The “t-on-click” function of the button can be defined in the JavaScript file. This is where we may use the “doAction” method to launch a sale order wizard. There is no need to import the action from the hook again because it has already been added to the default ListController.

				
					/** @odoo-module */
import { ListController } from "@web/views/list/list_controller";
import { registry } from '@web/core/registry';
import { listView } from '@web/views/list/list_view';
export class ProductController extends ListController {
 setup() {
     super.setup();
 }
 OnClickOrder() {
     this.actionService.doAction({
        type: 'ir.actions.act_window',
        res_model: 'sale.order',
        name:'Sale Order',
        view_mode: 'form',
        view_type: 'form',
        views: [[false, 'form']],
        target: 'new',
        res_id: false,
    });
 }
}
ProductController.template = "button_on_tree_view.ListView.Buttons";
export const CustomProductController = {
  ...listView,
  Controller: ProductController,
};
registry.category("views").add("button_in_tree", CustomProductController);

				
			

Create the view file for the list inside the view directory

This particular JS class should now be included in the Product template’s treeview.

				
					<odoo>
  <record id="product_template_tree_view" model="ir.ui.view">
      <field name="name">product.template.view.tree.inherit.button.on.tree.view</field>
      <field name="model">product.template</field>
      <field name="inherit_id" ref="product.product_template_tree_view"/>
      <field name="arch" type="xml">
          <xpath expr="//list" position="attributes">
              <attribute name="js_class">button_in_tree</attribute>
          </xpath>
      </field>
  </record>
</odoo>

				
			

The ‘Create Order‘ button is now visible in the product list view, and clicking it launches our wizard.

how-to-add-a-new-button-next-to-the-create-button-in-tree-and-form-views-in-odoo-18

Custom Button in Form View

Now, let’s look at how to add a custom button to the product form view.

Add the form_button.xml file to the static/src/xml directory

In this instance, we use the XPath expression to add our button to the “web.FormView” template, which we extend by inheriting it. The word “Create Order” is contained within an element, which will be displayed as the button’s label.

				
					<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
  <t t-name="button_on_tree_view.FormView.Buttons" t-inherit="web.FormView">
      <xpath expr="//t[@t-set-slot='layout-buttons']" position="inside">
          <button type="button" class="btn btn-primary"
             t-on-click="OnFormClickOrder">
             <span>Create Order</span>
         </button>
      </xpath>
  </t>
</templates>

				
			

Add the form_button.js file to the static/src/js directory

In this instance, we can add our template and the default FormController component. After that, we can add a new “button_in_form” item to the view register. Additionally, provide the “Create order” button functionality.

This involves importing an action from the “useService” hook.

				
					/** @odoo-module */
import { FormController } from "@web/views/form/form_controller";
import { useService } from "@web/core/utils/hooks";
import { registry } from '@web/core/registry';
import { formView } from '@web/views/form/form_view';
export class ProductFormController extends FormController {
 setup() {
     super.setup();
     this.actionService = useService("action");
 }
 OnFormClickOrder() {
     this.actionService.doAction({
        type: 'ir.actions.act_window',
        res_model: 'sale.order',
        name:'Sale Order',
        view_mode: 'form',
        view_type: 'form',
        views: [[false, 'form']],
        target: 'new',
        res_id: false,
    });
 }
}
ProductFormController.template = "button_on_tree_view.FormView.Buttons";
export const CustomProductFormController = {
  ...formView,
  Controller: ProductFormController,
};
registry.category("views").add("button_in_form", CustomProductFormController);
				
			

Create the view file for Form inside the view directory

The new class must now be specified in the view to make this new button appear in the product template model’s form view. We can do so by using the “js_class” attribute in the view’s form tag.

				
					<?xml version="1.0" encoding="UTF-8"?>
<odoo>
   <record id="product_template_only_form_view" model="ir.ui.view">
       <field name="name">product.template.view.form.inherit.button.on.tree.view</field>
       <field name="model">product.template</field>
       <field name="inherit_id" ref="product.product_template_only_form_view"/>
       <field name="arch" type="xml">
           <xpath expr="//form" position="attributes">
               <attribute name="js_class">button_in_form</attribute>
           </xpath>
       </field>
   </record>
</odoo>

				
			

The “Create Order” button is now shown next to the “Create” button in the product form view.

how-to-add-a-new-button-next-to-the-create-button-in-tree-and-form-views-in-odoo-18

The do_action method defines a specific wizard that opens when the newly added button is clicked in both the form and tree views. A sale order creation wizard is used in this example.

how-to-add-a-new-button-next-to-the-create-button-in-tree-and-form-views-in-odoo-18

Build Smarter UI with Custom Buttons

Adding a custom button next to the “Create” button in Odoo 18 enhances user efficiency and provides quick access to frequently used actions. By inheriting the web.ListView.By extending the buttons template with custom functionality, developers can seamlessly integrate new features without disrupting the existing UI. This approach not only improves workflow automation but also creates a more intuitive, user-friendly experience in Odoo.

Picture of Ilsa Shabbir

Ilsa Shabbir

Hi, I'm Ilsa Shabbir, an Odoo ERP content writer specializing in functional blogs on Odoo modules. I simplify complex Odoo features, creating clear and engaging content to help businesses understand and optimize their ERP solutions.

Get a free quote