I want to add a custom selection field above the tree view in Odoo. Based on the selected value (for example, a specific date), the tree view should filter and display only the records that match that selection. How can I implement this functionality using OWL and custom controllers? Any guidance would be appreciated.
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- CRM
- e-Commerce
- Buchhaltung
- Lager
- PoS
- Project
- MRP
Diese Frage wurde gekennzeichnet
Hii
here is simple example for you to guide for your work
Create a custom JS file in your module, e.g., static/src/js/list_filter_controller.js.
/** @odoo-module **/
import { ListController } from "@web/views/list/list_controller";
import { registry } from "@web/core/registry";
import { useState } from "@odoo/owl";
export class CustomListController extends ListController {
setup() {
super.setup();
this.state = useState({ filterDate: null });
}
async onFilterChange(ev) {
const selectedDate = ev.target.value;
this.state.filterDate = selectedDate;
await this.model.load({
filters: this.state.filterDate
? [["your_date_field", "=", this.state.filterDate]]
: [],
});
}
renderButtons() {
const $buttons = super.renderButtons();
const dropdown = document.createElement("select");
const defaultOption = document.createElement("option");
defaultOption.value = "";
defaultOption.text = "Select a date";
dropdown.appendChild(defaultOption);
const dates = ["2024-06-01", "2024-06-10", "2024-06-13"]; // Example static dates
dates.forEach((date) => {
const option = document.createElement("option");
option.value = date;
option.text = date;
dropdown.appendChild(option);
});
dropdown.addEventListener("change", this.onFilterChange.bind(this));
$buttons.append(dropdown);
return $buttons;
}
}
// Register controller for specific model/view
registry.category("views").add("custom_list_controller", {
...registry.category("views").get("list"),
Controller: CustomListController,
});
Update your model’s view (ir.ui.view) definition to use your custom controller:
<record id="view_your_model_tree" model="ir.ui.view">
<field name="name">your.model.tree</field>
<field name="model">your.model</field>
<field name="arch" type="xml">
<tree js_class="custom_list_controller">
<field name="name"/>
<field name="your_date_field"/>
<!-- your fields -->
</tree>
</field>
</record>
In __manifest__.py:
"assets": {
"web.assets_backend": [
"your_module/static/src/js/list_filter_controller.js",
],
}
i hope this example help full
Diskutieren Sie gerne? Treten Sie bei, statt nur zu lesen!
Erstellen Sie heute ein Konto, um exklusive Funktionen zu nutzen und mit unserer tollen Community zu interagieren!
RegistrierenVerknüpfte Beiträge | Antworten | Ansichten | Aktivität | |
---|---|---|---|---|
|
2
Juli 25
|
274 | ||
|
2
Apr. 25
|
1157 | ||
|
0
März 15
|
3824 | ||
|
1
März 15
|
3785 | ||
|
1
Juli 25
|
335 |