Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
453 Lượt xem

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.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 7 25
274
2
thg 4 25
1157
0
thg 3 15
3824
1
thg 3 15
3784
1
thg 7 25
332