Skip to Content
Menu
This question has been flagged
3 Replies
756 Views

In an owl template, I would like to add a groups option, but it doens't work, it doesn't give an error but it's ignore, the button is displayed for every user. How can I limit the button visibility to a group of user like in forms ?

<t t-name="test.ListView.Buttons" t-inherit="web.ListView.Buttons" t-inherit-mode="primary">  
​<xpath expr="//div[hasclass('o_list_buttons')]" position="inside">
            <button type="button" class="btn btn-primary" groups="project.group_project_manager" t-on-click="AddNewClick">Test</button>             
   </xpath>   
</t>
Avatar
Discard
Best Answer

Hi,


In OWL templates, the groups attribute doesn't work directly as it does in traditional XML views. Instead, you need to manage group-based visibility through the JS.


XML:


<?xml version="1.0" encoding="UTF-8"?>

<templates>

    <t t-name="test.ListView.Buttons" t-inherit="web.ListView.Buttons" t-inherit-mode="extension">

        <xpath expr="//div[hasclass('o_list_buttons')]" position="inside">

            <button type="button" t-if="state.canSeeButton" class="btn btn-primary" t-on-click="onAddNewClick">Test</button>

        </xpath>

    </t>

</templates>


JS:


/** @odoo-module **/

import { ListController } from "@web/views/list/list_controller";

import { patch } from "@web/core/utils/patch";

import { useService } from "@web/core/utils/hooks";

import { useState, onWillStart } from "@odoo/owl";


patch(ListController.prototype, {

    setup() {

        super.setup();

        this.user = useService("user");

        this.state = useState({

            canSeeButton: false, // Initial button visibility state

        });


        // Check if the user belongs to the "project.group_project_manager" group

        onWillStart(async () => {

            this.state.canSeeButton = await this.user.hasGroup("project.group_project_manager");

        });

    },


    onAddNewClick() {

        //Add code according to the logic of the test button click

    }

});


Ensure that both the JS and XML files are loaded by adding them to your module's __manifest__.py under the assets section:

'assets': {

    'web.assets_backend': [

        'path/to/your/js/file',

        'path/to/your/xml/file',

    ],

},



Hope it helps

Avatar
Discard
Best Answer

since version 18 , You cannot access to useService("user"), You can use


import { user } from "@web/core/user";


instead, with

user.hasGroup("my.group.id") by a async call

Avatar
Discard
Author Best Answer

It works! Thanks.

Avatar
Discard