Yes, it is possible to dynamically activate a specific page in an Odoo notebook based on conditions, but it requires JavaScript customization since XML alone does not provide conditional control over active notebook pages.
Here’s how you can achieve this in Odoo 17 (and it should also work for earlier versions):
Solution: Use JavaScript to Control Notebook Page Activation
1. Add an ID to Your Notebook Pages in the XML View
Assign unique IDs to the notebook pages you want to control.
Example XML:
<notebook>
<page string="First Page" id="page_1">
<group>
<field name="field_a" />
</group>
</page>
<page string="Second Page" id="page_2">
<group>
<field name="field_b" />
</group>
</page>
<page string="Third Page" id="page_3">
<group>
<field name="field_c" />
</group>
</page>
</notebook>
2. Add JavaScript to Dynamically Activate Pages
Write JavaScript to dynamically activate the notebook page based on conditions.
Create a JS file in your custom module:
odoo.define('custom_module.dynamic_notebook_page', function (require) {
"use strict";
const FormRenderer = require('web.FormRenderer');
const fieldRegistry = require('web.field_registry');
FormRenderer.include({
_renderTabPage: function () {
this._super.apply(this, arguments);
// Check conditions and activate the appropriate page
const fieldA = this.state.data.field_a; // Assuming `field_a` is your boolean field
const fieldB = this.state.data.field_b; // Assuming `field_b` is another boolean field
if (fieldA) {
this._activateNotebookPage('page_2'); // Activate second page
} else if (fieldB) {
this._activateNotebookPage('page_3'); // Activate third page
}
},
_activateNotebookPage: function (pageId) {
const notebook = this.el.querySelector('.o_notebook'); // Target notebook
const page = notebook.querySelector(`[id="${pageId}"]`); // Find the page
if (page) {
const tabs = notebook.querySelectorAll('.o_notebook_tab');
tabs.forEach(tab => tab.classList.remove('active'));
const tab = notebook.querySelector(`[href="#${pageId}"]`);
tab.classList.add('active');
// Hide all pages and show the active page
const pages = notebook.querySelectorAll('.o_notebook_page');
pages.forEach(page => page.classList.add('o_notebook_page_hidden'));
page.classList.remove('o_notebook_page_hidden');
}
},
});
});
3. Load the JS File in Your Module
Ensure the JavaScript file is included in your module.
Add the following to your module’s __manifest__.py:
'assets': {
'web.assets_backend': [
'/custom_module/static/src/js/dynamic_notebook_page.js',
],
},
4. Install and Test
- Install or upgrade your module.
- Open the form view with the notebook.
- Check if the correct page is activated based on the boolean fields.
Alternative Approach: Use autofocus Attribute
If JavaScript is not preferred, you can use the autofocus attribute to focus on a specific field on another page. However, this might not work reliably in Odoo 17 (as mentioned in your example).
Example:
<field name="field_b" autofocus="autofocus" />
Key Considerations
- Performance: Ensure JavaScript modifications are efficient and don’t impact page load times.
- Testing: Test thoroughly in different scenarios to ensure correct page activation.
This approach allows you to dynamically activate the desired notebook page based on conditions. Let me know if you need further clarification or help!