【问题标题】:How to use 'like' operator in the attrs attribute to hide or show a button如何在 attrs 属性中使用“like”运算符来隐藏或显示按钮
【发布时间】:2016-09-06 18:22:54
【问题描述】:

我需要根据 Char 字段中是否包含特定字符串来隐藏或显示按钮。 'like' 运算符似乎是完美的。在我的 xml 中,我有:

 <record model="ir.ui.view" id="my_view">
     <field name="name">my.form</field>
     <field name="model">mymodule.my</field>
     <field name="arch" type="xml">
         <form string="My Form">
             <header>
                 <button name="test_like_1" type="object"
                         string="Should not appear"
                         attrs="{'invisible':[('state2','like','measure')]}"
                 />
                 <button name="test_like_2" type="object"
                         string="Should appear"
                         attrs="{'invisible':[('state2','not like','measure')]}"
                 />
...

State2 包含“measure,prelien”,所以我希望第一个按钮不可见,而第二个按钮可见。但是,两者都是不可见的。

我错过了什么?

编辑

我运行了我认为 Odoo 将从该域创建的查询 -

select id, description, state2 from mymodule_my where state2 like '%measure%';

它按预期运行,返回具有“测量”作为子字符串的记录。不知何故,这个 SQL 没有被生成/使用。我的下一步是挖掘 Odoo 代码并查看发生了什么。

谁能提供关于发生了什么的见解?

【问题讨论】:

  • @Kenly - 我已经读了好几遍了。根据我在该答案中的理解,我所拥有的应该可以工作,但事实并非如此。感觉好像我遗漏了一些明显的东西,但我找不到它。

标签: openerp odoo-9


【解决方案1】:

我发现了问题 - available operators for attrs in a view 对其进行了最好的描述并概述了一种可能的解决方案。要点是 attrs 中指定的域在客户端上的 javascript 中进行评估。 'like' 和 'ilike' 运算符未实现。

您可以通过查看控制台来验证这一点。就我而言,我收到了很多这样的警告 -

Unsupported operator ilike in domain [["state2","ilike","measure"]]

我正在考虑按照建议的1 扩展 odoo/addons/web/static/src/js/framework/data.js 中的 compute_domain 函数,或者只是解决限制。

【讨论】:

    【解决方案2】:

    你可以试试

    attrs="{'invisible':[('state2','in',['Measure','MEASURE','measure'])]}"
    

    attrs="{'invisible':[('state2','not in',['Measure','MEASURE','measure'])]}"
    

    您可能需要向列表中添加更多项目。我不确定是否支持喜欢和不喜欢,但这是我在其他插件中看到的一种方法。

    【讨论】:

    • 那行不通 - 我需要知道 state2 中是否包含字符串“measure”,而不是它是否是“measure”的变体。
    • 你可以破解这个问题。不要误会我的意思,我认为应该可以。除了域和属性之外,我在源代码中找不到它的示例。要解决此问题,您只需制作一个处理这两个功能的通用按钮。因此,该按钮执行一个函数,该函数评估“测量”的存在并适当地重定向到您要运行的实际函数,或者您将两个函数合并为一个并有一个 if 语句检查字符串并简单地运行适当的代码。
    【解决方案3】:

    您正在比较字符串 'state2' 而不是字段 state2 的值,您也应该反过来比较,这不是最好的例子,但您应该明白我的意思。

    >>> 'measure,prelien' in 'measure'
    False
    >>> 'measure' in 'measure,prelien'
    True
    >>> 
    

    第一个条件永远不会为真。你应该这样做

    定义一个名为 default 的 char 字段,并将其默认值设置为 'measure' 并使其隐藏在视图中

    measure = fields.Char('Measure', default='measure', store=False)
    

    那么你的视图应该是这样的

    <field name="measure" invisible="1" />
    
    <button name="test_like_1" type="object"
                             string="Should not appear"
                             attrs="{'invisible':[('measure', 'like', state2)]}"
                     />
    <button name="test_like_2" type="object"
                             string="Should appear"
                             attrs="{'invisible':[('measure', 'not like', state2)]}"
                     />
    

    【讨论】:

    • 我以前试过。它给出了这个错误 - 未捕获的错误:域中的未知字段度量 [["measure","ilike","state2"]]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多