【问题标题】:How to hide edit/create button on form by conditions?如何按条件隐藏表单上的编辑/创建按钮?
【发布时间】:2016-02-04 08:45:51
【问题描述】:

我是一名新的 Odoo 开发人员,当我的表单进入自定义状态时,我需要隐藏编辑按钮,因为安全问题,我需要这个。

当我尝试为表单赋予属性时,XML 中的这段代码不起作用。

<record model="ir.ui.view" id="pesan_form_view">
    <field name="name">pesan_service_form</field>
    <field name="model">pesan.service</field>
    <field name="arch" type="xml">
    <form string="Booking Service" attrs="{edit:'false':[('state','in','baru')]}">
    <!-- structure of form -->
</record>

我不知道为什么它不起作用。

【问题讨论】:

  • 当 state 为 'baru' 时,您可以将所有字段设置为只读..

标签: openerp xml-rpc odoo-8


【解决方案1】:

qWeb 条件不适用于FormView

你可以在这里查看(path_to_odoo/addons/web/static/src/js/framework/view.js):

 /**
  * Return whether the user can perform the action ('create', 'edit', 'delete') in this view.
  * An action is disabled by setting the corresponding attribute in the view's main element,
  * like: <form string="" create="false" edit="false" delete="false">
  */
  is_action_enabled: function(action) {
      var attrs = this.fields_view.arch.attrs;
      return (action in attrs) ? JSON.parse(attrs[action]) : true;
  },

此方法从 path_to_odoo/addons/web/static/src/xml/base.xml 中的模板 FormView.buttons 调用

<button t-if="widget.is_action_enabled('edit')"
    type="button"
    class="oe_form_button_edit btn btn-default btn-sm" accesskey="E">
    Edit
</button>

这些问题在 Odoo 中借助规则(ir.ruleOdoo 对象)解决了

您可以在 GUI 中查找和编辑规则:设置(顶部菜单)-> 安全(左侧菜单)-> 访问规则(左侧菜单)。为此,请在调试模式下使用 admin 用户

同时,您可以在模块的data.xml 中添加一些规则以进行导入。它们将在您安装或更新模块时添加。

小心! Record rules do not apply to the Administrator user.

同时你可以尝试扩展小部件FormView

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    试试这个代码。

    <record model="ir.ui.view" id="pesan_form_view">
        <field name="name">pesan_service_form</field>
        <field name="model">pesan.service</field>
        <field name="arch" type="xml">
        <form string="Booking Service" attrs="{'edit': [('state', 'in', ['baru'])]}">
        <!-- structure of form -->
    </record>
    

    【讨论】:

    • 我不知道 v8,但是这个解决方案不适用于 v9。
    • 好吧,我已经在 odoo9 中检查过了。请提供您使用的代码以获取更多详细信息。
    • 我检查了某个对象的 name&lt;form attrs="{'edit': [('name', 'not in', ['name1', 'name2'])]}"&gt;。它没有帮助。 nameChar 字段。也许这个解决方案只适用于field.Select
    • 当我继承某些视图时,此解决方案对我不起作用。我究竟做错了什么? &lt;xpath expr='(//form)' position="attributes"&gt; &lt;attribute name="attrs"&gt;{'edit': [('state', 'in', ['open'])]}&lt;/attribute&gt; &lt;/xpath&gt;
    猜你喜欢
    • 2021-07-25
    • 2014-04-12
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多