简体   繁体   中英

In odoo 10, how can I show a message like bootstrap alert?

I would like to add an alert in an on-change method, but without raising warning or user error. Just showing a like-bootstrap alert, without interrupting the possibility of the user to save the data. Similar to what happens when an invoice is validated.

How to do that please?

I don't know if this is the best way to do it but this worked for me.

In your views, place a bootstrap alert inside your xml field:

<field name="arch" type="xml">
    <form string="My Form">
        <div class="alert alert-success alert-dismissible" invisible="not context.get('show_message', False)">
            <a href="#" class="close" data-dismiss="alert" aria-label="close">X</a>
            <strong>Success!</strong> Indicates a successful or positive action.
        </div>
...

Notice the invisible attribute in the div element. So in your model, when handling an action, you can pass a variable show_message in the context. This worked for me:

@api.multi
def my_action(self):
    return {
        "type": "ir.actions.act_window",
        "res_model": "my_module.my_model",
        "views": [[False, "form"]],
        "res_id": self.id,
        "target": "main",
        "context": {'show_message': True},
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM