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.
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.
product.template.view.tree.inherit.button.on.tree.view
product.template
button_in_tree
The ‘Create Order‘ button is now visible in the product list view, and clicking it launches our wizard.
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.
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.
product.template.view.form.inherit.button.on.tree.view
product.template
button_in_form
The “Create Order” button is now shown next to the “Create” button in the product form view.
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.
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.



