Skip to Content
Menu
This question has been flagged
1 Reply
233 Views

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.

Avatar
Discard
Best Answer

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

Avatar
Discard
Related Posts Replies Views Activity
2
Apr 25
975
0
Mar 15
3674
1
Mar 15
3630
0
Jun 25
272
1
Jun 25
574