تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
1 الرد
357 أدوات العرض

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.

الصورة الرمزية
إهمال
أفضل إجابة

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

الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
2
أبريل 25
1077
0
مارس 15
3754
1
مارس 15
3695
0
يوليو 25
285
0
يونيو 25
499