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
- Boekhouding
- Voorraad
- PoS
- Project
- MRP
Deze vraag is gerapporteerd
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
Geniet je van het gesprek? Blijf niet alleen lezen, doe ook mee!
Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!
AanmeldenGerelateerde posts | Antwoorden | Weergaven | Activiteit | |
---|---|---|---|---|
|
2
jul. 25
|
272 | ||
|
2
apr. 25
|
1157 | ||
|
0
mrt. 15
|
3823 | ||
|
1
mrt. 15
|
3780 | ||
|
1
jul. 25
|
324 |